blob: 8ce67c8f9c28640e3265c7efc6d78ec11254565a [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 Bataev48c0bfb2016-01-20 09:07:54 +000093 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000094 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000095 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
96 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000097
98 struct SharingMapTy {
99 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000100 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000101 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000102 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000105 OpenMPDirectiveKind Directive;
106 DeclarationNameInfo DirectiveName;
107 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000108 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000109 /// \brief first argument (Expr *) contains optional argument of the
110 /// 'ordered' clause, the second one is true if the regions has 'ordered'
111 /// clause, false otherwise.
112 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000113 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000114 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000115 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000116 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000117 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000118 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000119 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000120 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000121 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000122 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000124 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000125 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000126 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000127 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000128 };
129
Axel Naumann323862e2016-02-03 10:45:22 +0000130 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000131
132 /// \brief Stack of used declaration and their data-sharing attributes.
133 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000134 /// \brief true, if check for DSA must be from parent directive, false, if
135 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000136 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000137 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000138 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000139 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140
141 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
142
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000143 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000144
145 /// \brief Checks if the variable is a local for OpenMP region.
146 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000147
Alexey Bataev758e55e2013-09-06 18:03:48 +0000148public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000149 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000150 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
151 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000152
Alexey Bataevaac108a2015-06-23 04:51:00 +0000153 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
154 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000156 bool isForceVarCapturing() const { return ForceCapturing; }
157 void setForceVarCapturing(bool V) { ForceCapturing = V; }
158
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000160 Scope *CurScope, SourceLocation Loc) {
161 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
162 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000163 }
164
165 void pop() {
166 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
167 Stack.pop_back();
168 }
169
Alexey Bataev28c75412015-12-15 08:19:24 +0000170 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
171 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
172 }
173 const std::pair<OMPCriticalDirective *, llvm::APSInt>
174 getCriticalWithHint(const DeclarationNameInfo &Name) const {
175 auto I = Criticals.find(Name.getAsString());
176 if (I != Criticals.end())
177 return I->second;
178 return std::make_pair(nullptr, llvm::APSInt());
179 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000180 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000181 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000182 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000183 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000184
Alexey Bataev9c821032015-04-30 04:23:23 +0000185 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000186 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000187 /// \brief Check if the specified variable is a loop control variable for
188 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000189 /// \return The index of the loop control variable in the list of associated
190 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000191 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000192 /// \brief Check if the specified variable is a loop control variable for
193 /// parent region.
194 /// \return The index of the loop control variable in the list of associated
195 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000196 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000197 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
198 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000199 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000200
Alexey Bataev758e55e2013-09-06 18:03:48 +0000201 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000202 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
203 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000204
Alexey Bataev758e55e2013-09-06 18:03:48 +0000205 /// \brief Returns data sharing attributes from top of the stack for the
206 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000207 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000208 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000209 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000210 /// \brief Checks if the specified variables has data-sharing attributes which
211 /// match specified \a CPred predicate in any directive which matches \a DPred
212 /// predicate.
213 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000214 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000215 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000216 /// \brief Checks if the specified variables has data-sharing attributes which
217 /// match specified \a CPred predicate in any innermost directive which
218 /// matches \a DPred predicate.
219 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000220 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
221 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000222 /// \brief Checks if the specified variables has explicit data-sharing
223 /// attributes which match specified \a CPred predicate at the specified
224 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000225 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000226 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
227 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000228
229 /// \brief Returns true if the directive at level \Level matches in the
230 /// specified \a DPred predicate.
231 bool hasExplicitDirective(
232 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
233 unsigned Level);
234
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000235 /// \brief Finds a directive which matches specified \a DPred predicate.
236 template <class NamedDirectivesPredicate>
237 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000238
Alexey Bataev758e55e2013-09-06 18:03:48 +0000239 /// \brief Returns currently analyzed directive.
240 OpenMPDirectiveKind getCurrentDirective() const {
241 return Stack.back().Directive;
242 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000243 /// \brief Returns parent directive.
244 OpenMPDirectiveKind getParentDirective() const {
245 if (Stack.size() > 2)
246 return Stack[Stack.size() - 2].Directive;
247 return OMPD_unknown;
248 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000249 /// \brief Return the directive associated with the provided scope.
250 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000251
252 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000253 void setDefaultDSANone(SourceLocation Loc) {
254 Stack.back().DefaultAttr = DSA_none;
255 Stack.back().DefaultAttrLoc = Loc;
256 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000257 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000258 void setDefaultDSAShared(SourceLocation Loc) {
259 Stack.back().DefaultAttr = DSA_shared;
260 Stack.back().DefaultAttrLoc = Loc;
261 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000262
263 DefaultDataSharingAttributes getDefaultDSA() const {
264 return Stack.back().DefaultAttr;
265 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000266 SourceLocation getDefaultDSALocation() const {
267 return Stack.back().DefaultAttrLoc;
268 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000269
Alexey Bataevf29276e2014-06-18 04:14:57 +0000270 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000271 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000272 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000273 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000274 }
275
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000276 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000277 void setOrderedRegion(bool IsOrdered, Expr *Param) {
278 Stack.back().OrderedRegion.setInt(IsOrdered);
279 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000280 }
281 /// \brief Returns true, if parent region is ordered (has associated
282 /// 'ordered' clause), false - otherwise.
283 bool isParentOrderedRegion() const {
284 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000285 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000286 return false;
287 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000288 /// \brief Returns optional parameter for the ordered region.
289 Expr *getParentOrderedRegionParam() const {
290 if (Stack.size() > 2)
291 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
292 return nullptr;
293 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000294 /// \brief Marks current region as nowait (it has a 'nowait' clause).
295 void setNowaitRegion(bool IsNowait = true) {
296 Stack.back().NowaitRegion = IsNowait;
297 }
298 /// \brief Returns true, if parent region is nowait (has associated
299 /// 'nowait' clause), false - otherwise.
300 bool isParentNowaitRegion() const {
301 if (Stack.size() > 2)
302 return Stack[Stack.size() - 2].NowaitRegion;
303 return false;
304 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000305 /// \brief Marks parent region as cancel region.
306 void setParentCancelRegion(bool Cancel = true) {
307 if (Stack.size() > 2)
308 Stack[Stack.size() - 2].CancelRegion =
309 Stack[Stack.size() - 2].CancelRegion || Cancel;
310 }
311 /// \brief Return true if current region has inner cancel construct.
312 bool isCancelRegion() const {
313 return Stack.back().CancelRegion;
314 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000315
Alexey Bataev9c821032015-04-30 04:23:23 +0000316 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000317 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000318 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000319 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000320
Alexey Bataev13314bf2014-10-09 04:18:56 +0000321 /// \brief Marks current target region as one with closely nested teams
322 /// region.
323 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
324 if (Stack.size() > 2)
325 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
326 }
327 /// \brief Returns true, if current region has closely nested teams region.
328 bool hasInnerTeamsRegion() const {
329 return getInnerTeamsRegionLoc().isValid();
330 }
331 /// \brief Returns location of the nested teams region (if any).
332 SourceLocation getInnerTeamsRegionLoc() const {
333 if (Stack.size() > 1)
334 return Stack.back().InnerTeamsRegionLoc;
335 return SourceLocation();
336 }
337
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000338 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000339 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000340 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000341
Samuel Antao5de996e2016-01-22 20:21:36 +0000342 // Do the check specified in MapInfoCheck and return true if any issue is
343 // found.
344 template <class MapInfoCheck>
345 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
346 MapInfoCheck Check) {
347 auto SI = Stack.rbegin();
348 auto SE = Stack.rend();
349
350 if (SI == SE)
351 return false;
352
353 if (CurrentRegionOnly) {
354 SE = std::next(SI);
355 } else {
356 ++SI;
357 }
358
359 for (; SI != SE; ++SI) {
360 auto MI = SI->MappedDecls.find(VD);
361 if (MI != SI->MappedDecls.end()) {
362 for (Expr *E : MI->second) {
363 if (Check(E))
364 return true;
365 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000366 }
367 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000368 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000369 }
370
Samuel Antao5de996e2016-01-22 20:21:36 +0000371 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000372 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000373 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000374 }
375 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000377bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
378 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000379 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000380 isOpenMPTaskLoopDirective(DKind);
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
Alexey Bataev758e55e2013-09-06 18:03:48 +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 Bataev758e55e2013-09-06 18:03:48 +0000476 if (DVar.DKind == OMPD_task) {
477 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
481 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000482 // in a Construct, implicitly determined, p.6]
483 // In a task construct, if no default clause is present, a variable
484 // whose data-sharing attribute is not determined by the rules above is
485 // firstprivate.
486 DVarTemp = getDSA(I, D);
487 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000488 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000490 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 return DVar;
492 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000493 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000494 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000495 }
496 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000497 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000498 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000499 return DVar;
500 }
501 }
502 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
503 // in a Construct, implicitly determined, p.3]
504 // For constructs other than task, if no default clause is present, these
505 // variables inherit their data-sharing attributes from the enclosing
506 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000507 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000508}
509
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000510Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000511 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000512 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000513 auto It = Stack.back().AlignedMap.find(D);
514 if (It == Stack.back().AlignedMap.end()) {
515 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
516 Stack.back().AlignedMap[D] = NewDE;
517 return nullptr;
518 } else {
519 assert(It->second && "Unexpected nullptr expr in the aligned map");
520 return It->second;
521 }
522 return nullptr;
523}
524
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000525void DSAStackTy::addLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000526 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000527 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000528 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000529}
530
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000532 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000533 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000534 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
535}
536
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000537unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000538 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000539 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000540 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
541 ? Stack[Stack.size() - 2].LCVMap[D]
542 : 0;
543}
544
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000545ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000546 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
547 if (Stack[Stack.size() - 2].LCVMap.size() < I)
548 return nullptr;
549 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
550 if (Pair.second == I)
551 return Pair.first;
552 }
553 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000554}
555
Alexey Bataev90c228f2016-02-08 09:29:13 +0000556void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
557 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000558 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000559 if (A == OMPC_threadprivate) {
560 Stack[0].SharingMap[D].Attributes = A;
561 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000562 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000563 } else {
564 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
565 Stack.back().SharingMap[D].Attributes = A;
566 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000567 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
568 if (PrivateCopy)
569 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000570 }
571}
572
Alexey Bataeved09d242014-05-28 05:53:51 +0000573bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000574 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000575 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000576 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000577 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000578 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000579 ++I;
580 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000581 if (I == E)
582 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000583 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000584 Scope *CurScope = getCurScope();
585 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000586 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000587 }
588 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000589 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000590 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000591}
592
Alexey Bataev39f915b82015-05-08 10:41:21 +0000593/// \brief Build a variable declaration for OpenMP loop iteration variable.
594static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000595 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000596 DeclContext *DC = SemaRef.CurContext;
597 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
598 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
599 VarDecl *Decl =
600 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000601 if (Attrs) {
602 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
603 I != E; ++I)
604 Decl->addAttr(*I);
605 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000606 Decl->setImplicit();
607 return Decl;
608}
609
610static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
611 SourceLocation Loc,
612 bool RefersToCapture = false) {
613 D->setReferenced();
614 D->markUsed(S.Context);
615 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
616 SourceLocation(), D, RefersToCapture, Loc, Ty,
617 VK_LValue);
618}
619
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000620DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
621 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000622 DSAVarData DVar;
623
624 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
625 // in a Construct, C/C++, predetermined, p.1]
626 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000627 auto *VD = dyn_cast<VarDecl>(D);
628 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
629 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000630 SemaRef.getLangOpts().OpenMPUseTLS &&
631 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000632 (VD && VD->getStorageClass() == SC_Register &&
633 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
634 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000635 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000636 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000637 }
638 if (Stack[0].SharingMap.count(D)) {
639 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
640 DVar.CKind = OMPC_threadprivate;
641 return DVar;
642 }
643
644 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000645 // in a Construct, C/C++, predetermined, p.4]
646 // Static data members are shared.
647 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
648 // in a Construct, C/C++, predetermined, p.7]
649 // Variables with static storage duration that are declared in a scope
650 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000651 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000652 DSAVarData DVarTemp =
653 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
654 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000655 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000656
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000657 DVar.CKind = OMPC_shared;
658 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000659 }
660
661 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000662 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
663 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
665 // in a Construct, C/C++, predetermined, p.6]
666 // Variables with const qualified type having no mutable member are
667 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000668 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000669 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000670 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
671 if (auto *CTD = CTSD->getSpecializedTemplate())
672 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000674 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
675 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000676 // Variables with const-qualified type having no mutable member may be
677 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000678 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
679 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000680 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
681 return DVar;
682
Alexey Bataev758e55e2013-09-06 18:03:48 +0000683 DVar.CKind = OMPC_shared;
684 return DVar;
685 }
686
Alexey Bataev758e55e2013-09-06 18:03:48 +0000687 // Explicitly specified attributes and local variables with predetermined
688 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000689 auto StartI = std::next(Stack.rbegin());
690 auto EndI = std::prev(Stack.rend());
691 if (FromParent && StartI != EndI) {
692 StartI = std::next(StartI);
693 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000694 auto I = std::prev(StartI);
695 if (I->SharingMap.count(D)) {
696 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000697 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000698 DVar.CKind = I->SharingMap[D].Attributes;
699 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000700 }
701
702 return DVar;
703}
704
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000705DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
706 bool FromParent) {
707 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000708 auto StartI = Stack.rbegin();
709 auto EndI = std::prev(Stack.rend());
710 if (FromParent && StartI != EndI) {
711 StartI = std::next(StartI);
712 }
713 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000714}
715
Alexey Bataevf29276e2014-06-18 04:14:57 +0000716template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000717DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000718 DirectivesPredicate DPred,
719 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000720 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000721 auto StartI = std::next(Stack.rbegin());
722 auto EndI = std::prev(Stack.rend());
723 if (FromParent && StartI != EndI) {
724 StartI = std::next(StartI);
725 }
726 for (auto I = StartI, EE = EndI; I != EE; ++I) {
727 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000728 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000729 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000730 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000731 return DVar;
732 }
733 return DSAVarData();
734}
735
Alexey Bataevf29276e2014-06-18 04:14:57 +0000736template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000737DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000738DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000739 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000740 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000741 auto StartI = std::next(Stack.rbegin());
742 auto EndI = std::prev(Stack.rend());
743 if (FromParent && StartI != EndI) {
744 StartI = std::next(StartI);
745 }
746 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000747 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000748 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000749 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000750 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000751 return DVar;
752 return DSAVarData();
753 }
754 return DSAVarData();
755}
756
Alexey Bataevaac108a2015-06-23 04:51:00 +0000757bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000758 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000759 unsigned Level) {
760 if (CPred(ClauseKindMode))
761 return true;
762 if (isClauseParsingMode())
763 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000764 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000765 auto StartI = Stack.rbegin();
766 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000767 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000768 return false;
769 std::advance(StartI, Level);
770 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
771 CPred(StartI->SharingMap[D].Attributes);
772}
773
Samuel Antao4be30e92015-10-02 17:14:03 +0000774bool DSAStackTy::hasExplicitDirective(
775 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
776 unsigned Level) {
777 if (isClauseParsingMode())
778 ++Level;
779 auto StartI = Stack.rbegin();
780 auto EndI = std::prev(Stack.rend());
781 if (std::distance(StartI, EndI) <= (int)Level)
782 return false;
783 std::advance(StartI, Level);
784 return DPred(StartI->Directive);
785}
786
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000787template <class NamedDirectivesPredicate>
788bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
789 auto StartI = std::next(Stack.rbegin());
790 auto EndI = std::prev(Stack.rend());
791 if (FromParent && StartI != EndI) {
792 StartI = std::next(StartI);
793 }
794 for (auto I = StartI, EE = EndI; I != EE; ++I) {
795 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
796 return true;
797 }
798 return false;
799}
800
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000801OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
802 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
803 if (I->CurScope == S)
804 return I->Directive;
805 return OMPD_unknown;
806}
807
Alexey Bataev758e55e2013-09-06 18:03:48 +0000808void Sema::InitDataSharingAttributesStack() {
809 VarDataSharingAttributesStack = new DSAStackTy(*this);
810}
811
812#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
813
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000814bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000815 const CapturedRegionScopeInfo *RSI) {
816 assert(LangOpts.OpenMP && "OpenMP is not allowed");
817
818 auto &Ctx = getASTContext();
819 bool IsByRef = true;
820
821 // Find the directive that is associated with the provided scope.
822 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000823 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000824
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000825 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000826 // This table summarizes how a given variable should be passed to the device
827 // given its type and the clauses where it appears. This table is based on
828 // the description in OpenMP 4.5 [2.10.4, target Construct] and
829 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
830 //
831 // =========================================================================
832 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
833 // | |(tofrom:scalar)| | pvt | | | |
834 // =========================================================================
835 // | scl | | | | - | | bycopy|
836 // | scl | | - | x | - | - | bycopy|
837 // | scl | | x | - | - | - | null |
838 // | scl | x | | | - | | byref |
839 // | scl | x | - | x | - | - | bycopy|
840 // | scl | x | x | - | - | - | null |
841 // | scl | | - | - | - | x | byref |
842 // | scl | x | - | - | - | x | byref |
843 //
844 // | agg | n.a. | | | - | | byref |
845 // | agg | n.a. | - | x | - | - | byref |
846 // | agg | n.a. | x | - | - | - | null |
847 // | agg | n.a. | - | - | - | x | byref |
848 // | agg | n.a. | - | - | - | x[] | byref |
849 //
850 // | ptr | n.a. | | | - | | bycopy|
851 // | ptr | n.a. | - | x | - | - | bycopy|
852 // | ptr | n.a. | x | - | - | - | null |
853 // | ptr | n.a. | - | - | - | x | byref |
854 // | ptr | n.a. | - | - | - | x[] | bycopy|
855 // | ptr | n.a. | - | - | x | | bycopy|
856 // | ptr | n.a. | - | - | x | x | bycopy|
857 // | ptr | n.a. | - | - | x | x[] | bycopy|
858 // =========================================================================
859 // Legend:
860 // scl - scalar
861 // ptr - pointer
862 // agg - aggregate
863 // x - applies
864 // - - invalid in this combination
865 // [] - mapped with an array section
866 // byref - should be mapped by reference
867 // byval - should be mapped by value
868 // null - initialize a local variable to null on the device
869 //
870 // Observations:
871 // - All scalar declarations that show up in a map clause have to be passed
872 // by reference, because they may have been mapped in the enclosing data
873 // environment.
874 // - If the scalar value does not fit the size of uintptr, it has to be
875 // passed by reference, regardless the result in the table above.
876 // - For pointers mapped by value that have either an implicit map or an
877 // array section, the runtime library may pass the NULL value to the
878 // device instead of the value passed to it by the compiler.
879
880 // FIXME: Right now, only implicit maps are implemented. Properly mapping
881 // values requires having the map, private, and firstprivate clauses SEMA
882 // and parsing in place, which we don't yet.
883
884 if (Ty->isReferenceType())
885 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
886 IsByRef = !Ty->isScalarType();
887 }
888
889 // When passing data by value, we need to make sure it fits the uintptr size
890 // and alignment, because the runtime library only deals with uintptr types.
891 // If it does not fit the uintptr size, we need to pass the data by reference
892 // instead.
893 if (!IsByRef &&
894 (Ctx.getTypeSizeInChars(Ty) >
895 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000896 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000897 IsByRef = true;
898
899 return IsByRef;
900}
901
Alexey Bataev90c228f2016-02-08 09:29:13 +0000902VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000903 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000904 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000905
906 // If we are attempting to capture a global variable in a directive with
907 // 'target' we return true so that this global is also mapped to the device.
908 //
909 // FIXME: If the declaration is enclosed in a 'declare target' directive,
910 // then it should not be captured. Therefore, an extra check has to be
911 // inserted here once support for 'declare target' is added.
912 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000913 auto *VD = dyn_cast<VarDecl>(D);
914 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000915 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000916 !DSAStack->isClauseParsingMode())
917 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000918 if (DSAStack->getCurScope() &&
919 DSAStack->hasDirective(
920 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
921 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000922 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000923 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000924 false))
925 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000926 }
927
Alexey Bataev48977c32015-08-04 08:10:48 +0000928 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
929 (!DSAStack->isClauseParsingMode() ||
930 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000931 if (DSAStack->isLoopControlVariable(D) ||
932 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000933 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000934 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000935 return VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000936 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000937 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000938 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000939 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000940 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000941 if (DVarPrivate.CKind != OMPC_unknown)
942 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000943 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000944 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000945}
946
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000947bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000948 assert(LangOpts.OpenMP && "OpenMP is not allowed");
949 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000950 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000951}
952
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000953bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000954 assert(LangOpts.OpenMP && "OpenMP is not allowed");
955 // Return true if the current level is no longer enclosed in a target region.
956
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000957 auto *VD = dyn_cast<VarDecl>(D);
958 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000959 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
960 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000961}
962
Alexey Bataeved09d242014-05-28 05:53:51 +0000963void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000964
965void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
966 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000967 Scope *CurScope, SourceLocation Loc) {
968 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000969 PushExpressionEvaluationContext(PotentiallyEvaluated);
970}
971
Alexey Bataevaac108a2015-06-23 04:51:00 +0000972void Sema::StartOpenMPClause(OpenMPClauseKind K) {
973 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000974}
975
Alexey Bataevaac108a2015-06-23 04:51:00 +0000976void Sema::EndOpenMPClause() {
977 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000978}
979
Alexey Bataev758e55e2013-09-06 18:03:48 +0000980void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000981 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
982 // A variable of class type (or array thereof) that appears in a lastprivate
983 // clause requires an accessible, unambiguous default constructor for the
984 // class type, unless the list item is also specified in a firstprivate
985 // clause.
986 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000987 for (auto *C : D->clauses()) {
988 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
989 SmallVector<Expr *, 8> PrivateCopies;
990 for (auto *DE : Clause->varlists()) {
991 if (DE->isValueDependent() || DE->isTypeDependent()) {
992 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000993 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000994 }
Alexey Bataev74caaf22016-02-20 04:09:36 +0000995 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +0000996 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
997 QualType Type = VD->getType().getNonReferenceType();
998 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000999 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001000 // Generate helper private variable and initialize it with the
1001 // default value. The address of the original variable is replaced
1002 // by the address of the new private variable in CodeGen. This new
1003 // variable is not added to IdResolver, so the code in the OpenMP
1004 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001005 auto *VDPrivate = buildVarDecl(
1006 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001007 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001008 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1009 if (VDPrivate->isInvalidDecl())
1010 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001011 PrivateCopies.push_back(buildDeclRefExpr(
1012 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001013 } else {
1014 // The variable is also a firstprivate, so initialization sequence
1015 // for private copy is generated already.
1016 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001017 }
1018 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001019 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001020 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001021 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001022 }
1023 }
1024 }
1025
Alexey Bataev758e55e2013-09-06 18:03:48 +00001026 DSAStack->pop();
1027 DiscardCleanupsInEvaluationContext();
1028 PopExpressionEvaluationContext();
1029}
1030
Alexey Bataev5a3af132016-03-29 08:58:54 +00001031static bool
1032FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1033 Expr *NumIterations, Sema &SemaRef, Scope *S);
Alexander Musman3276a272015-03-21 10:12:56 +00001034
Alexey Bataeva769e072013-03-22 06:34:35 +00001035namespace {
1036
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001037class VarDeclFilterCCC : public CorrectionCandidateCallback {
1038private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001039 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001040
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001041public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001042 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001043 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001044 NamedDecl *ND = Candidate.getCorrectionDecl();
1045 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1046 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001047 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1048 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001049 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001051 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001052};
Alexey Bataeved09d242014-05-28 05:53:51 +00001053} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001054
1055ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1056 CXXScopeSpec &ScopeSpec,
1057 const DeclarationNameInfo &Id) {
1058 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1059 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1060
1061 if (Lookup.isAmbiguous())
1062 return ExprError();
1063
1064 VarDecl *VD;
1065 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001066 if (TypoCorrection Corrected = CorrectTypo(
1067 Id, LookupOrdinaryName, CurScope, nullptr,
1068 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001069 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001070 PDiag(Lookup.empty()
1071 ? diag::err_undeclared_var_use_suggest
1072 : diag::err_omp_expected_var_arg_suggest)
1073 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001074 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001075 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001076 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1077 : diag::err_omp_expected_var_arg)
1078 << Id.getName();
1079 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001080 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001081 } else {
1082 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001083 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001084 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1085 return ExprError();
1086 }
1087 }
1088 Lookup.suppressDiagnostics();
1089
1090 // OpenMP [2.9.2, Syntax, C/C++]
1091 // Variables must be file-scope, namespace-scope, or static block-scope.
1092 if (!VD->hasGlobalStorage()) {
1093 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001094 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1095 bool IsDecl =
1096 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001097 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001098 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1099 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001100 return ExprError();
1101 }
1102
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001103 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1104 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001105 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1106 // A threadprivate directive for file-scope variables must appear outside
1107 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001108 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1109 !getCurLexicalContext()->isTranslationUnit()) {
1110 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001111 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1112 bool IsDecl =
1113 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1114 Diag(VD->getLocation(),
1115 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1116 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001117 return ExprError();
1118 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001119 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1120 // A threadprivate directive for static class member variables must appear
1121 // in the class definition, in the same scope in which the member
1122 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001123 if (CanonicalVD->isStaticDataMember() &&
1124 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1125 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001126 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1127 bool IsDecl =
1128 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1129 Diag(VD->getLocation(),
1130 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1131 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001132 return ExprError();
1133 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001134 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1135 // A threadprivate directive for namespace-scope variables must appear
1136 // outside any definition or declaration other than the namespace
1137 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001138 if (CanonicalVD->getDeclContext()->isNamespace() &&
1139 (!getCurLexicalContext()->isFileContext() ||
1140 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1141 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001142 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1143 bool IsDecl =
1144 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1145 Diag(VD->getLocation(),
1146 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1147 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001148 return ExprError();
1149 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001150 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1151 // A threadprivate directive for static block-scope variables must appear
1152 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001153 if (CanonicalVD->isStaticLocal() && CurScope &&
1154 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001155 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001156 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1157 bool IsDecl =
1158 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1159 Diag(VD->getLocation(),
1160 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1161 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001162 return ExprError();
1163 }
1164
1165 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1166 // A threadprivate directive must lexically precede all references to any
1167 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001168 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001169 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001170 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 return ExprError();
1172 }
1173
1174 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001175 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1176 SourceLocation(), VD,
1177 /*RefersToEnclosingVariableOrCapture=*/false,
1178 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001179}
1180
Alexey Bataeved09d242014-05-28 05:53:51 +00001181Sema::DeclGroupPtrTy
1182Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1183 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001184 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001185 CurContext->addDecl(D);
1186 return DeclGroupPtrTy::make(DeclGroupRef(D));
1187 }
David Blaikie0403cb12016-01-15 23:43:25 +00001188 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001189}
1190
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001191namespace {
1192class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1193 Sema &SemaRef;
1194
1195public:
1196 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1197 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1198 if (VD->hasLocalStorage()) {
1199 SemaRef.Diag(E->getLocStart(),
1200 diag::err_omp_local_var_in_threadprivate_init)
1201 << E->getSourceRange();
1202 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1203 << VD << VD->getSourceRange();
1204 return true;
1205 }
1206 }
1207 return false;
1208 }
1209 bool VisitStmt(const Stmt *S) {
1210 for (auto Child : S->children()) {
1211 if (Child && Visit(Child))
1212 return true;
1213 }
1214 return false;
1215 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001216 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001217};
1218} // namespace
1219
Alexey Bataeved09d242014-05-28 05:53:51 +00001220OMPThreadPrivateDecl *
1221Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001222 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001223 for (auto &RefExpr : VarList) {
1224 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001225 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1226 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001227
Alexey Bataev376b4a42016-02-09 09:41:09 +00001228 // Mark variable as used.
1229 VD->setReferenced();
1230 VD->markUsed(Context);
1231
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001232 QualType QType = VD->getType();
1233 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1234 // It will be analyzed later.
1235 Vars.push_back(DE);
1236 continue;
1237 }
1238
Alexey Bataeva769e072013-03-22 06:34:35 +00001239 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1240 // A threadprivate variable must not have an incomplete type.
1241 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001242 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001243 continue;
1244 }
1245
1246 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1247 // A threadprivate variable must not have a reference type.
1248 if (VD->getType()->isReferenceType()) {
1249 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001250 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1251 bool IsDecl =
1252 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1253 Diag(VD->getLocation(),
1254 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1255 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001256 continue;
1257 }
1258
Samuel Antaof8b50122015-07-13 22:54:53 +00001259 // Check if this is a TLS variable. If TLS is not being supported, produce
1260 // the corresponding diagnostic.
1261 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1262 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1263 getLangOpts().OpenMPUseTLS &&
1264 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001265 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1266 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001267 Diag(ILoc, diag::err_omp_var_thread_local)
1268 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001269 bool IsDecl =
1270 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1271 Diag(VD->getLocation(),
1272 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1273 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001274 continue;
1275 }
1276
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001277 // Check if initial value of threadprivate variable reference variable with
1278 // local storage (it is not supported by runtime).
1279 if (auto Init = VD->getAnyInitializer()) {
1280 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001281 if (Checker.Visit(Init))
1282 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001283 }
1284
Alexey Bataeved09d242014-05-28 05:53:51 +00001285 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001286 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001287 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1288 Context, SourceRange(Loc, Loc)));
1289 if (auto *ML = Context.getASTMutationListener())
1290 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001291 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001292 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001293 if (!Vars.empty()) {
1294 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1295 Vars);
1296 D->setAccess(AS_public);
1297 }
1298 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001299}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001300
Alexey Bataev7ff55242014-06-19 09:13:45 +00001301static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001302 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001303 bool IsLoopIterVar = false) {
1304 if (DVar.RefExpr) {
1305 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1306 << getOpenMPClauseName(DVar.CKind);
1307 return;
1308 }
1309 enum {
1310 PDSA_StaticMemberShared,
1311 PDSA_StaticLocalVarShared,
1312 PDSA_LoopIterVarPrivate,
1313 PDSA_LoopIterVarLinear,
1314 PDSA_LoopIterVarLastprivate,
1315 PDSA_ConstVarShared,
1316 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001317 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001318 PDSA_LocalVarPrivate,
1319 PDSA_Implicit
1320 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001321 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001322 auto ReportLoc = D->getLocation();
1323 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001324 if (IsLoopIterVar) {
1325 if (DVar.CKind == OMPC_private)
1326 Reason = PDSA_LoopIterVarPrivate;
1327 else if (DVar.CKind == OMPC_lastprivate)
1328 Reason = PDSA_LoopIterVarLastprivate;
1329 else
1330 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001331 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1332 Reason = PDSA_TaskVarFirstprivate;
1333 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001334 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001335 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001336 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001337 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001338 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001339 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001340 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001341 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001342 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001343 ReportHint = true;
1344 Reason = PDSA_LocalVarPrivate;
1345 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001346 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001347 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001348 << Reason << ReportHint
1349 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1350 } else if (DVar.ImplicitDSALoc.isValid()) {
1351 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1352 << getOpenMPClauseName(DVar.CKind);
1353 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001354}
1355
Alexey Bataev758e55e2013-09-06 18:03:48 +00001356namespace {
1357class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1358 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001359 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360 bool ErrorFound;
1361 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001362 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001363 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001364
Alexey Bataev758e55e2013-09-06 18:03:48 +00001365public:
1366 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001367 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001368 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001369 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1370 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001371
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001372 auto DVar = Stack->getTopDSA(VD, false);
1373 // Check if the variable has explicit DSA set and stop analysis if it so.
1374 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001375
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001376 auto ELoc = E->getExprLoc();
1377 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001378 // The default(none) clause requires that each variable that is referenced
1379 // in the construct, and does not have a predetermined data-sharing
1380 // attribute, must have its data-sharing attribute explicitly determined
1381 // by being listed in a data-sharing attribute clause.
1382 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001383 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001384 VarsWithInheritedDSA.count(VD) == 0) {
1385 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001386 return;
1387 }
1388
1389 // OpenMP [2.9.3.6, Restrictions, p.2]
1390 // A list item that appears in a reduction clause of the innermost
1391 // enclosing worksharing or parallel construct may not be accessed in an
1392 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001393 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001394 [](OpenMPDirectiveKind K) -> bool {
1395 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001396 isOpenMPWorksharingDirective(K) ||
1397 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001398 },
1399 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001400 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1401 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001402 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1403 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001404 return;
1405 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001406
1407 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001408 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001409 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001410 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001411 }
1412 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001413 void VisitMemberExpr(MemberExpr *E) {
1414 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1415 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1416 auto DVar = Stack->getTopDSA(FD, false);
1417 // Check if the variable has explicit DSA set and stop analysis if it
1418 // so.
1419 if (DVar.RefExpr)
1420 return;
1421
1422 auto ELoc = E->getExprLoc();
1423 auto DKind = Stack->getCurrentDirective();
1424 // OpenMP [2.9.3.6, Restrictions, p.2]
1425 // A list item that appears in a reduction clause of the innermost
1426 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001427 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001428 DVar =
1429 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1430 [](OpenMPDirectiveKind K) -> bool {
1431 return isOpenMPParallelDirective(K) ||
1432 isOpenMPWorksharingDirective(K) ||
1433 isOpenMPTeamsDirective(K);
1434 },
1435 false);
1436 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1437 ErrorFound = true;
1438 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1439 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1440 return;
1441 }
1442
1443 // Define implicit data-sharing attributes for task.
1444 DVar = Stack->getImplicitDSA(FD, false);
1445 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1446 ImplicitFirstprivate.push_back(E);
1447 }
1448 }
1449 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001450 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001451 for (auto *C : S->clauses()) {
1452 // Skip analysis of arguments of implicitly defined firstprivate clause
1453 // for task directives.
1454 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1455 for (auto *CC : C->children()) {
1456 if (CC)
1457 Visit(CC);
1458 }
1459 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001460 }
1461 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001462 for (auto *C : S->children()) {
1463 if (C && !isa<OMPExecutableDirective>(C))
1464 Visit(C);
1465 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001466 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001467
1468 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001469 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001470 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001471 return VarsWithInheritedDSA;
1472 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001473
Alexey Bataev7ff55242014-06-19 09:13:45 +00001474 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1475 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476};
Alexey Bataeved09d242014-05-28 05:53:51 +00001477} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001478
Alexey Bataevbae9a792014-06-27 10:37:06 +00001479void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001480 switch (DKind) {
1481 case OMPD_parallel: {
1482 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001483 QualType KmpInt32PtrTy =
1484 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001485 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001486 std::make_pair(".global_tid.", KmpInt32PtrTy),
1487 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1488 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001489 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001490 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1491 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001492 break;
1493 }
1494 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001495 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001496 std::make_pair(StringRef(), QualType()) // __context with shared vars
1497 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001498 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1499 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001500 break;
1501 }
1502 case OMPD_for: {
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
Alexey Bataev9959db52014-05-06 10:08:46 +00001505 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1507 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001508 break;
1509 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001510 case OMPD_for_simd: {
1511 Sema::CapturedParamNameType Params[] = {
1512 std::make_pair(StringRef(), QualType()) // __context with shared vars
1513 };
1514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
1516 break;
1517 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001518 case OMPD_sections: {
1519 Sema::CapturedParamNameType Params[] = {
1520 std::make_pair(StringRef(), QualType()) // __context with shared vars
1521 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1523 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001524 break;
1525 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001526 case OMPD_section: {
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 Bataev1e0498a2014-06-26 08:21:58 +00001532 break;
1533 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001534 case OMPD_single: {
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 Bataevd1e40fb2014-06-26 12:05:45 +00001540 break;
1541 }
Alexander Musman80c22892014-07-17 08:54:58 +00001542 case OMPD_master: {
1543 Sema::CapturedParamNameType Params[] = {
1544 std::make_pair(StringRef(), QualType()) // __context with shared vars
1545 };
1546 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1547 Params);
1548 break;
1549 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001550 case OMPD_critical: {
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 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001558 case OMPD_parallel_for: {
1559 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001560 QualType KmpInt32PtrTy =
1561 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001562 Sema::CapturedParamNameType Params[] = {
1563 std::make_pair(".global_tid.", KmpInt32PtrTy),
1564 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1565 std::make_pair(StringRef(), QualType()) // __context with shared vars
1566 };
1567 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1568 Params);
1569 break;
1570 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001571 case OMPD_parallel_for_simd: {
1572 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001573 QualType KmpInt32PtrTy =
1574 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001575 Sema::CapturedParamNameType Params[] = {
1576 std::make_pair(".global_tid.", KmpInt32PtrTy),
1577 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1578 std::make_pair(StringRef(), QualType()) // __context with shared vars
1579 };
1580 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1581 Params);
1582 break;
1583 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001584 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001585 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001586 QualType KmpInt32PtrTy =
1587 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001588 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001589 std::make_pair(".global_tid.", KmpInt32PtrTy),
1590 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001591 std::make_pair(StringRef(), QualType()) // __context with shared vars
1592 };
1593 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1594 Params);
1595 break;
1596 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001597 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001598 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001599 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1600 FunctionProtoType::ExtProtoInfo EPI;
1601 EPI.Variadic = true;
1602 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001603 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001604 std::make_pair(".global_tid.", KmpInt32Ty),
1605 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001606 std::make_pair(".privates.",
1607 Context.VoidPtrTy.withConst().withRestrict()),
1608 std::make_pair(
1609 ".copy_fn.",
1610 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001611 std::make_pair(StringRef(), QualType()) // __context with shared vars
1612 };
1613 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1614 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001615 // Mark this captured region as inlined, because we don't use outlined
1616 // function directly.
1617 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1618 AlwaysInlineAttr::CreateImplicit(
1619 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001620 break;
1621 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001622 case OMPD_ordered: {
1623 Sema::CapturedParamNameType Params[] = {
1624 std::make_pair(StringRef(), QualType()) // __context with shared vars
1625 };
1626 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1627 Params);
1628 break;
1629 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001630 case OMPD_atomic: {
1631 Sema::CapturedParamNameType Params[] = {
1632 std::make_pair(StringRef(), QualType()) // __context with shared vars
1633 };
1634 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1635 Params);
1636 break;
1637 }
Michael Wong65f367f2015-07-21 13:44:28 +00001638 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001639 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001640 case OMPD_target_parallel:
1641 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001642 Sema::CapturedParamNameType Params[] = {
1643 std::make_pair(StringRef(), QualType()) // __context with shared vars
1644 };
1645 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1646 Params);
1647 break;
1648 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001649 case OMPD_teams: {
1650 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001651 QualType KmpInt32PtrTy =
1652 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001653 Sema::CapturedParamNameType Params[] = {
1654 std::make_pair(".global_tid.", KmpInt32PtrTy),
1655 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1656 std::make_pair(StringRef(), QualType()) // __context with shared vars
1657 };
1658 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1659 Params);
1660 break;
1661 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001662 case OMPD_taskgroup: {
1663 Sema::CapturedParamNameType Params[] = {
1664 std::make_pair(StringRef(), QualType()) // __context with shared vars
1665 };
1666 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1667 Params);
1668 break;
1669 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001670 case OMPD_taskloop: {
1671 Sema::CapturedParamNameType Params[] = {
1672 std::make_pair(StringRef(), QualType()) // __context with shared vars
1673 };
1674 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1675 Params);
1676 break;
1677 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001678 case OMPD_taskloop_simd: {
1679 Sema::CapturedParamNameType Params[] = {
1680 std::make_pair(StringRef(), QualType()) // __context with shared vars
1681 };
1682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1683 Params);
1684 break;
1685 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001686 case OMPD_distribute: {
1687 Sema::CapturedParamNameType Params[] = {
1688 std::make_pair(StringRef(), QualType()) // __context with shared vars
1689 };
1690 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1691 Params);
1692 break;
1693 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001694 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001695 case OMPD_taskyield:
1696 case OMPD_barrier:
1697 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001698 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001699 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001700 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001701 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001702 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001703 case OMPD_declare_reduction:
Alexey Bataev9959db52014-05-06 10:08:46 +00001704 llvm_unreachable("OpenMP Directive is not allowed");
1705 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001706 llvm_unreachable("Unknown OpenMP directive");
1707 }
1708}
1709
Alexey Bataev3392d762016-02-16 11:18:12 +00001710static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001711 Expr *CaptureExpr, bool WithInit,
1712 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001713 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001714 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001715 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001716 QualType Ty = Init->getType();
1717 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1718 if (S.getLangOpts().CPlusPlus)
1719 Ty = C.getLValueReferenceType(Ty);
1720 else {
1721 Ty = C.getPointerType(Ty);
1722 ExprResult Res =
1723 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1724 if (!Res.isUsable())
1725 return nullptr;
1726 Init = Res.get();
1727 }
Alexey Bataev61205072016-03-02 04:57:40 +00001728 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001729 }
1730 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001731 if (!WithInit)
1732 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001733 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001734 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1735 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001736 return CED;
1737}
1738
Alexey Bataev61205072016-03-02 04:57:40 +00001739static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1740 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001741 OMPCapturedExprDecl *CD;
1742 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1743 CD = cast<OMPCapturedExprDecl>(VD);
1744 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001745 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1746 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001747 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001748 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001749}
1750
Alexey Bataev5a3af132016-03-29 08:58:54 +00001751static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1752 if (!Ref) {
1753 auto *CD =
1754 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1755 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1756 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1757 CaptureExpr->getExprLoc());
1758 }
1759 ExprResult Res = Ref;
1760 if (!S.getLangOpts().CPlusPlus &&
1761 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1762 Ref->getType()->isPointerType())
1763 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1764 if (!Res.isUsable())
1765 return ExprError();
1766 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001767}
1768
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001769StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1770 ArrayRef<OMPClause *> Clauses) {
1771 if (!S.isUsable()) {
1772 ActOnCapturedRegionError();
1773 return StmtError();
1774 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001775
1776 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001777 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001778 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001779 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001780 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001781 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001782 Clause->getClauseKind() == OMPC_copyprivate ||
1783 (getLangOpts().OpenMPUseTLS &&
1784 getASTContext().getTargetInfo().isTLSSupported() &&
1785 Clause->getClauseKind() == OMPC_copyin)) {
1786 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001787 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001788 for (auto *VarRef : Clause->children()) {
1789 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001790 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001791 }
1792 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001793 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001794 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001795 // Mark all variables in private list clauses as used in inner region.
1796 // Required for proper codegen of combined directives.
1797 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001798 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001799 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1800 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001801 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1802 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001803 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001804 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1805 if (auto *E = C->getPostUpdateExpr())
1806 MarkDeclarationsReferencedInExpr(E);
1807 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001808 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001809 if (Clause->getClauseKind() == OMPC_schedule)
1810 SC = cast<OMPScheduleClause>(Clause);
1811 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001812 OC = cast<OMPOrderedClause>(Clause);
1813 else if (Clause->getClauseKind() == OMPC_linear)
1814 LCs.push_back(cast<OMPLinearClause>(Clause));
1815 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001816 bool ErrorFound = false;
1817 // OpenMP, 2.7.1 Loop Construct, Restrictions
1818 // The nonmonotonic modifier cannot be specified if an ordered clause is
1819 // specified.
1820 if (SC &&
1821 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1822 SC->getSecondScheduleModifier() ==
1823 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1824 OC) {
1825 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1826 ? SC->getFirstScheduleModifierLoc()
1827 : SC->getSecondScheduleModifierLoc(),
1828 diag::err_omp_schedule_nonmonotonic_ordered)
1829 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1830 ErrorFound = true;
1831 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001832 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1833 for (auto *C : LCs) {
1834 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1835 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1836 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001837 ErrorFound = true;
1838 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001839 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1840 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1841 OC->getNumForLoops()) {
1842 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1843 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1844 ErrorFound = true;
1845 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001846 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001847 ActOnCapturedRegionError();
1848 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001849 }
1850 return ActOnCapturedRegionEnd(S.get());
1851}
1852
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001853static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1854 OpenMPDirectiveKind CurrentRegion,
1855 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001856 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001857 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001858 // Allowed nesting of constructs
1859 // +------------------+-----------------+------------------------------------+
1860 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1861 // +------------------+-----------------+------------------------------------+
1862 // | parallel | parallel | * |
1863 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001864 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001865 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001866 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001867 // | parallel | simd | * |
1868 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001869 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001870 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001871 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001872 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001873 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001874 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001875 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001876 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001877 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001878 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001879 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001880 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001881 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001882 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001883 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001884 // | parallel | target parallel | * |
1885 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001886 // | parallel | target enter | * |
1887 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001888 // | parallel | target exit | * |
1889 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001890 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001891 // | parallel | cancellation | |
1892 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001893 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001894 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001895 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001896 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001897 // +------------------+-----------------+------------------------------------+
1898 // | for | parallel | * |
1899 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001900 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001901 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001902 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001903 // | for | simd | * |
1904 // | for | sections | + |
1905 // | for | section | + |
1906 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001907 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001908 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001909 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001910 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001911 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001912 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001913 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001914 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001915 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001916 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001917 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001918 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001919 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001920 // | for | target parallel | * |
1921 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001922 // | for | target enter | * |
1923 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001924 // | for | target exit | * |
1925 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001926 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001927 // | for | cancellation | |
1928 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001929 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001930 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001931 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001932 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001933 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001934 // | master | parallel | * |
1935 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001936 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001937 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001938 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001939 // | master | simd | * |
1940 // | master | sections | + |
1941 // | master | section | + |
1942 // | master | single | + |
1943 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001944 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001945 // | master |parallel sections| * |
1946 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001947 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001948 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001949 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001950 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001951 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001952 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001953 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001954 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001955 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001956 // | master | target parallel | * |
1957 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001958 // | master | target enter | * |
1959 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001960 // | master | target exit | * |
1961 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001962 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001963 // | master | cancellation | |
1964 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001965 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001966 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001967 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001968 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001969 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001970 // | critical | parallel | * |
1971 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001972 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001973 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001974 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001975 // | critical | simd | * |
1976 // | critical | sections | + |
1977 // | critical | section | + |
1978 // | critical | single | + |
1979 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001980 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001981 // | critical |parallel sections| * |
1982 // | critical | task | * |
1983 // | critical | taskyield | * |
1984 // | critical | barrier | + |
1985 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001986 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001987 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001988 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001989 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001990 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001991 // | critical | target parallel | * |
1992 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001993 // | critical | target enter | * |
1994 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001995 // | critical | target exit | * |
1996 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001997 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001998 // | critical | cancellation | |
1999 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002000 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002001 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002002 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002003 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002004 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002005 // | simd | parallel | |
2006 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002007 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002008 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002009 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002010 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002011 // | simd | sections | |
2012 // | simd | section | |
2013 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002014 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002015 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002016 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002017 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002018 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002019 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002020 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002021 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002022 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002023 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002024 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002025 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002026 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002027 // | simd | target parallel | |
2028 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002029 // | simd | target enter | |
2030 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002031 // | simd | target exit | |
2032 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002033 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002034 // | simd | cancellation | |
2035 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002036 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002037 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002038 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002039 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002040 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002041 // | for simd | parallel | |
2042 // | for simd | for | |
2043 // | for simd | for simd | |
2044 // | for simd | master | |
2045 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002046 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002047 // | for simd | sections | |
2048 // | for simd | section | |
2049 // | for simd | single | |
2050 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002051 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002052 // | for simd |parallel sections| |
2053 // | for simd | task | |
2054 // | for simd | taskyield | |
2055 // | for simd | barrier | |
2056 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002057 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002058 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002059 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002060 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002061 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002062 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002063 // | for simd | target parallel | |
2064 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002065 // | for simd | target enter | |
2066 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002067 // | for simd | target exit | |
2068 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002069 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002070 // | for simd | cancellation | |
2071 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002072 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002073 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002074 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002075 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002076 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002077 // | parallel for simd| parallel | |
2078 // | parallel for simd| for | |
2079 // | parallel for simd| for simd | |
2080 // | parallel for simd| master | |
2081 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002082 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002083 // | parallel for simd| sections | |
2084 // | parallel for simd| section | |
2085 // | parallel for simd| single | |
2086 // | parallel for simd| parallel for | |
2087 // | parallel for simd|parallel for simd| |
2088 // | parallel for simd|parallel sections| |
2089 // | parallel for simd| task | |
2090 // | parallel for simd| taskyield | |
2091 // | parallel for simd| barrier | |
2092 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002093 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002094 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002095 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002096 // | parallel for simd| atomic | |
2097 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002098 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002099 // | parallel for simd| target parallel | |
2100 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002101 // | parallel for simd| target enter | |
2102 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002103 // | parallel for simd| target exit | |
2104 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002105 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002106 // | parallel for simd| cancellation | |
2107 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002108 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002109 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002110 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002111 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002112 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002113 // | sections | parallel | * |
2114 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002115 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002116 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002117 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002118 // | sections | simd | * |
2119 // | sections | sections | + |
2120 // | sections | section | * |
2121 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002122 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002123 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002124 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002125 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002126 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002127 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002128 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002129 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002130 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002131 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002132 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002133 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002134 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002135 // | sections | target parallel | * |
2136 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002137 // | sections | target enter | * |
2138 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002139 // | sections | target exit | * |
2140 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002141 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002142 // | sections | cancellation | |
2143 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002144 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002145 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002146 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002147 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002148 // +------------------+-----------------+------------------------------------+
2149 // | section | parallel | * |
2150 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002151 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002152 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002153 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002154 // | section | simd | * |
2155 // | section | sections | + |
2156 // | section | section | + |
2157 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002158 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002159 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002160 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002161 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002162 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002163 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002164 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002165 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002166 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002167 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002168 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002169 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002170 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002171 // | section | target parallel | * |
2172 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002173 // | section | target enter | * |
2174 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002175 // | section | target exit | * |
2176 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002177 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002178 // | section | cancellation | |
2179 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002180 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002181 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002182 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002183 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002184 // +------------------+-----------------+------------------------------------+
2185 // | single | parallel | * |
2186 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002187 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002188 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002189 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002190 // | single | simd | * |
2191 // | single | sections | + |
2192 // | single | section | + |
2193 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002194 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002195 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002196 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002197 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002198 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002199 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002200 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002201 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002202 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002203 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002204 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002205 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002206 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002207 // | single | target parallel | * |
2208 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002209 // | single | target enter | * |
2210 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002211 // | single | target exit | * |
2212 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002213 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002214 // | single | cancellation | |
2215 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002216 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002217 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002218 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002219 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002220 // +------------------+-----------------+------------------------------------+
2221 // | parallel for | parallel | * |
2222 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002223 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002224 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002225 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002226 // | parallel for | simd | * |
2227 // | parallel for | sections | + |
2228 // | parallel for | section | + |
2229 // | parallel for | single | + |
2230 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002231 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002232 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002233 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002234 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002235 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002236 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002237 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002238 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002239 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002240 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002241 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002242 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002243 // | parallel for | target parallel | * |
2244 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002245 // | parallel for | target enter | * |
2246 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002247 // | parallel for | target exit | * |
2248 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002249 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002250 // | parallel for | cancellation | |
2251 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002252 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002253 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002254 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002255 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002256 // +------------------+-----------------+------------------------------------+
2257 // | parallel sections| parallel | * |
2258 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002259 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002260 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002261 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002262 // | parallel sections| simd | * |
2263 // | parallel sections| sections | + |
2264 // | parallel sections| section | * |
2265 // | parallel sections| single | + |
2266 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002267 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002268 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002269 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002270 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002271 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002272 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002273 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002274 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002275 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002276 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002277 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002278 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002279 // | parallel sections| target parallel | * |
2280 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002281 // | parallel sections| target enter | * |
2282 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002283 // | parallel sections| target exit | * |
2284 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002285 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002286 // | parallel sections| cancellation | |
2287 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002288 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002289 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002290 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002291 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002292 // +------------------+-----------------+------------------------------------+
2293 // | task | parallel | * |
2294 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002295 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002296 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002297 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002298 // | task | simd | * |
2299 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002300 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002301 // | task | single | + |
2302 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002303 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002304 // | task |parallel sections| * |
2305 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002306 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002307 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002308 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002309 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002310 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002311 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002312 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002313 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002314 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002315 // | task | target parallel | * |
2316 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002317 // | task | target enter | * |
2318 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002319 // | task | target exit | * |
2320 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002321 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002322 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002323 // | | point | ! |
2324 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002325 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002326 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002327 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002328 // +------------------+-----------------+------------------------------------+
2329 // | ordered | parallel | * |
2330 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002331 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002332 // | ordered | master | * |
2333 // | ordered | critical | * |
2334 // | ordered | simd | * |
2335 // | ordered | sections | + |
2336 // | ordered | section | + |
2337 // | ordered | single | + |
2338 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002339 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002340 // | ordered |parallel sections| * |
2341 // | ordered | task | * |
2342 // | ordered | taskyield | * |
2343 // | ordered | barrier | + |
2344 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002345 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002346 // | ordered | flush | * |
2347 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002348 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002349 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002350 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002351 // | ordered | target parallel | * |
2352 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002353 // | ordered | target enter | * |
2354 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002355 // | ordered | target exit | * |
2356 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002357 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002358 // | ordered | cancellation | |
2359 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002360 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002361 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002362 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002363 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002364 // +------------------+-----------------+------------------------------------+
2365 // | atomic | parallel | |
2366 // | atomic | for | |
2367 // | atomic | for simd | |
2368 // | atomic | master | |
2369 // | atomic | critical | |
2370 // | atomic | simd | |
2371 // | atomic | sections | |
2372 // | atomic | section | |
2373 // | atomic | single | |
2374 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002375 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002376 // | atomic |parallel sections| |
2377 // | atomic | task | |
2378 // | atomic | taskyield | |
2379 // | atomic | barrier | |
2380 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002381 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002382 // | atomic | flush | |
2383 // | atomic | ordered | |
2384 // | atomic | atomic | |
2385 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002386 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002387 // | atomic | target parallel | |
2388 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002389 // | atomic | target enter | |
2390 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002391 // | atomic | target exit | |
2392 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002393 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002394 // | atomic | cancellation | |
2395 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002396 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002397 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002398 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002399 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002400 // +------------------+-----------------+------------------------------------+
2401 // | target | parallel | * |
2402 // | target | for | * |
2403 // | target | for simd | * |
2404 // | target | master | * |
2405 // | target | critical | * |
2406 // | target | simd | * |
2407 // | target | sections | * |
2408 // | target | section | * |
2409 // | target | single | * |
2410 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002411 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002412 // | target |parallel sections| * |
2413 // | target | task | * |
2414 // | target | taskyield | * |
2415 // | target | barrier | * |
2416 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002417 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002418 // | target | flush | * |
2419 // | target | ordered | * |
2420 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002421 // | target | target | |
2422 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002423 // | target | target parallel | |
2424 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002425 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002426 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002427 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002428 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002429 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002430 // | target | cancellation | |
2431 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002432 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002433 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002434 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002435 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002436 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002437 // | target parallel | parallel | * |
2438 // | target parallel | for | * |
2439 // | target parallel | for simd | * |
2440 // | target parallel | master | * |
2441 // | target parallel | critical | * |
2442 // | target parallel | simd | * |
2443 // | target parallel | sections | * |
2444 // | target parallel | section | * |
2445 // | target parallel | single | * |
2446 // | target parallel | parallel for | * |
2447 // | target parallel |parallel for simd| * |
2448 // | target parallel |parallel sections| * |
2449 // | target parallel | task | * |
2450 // | target parallel | taskyield | * |
2451 // | target parallel | barrier | * |
2452 // | target parallel | taskwait | * |
2453 // | target parallel | taskgroup | * |
2454 // | target parallel | flush | * |
2455 // | target parallel | ordered | * |
2456 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002457 // | target parallel | target | |
2458 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002459 // | target parallel | target parallel | |
2460 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002461 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002462 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002463 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002464 // | | data | |
2465 // | target parallel | teams | |
2466 // | target parallel | cancellation | |
2467 // | | point | ! |
2468 // | target parallel | cancel | ! |
2469 // | target parallel | taskloop | * |
2470 // | target parallel | taskloop simd | * |
2471 // | target parallel | distribute | |
2472 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002473 // | target parallel | parallel | * |
2474 // | for | | |
2475 // | target parallel | for | * |
2476 // | for | | |
2477 // | target parallel | for simd | * |
2478 // | for | | |
2479 // | target parallel | master | * |
2480 // | for | | |
2481 // | target parallel | critical | * |
2482 // | for | | |
2483 // | target parallel | simd | * |
2484 // | for | | |
2485 // | target parallel | sections | * |
2486 // | for | | |
2487 // | target parallel | section | * |
2488 // | for | | |
2489 // | target parallel | single | * |
2490 // | for | | |
2491 // | target parallel | parallel for | * |
2492 // | for | | |
2493 // | target parallel |parallel for simd| * |
2494 // | for | | |
2495 // | target parallel |parallel sections| * |
2496 // | for | | |
2497 // | target parallel | task | * |
2498 // | for | | |
2499 // | target parallel | taskyield | * |
2500 // | for | | |
2501 // | target parallel | barrier | * |
2502 // | for | | |
2503 // | target parallel | taskwait | * |
2504 // | for | | |
2505 // | target parallel | taskgroup | * |
2506 // | for | | |
2507 // | target parallel | flush | * |
2508 // | for | | |
2509 // | target parallel | ordered | * |
2510 // | for | | |
2511 // | target parallel | atomic | * |
2512 // | for | | |
2513 // | target parallel | target | |
2514 // | for | | |
2515 // | target parallel | target parallel | |
2516 // | for | | |
2517 // | target parallel | target parallel | |
2518 // | for | for | |
2519 // | target parallel | target enter | |
2520 // | for | data | |
2521 // | target parallel | target exit | |
2522 // | for | data | |
2523 // | target parallel | teams | |
2524 // | for | | |
2525 // | target parallel | cancellation | |
2526 // | for | point | ! |
2527 // | target parallel | cancel | ! |
2528 // | for | | |
2529 // | target parallel | taskloop | * |
2530 // | for | | |
2531 // | target parallel | taskloop simd | * |
2532 // | for | | |
2533 // | target parallel | distribute | |
2534 // | for | | |
2535 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002536 // | teams | parallel | * |
2537 // | teams | for | + |
2538 // | teams | for simd | + |
2539 // | teams | master | + |
2540 // | teams | critical | + |
2541 // | teams | simd | + |
2542 // | teams | sections | + |
2543 // | teams | section | + |
2544 // | teams | single | + |
2545 // | teams | parallel for | * |
2546 // | teams |parallel for simd| * |
2547 // | teams |parallel sections| * |
2548 // | teams | task | + |
2549 // | teams | taskyield | + |
2550 // | teams | barrier | + |
2551 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002552 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002553 // | teams | flush | + |
2554 // | teams | ordered | + |
2555 // | teams | atomic | + |
2556 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002557 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002558 // | teams | target parallel | + |
2559 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002560 // | teams | target enter | + |
2561 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002562 // | teams | target exit | + |
2563 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002564 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002565 // | teams | cancellation | |
2566 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002567 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002568 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002569 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002570 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002571 // +------------------+-----------------+------------------------------------+
2572 // | taskloop | parallel | * |
2573 // | taskloop | for | + |
2574 // | taskloop | for simd | + |
2575 // | taskloop | master | + |
2576 // | taskloop | critical | * |
2577 // | taskloop | simd | * |
2578 // | taskloop | sections | + |
2579 // | taskloop | section | + |
2580 // | taskloop | single | + |
2581 // | taskloop | parallel for | * |
2582 // | taskloop |parallel for simd| * |
2583 // | taskloop |parallel sections| * |
2584 // | taskloop | task | * |
2585 // | taskloop | taskyield | * |
2586 // | taskloop | barrier | + |
2587 // | taskloop | taskwait | * |
2588 // | taskloop | taskgroup | * |
2589 // | taskloop | flush | * |
2590 // | taskloop | ordered | + |
2591 // | taskloop | atomic | * |
2592 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002593 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002594 // | taskloop | target parallel | * |
2595 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002596 // | taskloop | target enter | * |
2597 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002598 // | taskloop | target exit | * |
2599 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002600 // | taskloop | teams | + |
2601 // | taskloop | cancellation | |
2602 // | | point | |
2603 // | taskloop | cancel | |
2604 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002605 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002606 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002607 // | taskloop simd | parallel | |
2608 // | taskloop simd | for | |
2609 // | taskloop simd | for simd | |
2610 // | taskloop simd | master | |
2611 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002612 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002613 // | taskloop simd | sections | |
2614 // | taskloop simd | section | |
2615 // | taskloop simd | single | |
2616 // | taskloop simd | parallel for | |
2617 // | taskloop simd |parallel for simd| |
2618 // | taskloop simd |parallel sections| |
2619 // | taskloop simd | task | |
2620 // | taskloop simd | taskyield | |
2621 // | taskloop simd | barrier | |
2622 // | taskloop simd | taskwait | |
2623 // | taskloop simd | taskgroup | |
2624 // | taskloop simd | flush | |
2625 // | taskloop simd | ordered | + (with simd clause) |
2626 // | taskloop simd | atomic | |
2627 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002628 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002629 // | taskloop simd | target parallel | |
2630 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002631 // | taskloop simd | target enter | |
2632 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002633 // | taskloop simd | target exit | |
2634 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002635 // | taskloop simd | teams | |
2636 // | taskloop simd | cancellation | |
2637 // | | point | |
2638 // | taskloop simd | cancel | |
2639 // | taskloop simd | taskloop | |
2640 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002641 // | taskloop simd | distribute | |
2642 // +------------------+-----------------+------------------------------------+
2643 // | distribute | parallel | * |
2644 // | distribute | for | * |
2645 // | distribute | for simd | * |
2646 // | distribute | master | * |
2647 // | distribute | critical | * |
2648 // | distribute | simd | * |
2649 // | distribute | sections | * |
2650 // | distribute | section | * |
2651 // | distribute | single | * |
2652 // | distribute | parallel for | * |
2653 // | distribute |parallel for simd| * |
2654 // | distribute |parallel sections| * |
2655 // | distribute | task | * |
2656 // | distribute | taskyield | * |
2657 // | distribute | barrier | * |
2658 // | distribute | taskwait | * |
2659 // | distribute | taskgroup | * |
2660 // | distribute | flush | * |
2661 // | distribute | ordered | + |
2662 // | distribute | atomic | * |
2663 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002664 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002665 // | distribute | target parallel | |
2666 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002667 // | distribute | target enter | |
2668 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002669 // | distribute | target exit | |
2670 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002671 // | distribute | teams | |
2672 // | distribute | cancellation | + |
2673 // | | point | |
2674 // | distribute | cancel | + |
2675 // | distribute | taskloop | * |
2676 // | distribute | taskloop simd | * |
2677 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002678 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002679 if (Stack->getCurScope()) {
2680 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002681 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002682 bool NestingProhibited = false;
2683 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002684 enum {
2685 NoRecommend,
2686 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002687 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002688 ShouldBeInTargetRegion,
2689 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002690 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002691 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2692 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002693 // OpenMP [2.16, Nesting of Regions]
2694 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002695 // OpenMP [2.8.1,simd Construct, Restrictions]
2696 // An ordered construct with the simd clause is the only OpenMP construct
2697 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002698 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2699 return true;
2700 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002701 if (ParentRegion == OMPD_atomic) {
2702 // OpenMP [2.16, Nesting of Regions]
2703 // OpenMP constructs may not be nested inside an atomic region.
2704 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2705 return true;
2706 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002707 if (CurrentRegion == OMPD_section) {
2708 // OpenMP [2.7.2, sections Construct, Restrictions]
2709 // Orphaned section directives are prohibited. That is, the section
2710 // directives must appear within the sections construct and must not be
2711 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002712 if (ParentRegion != OMPD_sections &&
2713 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002714 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2715 << (ParentRegion != OMPD_unknown)
2716 << getOpenMPDirectiveName(ParentRegion);
2717 return true;
2718 }
2719 return false;
2720 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002721 // Allow some constructs to be orphaned (they could be used in functions,
2722 // called from OpenMP regions with the required preconditions).
2723 if (ParentRegion == OMPD_unknown)
2724 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002725 if (CurrentRegion == OMPD_cancellation_point ||
2726 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002727 // OpenMP [2.16, Nesting of Regions]
2728 // A cancellation point construct for which construct-type-clause is
2729 // taskgroup must be nested inside a task construct. A cancellation
2730 // point construct for which construct-type-clause is not taskgroup must
2731 // be closely nested inside an OpenMP construct that matches the type
2732 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002733 // A cancel construct for which construct-type-clause is taskgroup must be
2734 // nested inside a task construct. A cancel construct for which
2735 // construct-type-clause is not taskgroup must be closely nested inside an
2736 // OpenMP construct that matches the type specified in
2737 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002738 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002739 !((CancelRegion == OMPD_parallel &&
2740 (ParentRegion == OMPD_parallel ||
2741 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002742 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002743 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2744 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002745 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2746 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002747 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2748 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002749 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002750 // OpenMP [2.16, Nesting of Regions]
2751 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002752 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002753 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002754 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002755 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002756 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2757 // OpenMP [2.16, Nesting of Regions]
2758 // A critical region may not be nested (closely or otherwise) inside a
2759 // critical region with the same name. Note that this restriction is not
2760 // sufficient to prevent deadlock.
2761 SourceLocation PreviousCriticalLoc;
2762 bool DeadLock =
2763 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2764 OpenMPDirectiveKind K,
2765 const DeclarationNameInfo &DNI,
2766 SourceLocation Loc)
2767 ->bool {
2768 if (K == OMPD_critical &&
2769 DNI.getName() == CurrentName.getName()) {
2770 PreviousCriticalLoc = Loc;
2771 return true;
2772 } else
2773 return false;
2774 },
2775 false /* skip top directive */);
2776 if (DeadLock) {
2777 SemaRef.Diag(StartLoc,
2778 diag::err_omp_prohibited_region_critical_same_name)
2779 << CurrentName.getName();
2780 if (PreviousCriticalLoc.isValid())
2781 SemaRef.Diag(PreviousCriticalLoc,
2782 diag::note_omp_previous_critical_region);
2783 return true;
2784 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002785 } else if (CurrentRegion == OMPD_barrier) {
2786 // OpenMP [2.16, Nesting of Regions]
2787 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002788 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002789 NestingProhibited =
2790 isOpenMPWorksharingDirective(ParentRegion) ||
2791 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002792 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002793 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002794 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002795 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002796 // OpenMP [2.16, Nesting of Regions]
2797 // A worksharing region may not be closely nested inside a worksharing,
2798 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002799 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002800 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002801 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002802 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002803 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002804 Recommend = ShouldBeInParallelRegion;
2805 } else if (CurrentRegion == OMPD_ordered) {
2806 // OpenMP [2.16, Nesting of Regions]
2807 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002808 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002809 // An ordered region must be closely nested inside a loop region (or
2810 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002811 // OpenMP [2.8.1,simd Construct, Restrictions]
2812 // An ordered construct with the simd clause is the only OpenMP construct
2813 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002814 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002815 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002816 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002817 !(isOpenMPSimdDirective(ParentRegion) ||
2818 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002819 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002820 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2821 // OpenMP [2.16, Nesting of Regions]
2822 // If specified, a teams construct must be contained within a target
2823 // construct.
2824 NestingProhibited = ParentRegion != OMPD_target;
2825 Recommend = ShouldBeInTargetRegion;
2826 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2827 }
2828 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2829 // OpenMP [2.16, Nesting of Regions]
2830 // distribute, parallel, parallel sections, parallel workshare, and the
2831 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2832 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002833 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2834 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002835 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002836 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002837 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2838 // OpenMP 4.5 [2.17 Nesting of Regions]
2839 // The region associated with the distribute construct must be strictly
2840 // nested inside a teams region
2841 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2842 Recommend = ShouldBeInTeamsRegion;
2843 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002844 if (!NestingProhibited &&
2845 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2846 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2847 // OpenMP 4.5 [2.17 Nesting of Regions]
2848 // If a target, target update, target data, target enter data, or
2849 // target exit data construct is encountered during execution of a
2850 // target region, the behavior is unspecified.
2851 NestingProhibited = Stack->hasDirective(
2852 [&OffendingRegion](OpenMPDirectiveKind K,
2853 const DeclarationNameInfo &DNI,
2854 SourceLocation Loc) -> bool {
2855 if (isOpenMPTargetExecutionDirective(K)) {
2856 OffendingRegion = K;
2857 return true;
2858 } else
2859 return false;
2860 },
2861 false /* don't skip top directive */);
2862 CloseNesting = false;
2863 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002864 if (NestingProhibited) {
2865 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002866 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2867 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002868 return true;
2869 }
2870 }
2871 return false;
2872}
2873
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002874static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2875 ArrayRef<OMPClause *> Clauses,
2876 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2877 bool ErrorFound = false;
2878 unsigned NamedModifiersNumber = 0;
2879 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2880 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002881 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002882 for (const auto *C : Clauses) {
2883 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2884 // At most one if clause without a directive-name-modifier can appear on
2885 // the directive.
2886 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2887 if (FoundNameModifiers[CurNM]) {
2888 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2889 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2890 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2891 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002892 } else if (CurNM != OMPD_unknown) {
2893 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002894 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002895 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002896 FoundNameModifiers[CurNM] = IC;
2897 if (CurNM == OMPD_unknown)
2898 continue;
2899 // Check if the specified name modifier is allowed for the current
2900 // directive.
2901 // At most one if clause with the particular directive-name-modifier can
2902 // appear on the directive.
2903 bool MatchFound = false;
2904 for (auto NM : AllowedNameModifiers) {
2905 if (CurNM == NM) {
2906 MatchFound = true;
2907 break;
2908 }
2909 }
2910 if (!MatchFound) {
2911 S.Diag(IC->getNameModifierLoc(),
2912 diag::err_omp_wrong_if_directive_name_modifier)
2913 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2914 ErrorFound = true;
2915 }
2916 }
2917 }
2918 // If any if clause on the directive includes a directive-name-modifier then
2919 // all if clauses on the directive must include a directive-name-modifier.
2920 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2921 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2922 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2923 diag::err_omp_no_more_if_clause);
2924 } else {
2925 std::string Values;
2926 std::string Sep(", ");
2927 unsigned AllowedCnt = 0;
2928 unsigned TotalAllowedNum =
2929 AllowedNameModifiers.size() - NamedModifiersNumber;
2930 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2931 ++Cnt) {
2932 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2933 if (!FoundNameModifiers[NM]) {
2934 Values += "'";
2935 Values += getOpenMPDirectiveName(NM);
2936 Values += "'";
2937 if (AllowedCnt + 2 == TotalAllowedNum)
2938 Values += " or ";
2939 else if (AllowedCnt + 1 != TotalAllowedNum)
2940 Values += Sep;
2941 ++AllowedCnt;
2942 }
2943 }
2944 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2945 diag::err_omp_unnamed_if_clause)
2946 << (TotalAllowedNum > 1) << Values;
2947 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002948 for (auto Loc : NameModifierLoc) {
2949 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2950 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002951 ErrorFound = true;
2952 }
2953 return ErrorFound;
2954}
2955
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002956StmtResult Sema::ActOnOpenMPExecutableDirective(
2957 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2958 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2959 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002960 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002961 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2962 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002963 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002964
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002965 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002966 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002967 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002968 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002969 if (AStmt) {
2970 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2971
2972 // Check default data sharing attributes for referenced variables.
2973 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2974 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2975 if (DSAChecker.isErrorFound())
2976 return StmtError();
2977 // Generate list of implicitly defined firstprivate variables.
2978 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002979
2980 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2981 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2982 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2983 SourceLocation(), SourceLocation())) {
2984 ClausesWithImplicit.push_back(Implicit);
2985 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2986 DSAChecker.getImplicitFirstprivate().size();
2987 } else
2988 ErrorFound = true;
2989 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002990 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002991
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002992 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002993 switch (Kind) {
2994 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002995 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2996 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002997 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002998 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002999 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003000 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3001 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003002 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003003 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003004 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3005 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003006 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003007 case OMPD_for_simd:
3008 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3009 EndLoc, VarsWithInheritedDSA);
3010 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003011 case OMPD_sections:
3012 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3013 EndLoc);
3014 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003015 case OMPD_section:
3016 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003017 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003018 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3019 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003020 case OMPD_single:
3021 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3022 EndLoc);
3023 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003024 case OMPD_master:
3025 assert(ClausesWithImplicit.empty() &&
3026 "No clauses are allowed for 'omp master' directive");
3027 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3028 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003029 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003030 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3031 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003032 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003033 case OMPD_parallel_for:
3034 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3035 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003036 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003037 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003038 case OMPD_parallel_for_simd:
3039 Res = ActOnOpenMPParallelForSimdDirective(
3040 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003041 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003042 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003043 case OMPD_parallel_sections:
3044 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3045 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003046 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003047 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003048 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003049 Res =
3050 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003051 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003052 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003053 case OMPD_taskyield:
3054 assert(ClausesWithImplicit.empty() &&
3055 "No clauses are allowed for 'omp taskyield' directive");
3056 assert(AStmt == nullptr &&
3057 "No associated statement allowed for 'omp taskyield' directive");
3058 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3059 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003060 case OMPD_barrier:
3061 assert(ClausesWithImplicit.empty() &&
3062 "No clauses are allowed for 'omp barrier' directive");
3063 assert(AStmt == nullptr &&
3064 "No associated statement allowed for 'omp barrier' directive");
3065 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3066 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003067 case OMPD_taskwait:
3068 assert(ClausesWithImplicit.empty() &&
3069 "No clauses are allowed for 'omp taskwait' directive");
3070 assert(AStmt == nullptr &&
3071 "No associated statement allowed for 'omp taskwait' directive");
3072 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3073 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003074 case OMPD_taskgroup:
3075 assert(ClausesWithImplicit.empty() &&
3076 "No clauses are allowed for 'omp taskgroup' directive");
3077 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3078 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003079 case OMPD_flush:
3080 assert(AStmt == nullptr &&
3081 "No associated statement allowed for 'omp flush' directive");
3082 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3083 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003084 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003085 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3086 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003087 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003088 case OMPD_atomic:
3089 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3090 EndLoc);
3091 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003092 case OMPD_teams:
3093 Res =
3094 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3095 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003096 case OMPD_target:
3097 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3098 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003099 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003100 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003101 case OMPD_target_parallel:
3102 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3103 StartLoc, EndLoc);
3104 AllowedNameModifiers.push_back(OMPD_target);
3105 AllowedNameModifiers.push_back(OMPD_parallel);
3106 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003107 case OMPD_target_parallel_for:
3108 Res = ActOnOpenMPTargetParallelForDirective(
3109 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3110 AllowedNameModifiers.push_back(OMPD_target);
3111 AllowedNameModifiers.push_back(OMPD_parallel);
3112 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003113 case OMPD_cancellation_point:
3114 assert(ClausesWithImplicit.empty() &&
3115 "No clauses are allowed for 'omp cancellation point' directive");
3116 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3117 "cancellation point' directive");
3118 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3119 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003120 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003121 assert(AStmt == nullptr &&
3122 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003123 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3124 CancelRegion);
3125 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003126 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003127 case OMPD_target_data:
3128 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3129 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003130 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003131 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003132 case OMPD_target_enter_data:
3133 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3134 EndLoc);
3135 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3136 break;
Samuel Antao72590762016-01-19 20:04:50 +00003137 case OMPD_target_exit_data:
3138 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3139 EndLoc);
3140 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3141 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003142 case OMPD_taskloop:
3143 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3144 EndLoc, VarsWithInheritedDSA);
3145 AllowedNameModifiers.push_back(OMPD_taskloop);
3146 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003147 case OMPD_taskloop_simd:
3148 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3149 EndLoc, VarsWithInheritedDSA);
3150 AllowedNameModifiers.push_back(OMPD_taskloop);
3151 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003152 case OMPD_distribute:
3153 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3154 EndLoc, VarsWithInheritedDSA);
3155 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003156 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003157 case OMPD_declare_reduction:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003158 llvm_unreachable("OpenMP Directive is not allowed");
3159 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003160 llvm_unreachable("Unknown OpenMP directive");
3161 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003162
Alexey Bataev4acb8592014-07-07 13:01:15 +00003163 for (auto P : VarsWithInheritedDSA) {
3164 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3165 << P.first << P.second->getSourceRange();
3166 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003167 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3168
3169 if (!AllowedNameModifiers.empty())
3170 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3171 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003172
Alexey Bataeved09d242014-05-28 05:53:51 +00003173 if (ErrorFound)
3174 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003175 return Res;
3176}
3177
3178StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3179 Stmt *AStmt,
3180 SourceLocation StartLoc,
3181 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003182 if (!AStmt)
3183 return StmtError();
3184
Alexey Bataev9959db52014-05-06 10:08:46 +00003185 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3186 // 1.2.2 OpenMP Language Terminology
3187 // Structured block - An executable statement with a single entry at the
3188 // top and a single exit at the bottom.
3189 // The point of exit cannot be a branch out of the structured block.
3190 // longjmp() and throw() must not violate the entry/exit criteria.
3191 CS->getCapturedDecl()->setNothrow();
3192
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003193 getCurFunction()->setHasBranchProtectedScope();
3194
Alexey Bataev25e5b442015-09-15 12:52:43 +00003195 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3196 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003197}
3198
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003199namespace {
3200/// \brief Helper class for checking canonical form of the OpenMP loops and
3201/// extracting iteration space of each loop in the loop nest, that will be used
3202/// for IR generation.
3203class OpenMPIterationSpaceChecker {
3204 /// \brief Reference to Sema.
3205 Sema &SemaRef;
3206 /// \brief A location for diagnostics (when there is no some better location).
3207 SourceLocation DefaultLoc;
3208 /// \brief A location for diagnostics (when increment is not compatible).
3209 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003210 /// \brief A source location for referring to loop init later.
3211 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003212 /// \brief A source location for referring to condition later.
3213 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003214 /// \brief A source location for referring to increment later.
3215 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003216 /// \brief Loop variable.
3217 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003218 /// \brief Reference to loop variable.
3219 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003220 /// \brief Lower bound (initializer for the var).
3221 Expr *LB;
3222 /// \brief Upper bound.
3223 Expr *UB;
3224 /// \brief Loop step (increment).
3225 Expr *Step;
3226 /// \brief This flag is true when condition is one of:
3227 /// Var < UB
3228 /// Var <= UB
3229 /// UB > Var
3230 /// UB >= Var
3231 bool TestIsLessOp;
3232 /// \brief This flag is true when condition is strict ( < or > ).
3233 bool TestIsStrictOp;
3234 /// \brief This flag is true when step is subtracted on each iteration.
3235 bool SubtractStep;
3236
3237public:
3238 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3239 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003240 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3241 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003242 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3243 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003244 /// \brief Check init-expr for canonical loop form and save loop counter
3245 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003246 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003247 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3248 /// for less/greater and for strict/non-strict comparison.
3249 bool CheckCond(Expr *S);
3250 /// \brief Check incr-expr for canonical loop form and return true if it
3251 /// does not conform, otherwise save loop step (#Step).
3252 bool CheckInc(Expr *S);
3253 /// \brief Return the loop counter variable.
3254 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003255 /// \brief Return the reference expression to loop counter variable.
3256 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003257 /// \brief Source range of the loop init.
3258 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3259 /// \brief Source range of the loop condition.
3260 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3261 /// \brief Source range of the loop increment.
3262 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3263 /// \brief True if the step should be subtracted.
3264 bool ShouldSubtractStep() const { return SubtractStep; }
3265 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003266 Expr *
3267 BuildNumIterations(Scope *S, const bool LimitedType,
3268 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003269 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003270 Expr *BuildPreCond(Scope *S, Expr *Cond,
3271 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003272 /// \brief Build reference expression to the counter be used for codegen.
3273 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003274 /// \brief Build reference expression to the private counter be used for
3275 /// codegen.
3276 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003277 /// \brief Build initization of the counter be used for codegen.
3278 Expr *BuildCounterInit() const;
3279 /// \brief Build step of the counter be used for codegen.
3280 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003281 /// \brief Return true if any expression is dependent.
3282 bool Dependent() const;
3283
3284private:
3285 /// \brief Check the right-hand side of an assignment in the increment
3286 /// expression.
3287 bool CheckIncRHS(Expr *RHS);
3288 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003289 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003290 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003291 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003292 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003293 /// \brief Helper to set loop increment.
3294 bool SetStep(Expr *NewStep, bool Subtract);
3295};
3296
3297bool OpenMPIterationSpaceChecker::Dependent() const {
3298 if (!Var) {
3299 assert(!LB && !UB && !Step);
3300 return false;
3301 }
3302 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3303 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3304}
3305
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003306template <typename T>
3307static T *getExprAsWritten(T *E) {
3308 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3309 E = ExprTemp->getSubExpr();
3310
3311 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3312 E = MTE->GetTemporaryExpr();
3313
3314 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3315 E = Binder->getSubExpr();
3316
3317 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3318 E = ICE->getSubExprAsWritten();
3319 return E->IgnoreParens();
3320}
3321
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003322bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3323 DeclRefExpr *NewVarRefExpr,
3324 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003325 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003326 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3327 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003328 if (!NewVar || !NewLB)
3329 return true;
3330 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003331 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003332 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3333 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003334 if ((Ctor->isCopyOrMoveConstructor() ||
3335 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3336 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003337 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003338 LB = NewLB;
3339 return false;
3340}
3341
3342bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003343 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003344 // State consistency checking to ensure correct usage.
3345 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3346 !TestIsLessOp && !TestIsStrictOp);
3347 if (!NewUB)
3348 return true;
3349 UB = NewUB;
3350 TestIsLessOp = LessOp;
3351 TestIsStrictOp = StrictOp;
3352 ConditionSrcRange = SR;
3353 ConditionLoc = SL;
3354 return false;
3355}
3356
3357bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3358 // State consistency checking to ensure correct usage.
3359 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3360 if (!NewStep)
3361 return true;
3362 if (!NewStep->isValueDependent()) {
3363 // Check that the step is integer expression.
3364 SourceLocation StepLoc = NewStep->getLocStart();
3365 ExprResult Val =
3366 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3367 if (Val.isInvalid())
3368 return true;
3369 NewStep = Val.get();
3370
3371 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3372 // If test-expr is of form var relational-op b and relational-op is < or
3373 // <= then incr-expr must cause var to increase on each iteration of the
3374 // loop. If test-expr is of form var relational-op b and relational-op is
3375 // > or >= then incr-expr must cause var to decrease on each iteration of
3376 // the loop.
3377 // If test-expr is of form b relational-op var and relational-op is < or
3378 // <= then incr-expr must cause var to decrease on each iteration of the
3379 // loop. If test-expr is of form b relational-op var and relational-op is
3380 // > or >= then incr-expr must cause var to increase on each iteration of
3381 // the loop.
3382 llvm::APSInt Result;
3383 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3384 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3385 bool IsConstNeg =
3386 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003387 bool IsConstPos =
3388 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003389 bool IsConstZero = IsConstant && !Result.getBoolValue();
3390 if (UB && (IsConstZero ||
3391 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003392 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003393 SemaRef.Diag(NewStep->getExprLoc(),
3394 diag::err_omp_loop_incr_not_compatible)
3395 << Var << TestIsLessOp << NewStep->getSourceRange();
3396 SemaRef.Diag(ConditionLoc,
3397 diag::note_omp_loop_cond_requres_compatible_incr)
3398 << TestIsLessOp << ConditionSrcRange;
3399 return true;
3400 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003401 if (TestIsLessOp == Subtract) {
3402 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3403 NewStep).get();
3404 Subtract = !Subtract;
3405 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003406 }
3407
3408 Step = NewStep;
3409 SubtractStep = Subtract;
3410 return false;
3411}
3412
Alexey Bataev9c821032015-04-30 04:23:23 +00003413bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003414 // Check init-expr for canonical loop form and save loop counter
3415 // variable - #Var and its initialization value - #LB.
3416 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3417 // var = lb
3418 // integer-type var = lb
3419 // random-access-iterator-type var = lb
3420 // pointer-type var = lb
3421 //
3422 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003423 if (EmitDiags) {
3424 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3425 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003426 return true;
3427 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003428 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003429 if (Expr *E = dyn_cast<Expr>(S))
3430 S = E->IgnoreParens();
3431 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3432 if (BO->getOpcode() == BO_Assign)
3433 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003434 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003435 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003436 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3437 if (DS->isSingleDecl()) {
3438 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003439 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003440 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003441 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003442 SemaRef.Diag(S->getLocStart(),
3443 diag::ext_omp_loop_not_canonical_init)
3444 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003445 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003446 }
3447 }
3448 }
3449 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3450 if (CE->getOperator() == OO_Equal)
3451 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003452 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3453 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003454
Alexey Bataev9c821032015-04-30 04:23:23 +00003455 if (EmitDiags) {
3456 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3457 << S->getSourceRange();
3458 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003459 return true;
3460}
3461
Alexey Bataev23b69422014-06-18 07:08:49 +00003462/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003463/// variable (which may be the loop variable) if possible.
3464static const VarDecl *GetInitVarDecl(const Expr *E) {
3465 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003466 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003467 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003468 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3469 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003470 if ((Ctor->isCopyOrMoveConstructor() ||
3471 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3472 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003473 E = CE->getArg(0)->IgnoreParenImpCasts();
3474 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3475 if (!DRE)
3476 return nullptr;
3477 return dyn_cast<VarDecl>(DRE->getDecl());
3478}
3479
3480bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3481 // Check test-expr for canonical form, save upper-bound UB, flags for
3482 // less/greater and for strict/non-strict comparison.
3483 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3484 // var relational-op b
3485 // b relational-op var
3486 //
3487 if (!S) {
3488 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3489 return true;
3490 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003491 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003492 SourceLocation CondLoc = S->getLocStart();
3493 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3494 if (BO->isRelationalOp()) {
3495 if (GetInitVarDecl(BO->getLHS()) == Var)
3496 return SetUB(BO->getRHS(),
3497 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3498 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3499 BO->getSourceRange(), BO->getOperatorLoc());
3500 if (GetInitVarDecl(BO->getRHS()) == Var)
3501 return SetUB(BO->getLHS(),
3502 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3503 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3504 BO->getSourceRange(), BO->getOperatorLoc());
3505 }
3506 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3507 if (CE->getNumArgs() == 2) {
3508 auto Op = CE->getOperator();
3509 switch (Op) {
3510 case OO_Greater:
3511 case OO_GreaterEqual:
3512 case OO_Less:
3513 case OO_LessEqual:
3514 if (GetInitVarDecl(CE->getArg(0)) == Var)
3515 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3516 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3517 CE->getOperatorLoc());
3518 if (GetInitVarDecl(CE->getArg(1)) == Var)
3519 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3520 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3521 CE->getOperatorLoc());
3522 break;
3523 default:
3524 break;
3525 }
3526 }
3527 }
3528 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3529 << S->getSourceRange() << Var;
3530 return true;
3531}
3532
3533bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3534 // RHS of canonical loop form increment can be:
3535 // var + incr
3536 // incr + var
3537 // var - incr
3538 //
3539 RHS = RHS->IgnoreParenImpCasts();
3540 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3541 if (BO->isAdditiveOp()) {
3542 bool IsAdd = BO->getOpcode() == BO_Add;
3543 if (GetInitVarDecl(BO->getLHS()) == Var)
3544 return SetStep(BO->getRHS(), !IsAdd);
3545 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3546 return SetStep(BO->getLHS(), false);
3547 }
3548 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3549 bool IsAdd = CE->getOperator() == OO_Plus;
3550 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3551 if (GetInitVarDecl(CE->getArg(0)) == Var)
3552 return SetStep(CE->getArg(1), !IsAdd);
3553 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3554 return SetStep(CE->getArg(0), false);
3555 }
3556 }
3557 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3558 << RHS->getSourceRange() << Var;
3559 return true;
3560}
3561
3562bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3563 // Check incr-expr for canonical loop form and return true if it
3564 // does not conform.
3565 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3566 // ++var
3567 // var++
3568 // --var
3569 // var--
3570 // var += incr
3571 // var -= incr
3572 // var = var + incr
3573 // var = incr + var
3574 // var = var - incr
3575 //
3576 if (!S) {
3577 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3578 return true;
3579 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003580 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003581 S = S->IgnoreParens();
3582 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3583 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3584 return SetStep(
3585 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3586 (UO->isDecrementOp() ? -1 : 1)).get(),
3587 false);
3588 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3589 switch (BO->getOpcode()) {
3590 case BO_AddAssign:
3591 case BO_SubAssign:
3592 if (GetInitVarDecl(BO->getLHS()) == Var)
3593 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3594 break;
3595 case BO_Assign:
3596 if (GetInitVarDecl(BO->getLHS()) == Var)
3597 return CheckIncRHS(BO->getRHS());
3598 break;
3599 default:
3600 break;
3601 }
3602 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3603 switch (CE->getOperator()) {
3604 case OO_PlusPlus:
3605 case OO_MinusMinus:
3606 if (GetInitVarDecl(CE->getArg(0)) == Var)
3607 return SetStep(
3608 SemaRef.ActOnIntegerConstant(
3609 CE->getLocStart(),
3610 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3611 false);
3612 break;
3613 case OO_PlusEqual:
3614 case OO_MinusEqual:
3615 if (GetInitVarDecl(CE->getArg(0)) == Var)
3616 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3617 break;
3618 case OO_Equal:
3619 if (GetInitVarDecl(CE->getArg(0)) == Var)
3620 return CheckIncRHS(CE->getArg(1));
3621 break;
3622 default:
3623 break;
3624 }
3625 }
3626 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3627 << S->getSourceRange() << Var;
3628 return true;
3629}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003630
Alexey Bataev5a3af132016-03-29 08:58:54 +00003631static ExprResult
3632tryBuildCapture(Sema &SemaRef, Expr *Capture,
3633 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3634 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3635 return SemaRef.PerformImplicitConversion(
3636 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3637 /*AllowExplicit=*/true);
3638 auto I = Captures.find(Capture);
3639 if (I != Captures.end())
3640 return buildCapture(SemaRef, Capture, I->second);
3641 DeclRefExpr *Ref = nullptr;
3642 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3643 Captures[Capture] = Ref;
3644 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003645}
3646
Alexander Musmana5f070a2014-10-01 06:03:56 +00003647/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003648Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3649 Scope *S, const bool LimitedType,
3650 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003651 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003652 auto VarType = Var->getType().getNonReferenceType();
3653 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003654 SemaRef.getLangOpts().CPlusPlus) {
3655 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003656 auto *UBExpr = TestIsLessOp ? UB : LB;
3657 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00003658 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3659 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003660 if (!Upper || !Lower)
3661 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003662
3663 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3664
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003665 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003666 // BuildBinOp already emitted error, this one is to point user to upper
3667 // and lower bound, and to tell what is passed to 'operator-'.
3668 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3669 << Upper->getSourceRange() << Lower->getSourceRange();
3670 return nullptr;
3671 }
3672 }
3673
3674 if (!Diff.isUsable())
3675 return nullptr;
3676
3677 // Upper - Lower [- 1]
3678 if (TestIsStrictOp)
3679 Diff = SemaRef.BuildBinOp(
3680 S, DefaultLoc, BO_Sub, Diff.get(),
3681 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3682 if (!Diff.isUsable())
3683 return nullptr;
3684
3685 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00003686 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3687 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003688 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003689 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003690 if (!Diff.isUsable())
3691 return nullptr;
3692
3693 // Parentheses (for dumping/debugging purposes only).
3694 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3695 if (!Diff.isUsable())
3696 return nullptr;
3697
3698 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003699 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003700 if (!Diff.isUsable())
3701 return nullptr;
3702
Alexander Musman174b3ca2014-10-06 11:16:29 +00003703 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003704 QualType Type = Diff.get()->getType();
3705 auto &C = SemaRef.Context;
3706 bool UseVarType = VarType->hasIntegerRepresentation() &&
3707 C.getTypeSize(Type) > C.getTypeSize(VarType);
3708 if (!Type->isIntegerType() || UseVarType) {
3709 unsigned NewSize =
3710 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3711 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3712 : Type->hasSignedIntegerRepresentation();
3713 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003714 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3715 Diff = SemaRef.PerformImplicitConversion(
3716 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3717 if (!Diff.isUsable())
3718 return nullptr;
3719 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003720 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003721 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003722 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3723 if (NewSize != C.getTypeSize(Type)) {
3724 if (NewSize < C.getTypeSize(Type)) {
3725 assert(NewSize == 64 && "incorrect loop var size");
3726 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3727 << InitSrcRange << ConditionSrcRange;
3728 }
3729 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003730 NewSize, Type->hasSignedIntegerRepresentation() ||
3731 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00003732 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3733 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3734 Sema::AA_Converting, true);
3735 if (!Diff.isUsable())
3736 return nullptr;
3737 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003738 }
3739 }
3740
Alexander Musmana5f070a2014-10-01 06:03:56 +00003741 return Diff.get();
3742}
3743
Alexey Bataev5a3af132016-03-29 08:58:54 +00003744Expr *OpenMPIterationSpaceChecker::BuildPreCond(
3745 Scope *S, Expr *Cond,
3746 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003747 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3748 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3749 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003750
Alexey Bataev5a3af132016-03-29 08:58:54 +00003751 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
3752 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
3753 if (!NewLB.isUsable() || !NewUB.isUsable())
3754 return nullptr;
3755
Alexey Bataev62dbb972015-04-22 11:59:37 +00003756 auto CondExpr = SemaRef.BuildBinOp(
3757 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3758 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003759 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003760 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003761 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
3762 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00003763 CondExpr = SemaRef.PerformImplicitConversion(
3764 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3765 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003766 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003767 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3768 // Otherwise use original loop conditon and evaluate it in runtime.
3769 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3770}
3771
Alexander Musmana5f070a2014-10-01 06:03:56 +00003772/// \brief Build reference expression to the counter be used for codegen.
3773Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003774 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3775 DefaultLoc);
3776}
3777
3778Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3779 if (Var && !Var->isInvalidDecl()) {
3780 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003781 auto *PrivateVar =
3782 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3783 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003784 if (PrivateVar->isInvalidDecl())
3785 return nullptr;
3786 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3787 }
3788 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003789}
3790
3791/// \brief Build initization of the counter be used for codegen.
3792Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3793
3794/// \brief Build step of the counter be used for codegen.
3795Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3796
3797/// \brief Iteration space of a single for loop.
3798struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003799 /// \brief Condition of the loop.
3800 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003801 /// \brief This expression calculates the number of iterations in the loop.
3802 /// It is always possible to calculate it before starting the loop.
3803 Expr *NumIterations;
3804 /// \brief The loop counter variable.
3805 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003806 /// \brief Private loop counter variable.
3807 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003808 /// \brief This is initializer for the initial value of #CounterVar.
3809 Expr *CounterInit;
3810 /// \brief This is step for the #CounterVar used to generate its update:
3811 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3812 Expr *CounterStep;
3813 /// \brief Should step be subtracted?
3814 bool Subtract;
3815 /// \brief Source range of the loop init.
3816 SourceRange InitSrcRange;
3817 /// \brief Source range of the loop condition.
3818 SourceRange CondSrcRange;
3819 /// \brief Source range of the loop increment.
3820 SourceRange IncSrcRange;
3821};
3822
Alexey Bataev23b69422014-06-18 07:08:49 +00003823} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003824
Alexey Bataev9c821032015-04-30 04:23:23 +00003825void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3826 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3827 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003828 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3829 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003830 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3831 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003832 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003833 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003834 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003835 }
3836}
3837
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003838/// \brief Called on a for stmt to check and extract its iteration space
3839/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003840static bool CheckOpenMPIterationSpace(
3841 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3842 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003843 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003844 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00003845 LoopIterationSpace &ResultIterSpace,
3846 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003847 // OpenMP [2.6, Canonical Loop Form]
3848 // for (init-expr; test-expr; incr-expr) structured-block
3849 auto For = dyn_cast_or_null<ForStmt>(S);
3850 if (!For) {
3851 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003852 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3853 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3854 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3855 if (NestedLoopCount > 1) {
3856 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3857 SemaRef.Diag(DSA.getConstructLoc(),
3858 diag::note_omp_collapse_ordered_expr)
3859 << 2 << CollapseLoopCountExpr->getSourceRange()
3860 << OrderedLoopCountExpr->getSourceRange();
3861 else if (CollapseLoopCountExpr)
3862 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3863 diag::note_omp_collapse_ordered_expr)
3864 << 0 << CollapseLoopCountExpr->getSourceRange();
3865 else
3866 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3867 diag::note_omp_collapse_ordered_expr)
3868 << 1 << OrderedLoopCountExpr->getSourceRange();
3869 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003870 return true;
3871 }
3872 assert(For->getBody());
3873
3874 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3875
3876 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003877 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003878 if (ISC.CheckInit(Init)) {
3879 return true;
3880 }
3881
3882 bool HasErrors = false;
3883
3884 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003885 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003886
3887 // OpenMP [2.6, Canonical Loop Form]
3888 // Var is one of the following:
3889 // A variable of signed or unsigned integer type.
3890 // For C++, a variable of a random access iterator type.
3891 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003892 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003893 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3894 !VarType->isPointerType() &&
3895 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3896 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3897 << SemaRef.getLangOpts().CPlusPlus;
3898 HasErrors = true;
3899 }
3900
Alexey Bataev4acb8592014-07-07 13:01:15 +00003901 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3902 // Construct
3903 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3904 // parallel for construct is (are) private.
3905 // The loop iteration variable in the associated for-loop of a simd construct
3906 // with just one associated for-loop is linear with a constant-linear-step
3907 // that is the increment of the associated for-loop.
3908 // Exclude loop var from the list of variables with implicitly defined data
3909 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003910 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003911
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003912 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3913 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003914 // The loop iteration variable in the associated for-loop of a simd construct
3915 // with just one associated for-loop may be listed in a linear clause with a
3916 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003917 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3918 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003919 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003920 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3921 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3922 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003923 auto PredeterminedCKind =
3924 isOpenMPSimdDirective(DKind)
3925 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3926 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003927 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003928 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003929 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003930 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003931 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003932 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3933 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003934 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003935 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3936 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003937 if (DVar.RefExpr == nullptr)
3938 DVar.CKind = PredeterminedCKind;
3939 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003940 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003941 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003942 // Make the loop iteration variable private (for worksharing constructs),
3943 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003944 // lastprivate (for simd directives with several collapsed or ordered
3945 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003946 if (DVar.CKind == OMPC_unknown)
3947 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3948 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003949 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003950 }
3951
Alexey Bataev7ff55242014-06-19 09:13:45 +00003952 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003953
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003954 // Check test-expr.
3955 HasErrors |= ISC.CheckCond(For->getCond());
3956
3957 // Check incr-expr.
3958 HasErrors |= ISC.CheckInc(For->getInc());
3959
Alexander Musmana5f070a2014-10-01 06:03:56 +00003960 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003961 return HasErrors;
3962
Alexander Musmana5f070a2014-10-01 06:03:56 +00003963 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003964 ResultIterSpace.PreCond =
3965 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003966 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00003967 DSA.getCurScope(),
3968 (isOpenMPWorksharingDirective(DKind) ||
3969 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
3970 Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003971 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003972 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003973 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3974 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3975 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3976 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3977 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3978 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3979
Alexey Bataev62dbb972015-04-22 11:59:37 +00003980 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3981 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003982 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003983 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003984 ResultIterSpace.CounterInit == nullptr ||
3985 ResultIterSpace.CounterStep == nullptr);
3986
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003987 return HasErrors;
3988}
3989
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003990/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003991static ExprResult
3992BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
3993 ExprResult Start,
3994 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003995 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003996 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
3997 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003998 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00003999 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004000 VarRef.get()->getType())) {
4001 NewStart = SemaRef.PerformImplicitConversion(
4002 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4003 /*AllowExplicit=*/true);
4004 if (!NewStart.isUsable())
4005 return ExprError();
4006 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004007
4008 auto Init =
4009 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4010 return Init;
4011}
4012
Alexander Musmana5f070a2014-10-01 06:03:56 +00004013/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004014static ExprResult
4015BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4016 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4017 ExprResult Step, bool Subtract,
4018 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004019 // Add parentheses (for debugging purposes only).
4020 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4021 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4022 !Step.isUsable())
4023 return ExprError();
4024
Alexey Bataev5a3af132016-03-29 08:58:54 +00004025 ExprResult NewStep = Step;
4026 if (Captures)
4027 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004028 if (NewStep.isInvalid())
4029 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004030 ExprResult Update =
4031 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004032 if (!Update.isUsable())
4033 return ExprError();
4034
Alexey Bataevc0214e02016-02-16 12:13:49 +00004035 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4036 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004037 ExprResult NewStart = Start;
4038 if (Captures)
4039 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004040 if (NewStart.isInvalid())
4041 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004042
Alexey Bataevc0214e02016-02-16 12:13:49 +00004043 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4044 ExprResult SavedUpdate = Update;
4045 ExprResult UpdateVal;
4046 if (VarRef.get()->getType()->isOverloadableType() ||
4047 NewStart.get()->getType()->isOverloadableType() ||
4048 Update.get()->getType()->isOverloadableType()) {
4049 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4050 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4051 Update =
4052 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4053 if (Update.isUsable()) {
4054 UpdateVal =
4055 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4056 VarRef.get(), SavedUpdate.get());
4057 if (UpdateVal.isUsable()) {
4058 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4059 UpdateVal.get());
4060 }
4061 }
4062 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4063 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004064
Alexey Bataevc0214e02016-02-16 12:13:49 +00004065 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4066 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4067 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4068 NewStart.get(), SavedUpdate.get());
4069 if (!Update.isUsable())
4070 return ExprError();
4071
Alexey Bataev11481f52016-02-17 10:29:05 +00004072 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4073 VarRef.get()->getType())) {
4074 Update = SemaRef.PerformImplicitConversion(
4075 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4076 if (!Update.isUsable())
4077 return ExprError();
4078 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004079
4080 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4081 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004082 return Update;
4083}
4084
4085/// \brief Convert integer expression \a E to make it have at least \a Bits
4086/// bits.
4087static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4088 Sema &SemaRef) {
4089 if (E == nullptr)
4090 return ExprError();
4091 auto &C = SemaRef.Context;
4092 QualType OldType = E->getType();
4093 unsigned HasBits = C.getTypeSize(OldType);
4094 if (HasBits >= Bits)
4095 return ExprResult(E);
4096 // OK to convert to signed, because new type has more bits than old.
4097 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4098 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4099 true);
4100}
4101
4102/// \brief Check if the given expression \a E is a constant integer that fits
4103/// into \a Bits bits.
4104static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4105 if (E == nullptr)
4106 return false;
4107 llvm::APSInt Result;
4108 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4109 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4110 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004111}
4112
Alexey Bataev5a3af132016-03-29 08:58:54 +00004113/// Build preinits statement for the given declarations.
4114static Stmt *buildPreInits(ASTContext &Context,
4115 SmallVectorImpl<Decl *> &PreInits) {
4116 if (!PreInits.empty()) {
4117 return new (Context) DeclStmt(
4118 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4119 SourceLocation(), SourceLocation());
4120 }
4121 return nullptr;
4122}
4123
4124/// Build preinits statement for the given declarations.
4125static Stmt *buildPreInits(ASTContext &Context,
4126 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4127 if (!Captures.empty()) {
4128 SmallVector<Decl *, 16> PreInits;
4129 for (auto &Pair : Captures)
4130 PreInits.push_back(Pair.second->getDecl());
4131 return buildPreInits(Context, PreInits);
4132 }
4133 return nullptr;
4134}
4135
4136/// Build postupdate expression for the given list of postupdates expressions.
4137static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4138 Expr *PostUpdate = nullptr;
4139 if (!PostUpdates.empty()) {
4140 for (auto *E : PostUpdates) {
4141 Expr *ConvE = S.BuildCStyleCastExpr(
4142 E->getExprLoc(),
4143 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4144 E->getExprLoc(), E)
4145 .get();
4146 PostUpdate = PostUpdate
4147 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4148 PostUpdate, ConvE)
4149 .get()
4150 : ConvE;
4151 }
4152 }
4153 return PostUpdate;
4154}
4155
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004156/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004157/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4158/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004159static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004160CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4161 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4162 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004163 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004164 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004165 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004166 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004167 // Found 'collapse' clause - calculate collapse number.
4168 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004169 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004170 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004171 }
4172 if (OrderedLoopCountExpr) {
4173 // Found 'ordered' clause - calculate collapse number.
4174 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004175 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4176 if (Result.getLimitedValue() < NestedLoopCount) {
4177 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4178 diag::err_omp_wrong_ordered_loop_count)
4179 << OrderedLoopCountExpr->getSourceRange();
4180 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4181 diag::note_collapse_loop_count)
4182 << CollapseLoopCountExpr->getSourceRange();
4183 }
4184 NestedLoopCount = Result.getLimitedValue();
4185 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004186 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004187 // This is helper routine for loop directives (e.g., 'for', 'simd',
4188 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004189 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004190 SmallVector<LoopIterationSpace, 4> IterSpaces;
4191 IterSpaces.resize(NestedLoopCount);
4192 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004193 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004194 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004195 NestedLoopCount, CollapseLoopCountExpr,
4196 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004197 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004198 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004199 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004200 // OpenMP [2.8.1, simd construct, Restrictions]
4201 // All loops associated with the construct must be perfectly nested; that
4202 // is, there must be no intervening code nor any OpenMP directive between
4203 // any two loops.
4204 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004205 }
4206
Alexander Musmana5f070a2014-10-01 06:03:56 +00004207 Built.clear(/* size */ NestedLoopCount);
4208
4209 if (SemaRef.CurContext->isDependentContext())
4210 return NestedLoopCount;
4211
4212 // An example of what is generated for the following code:
4213 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004214 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004215 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004216 // for (k = 0; k < NK; ++k)
4217 // for (j = J0; j < NJ; j+=2) {
4218 // <loop body>
4219 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004220 //
4221 // We generate the code below.
4222 // Note: the loop body may be outlined in CodeGen.
4223 // Note: some counters may be C++ classes, operator- is used to find number of
4224 // iterations and operator+= to calculate counter value.
4225 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4226 // or i64 is currently supported).
4227 //
4228 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4229 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4230 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4231 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4232 // // similar updates for vars in clauses (e.g. 'linear')
4233 // <loop body (using local i and j)>
4234 // }
4235 // i = NI; // assign final values of counters
4236 // j = NJ;
4237 //
4238
4239 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4240 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004241 // Precondition tests if there is at least one iteration (all conditions are
4242 // true).
4243 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004244 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004245 ExprResult LastIteration32 = WidenIterationCount(
4246 32 /* Bits */, SemaRef.PerformImplicitConversion(
4247 N0->IgnoreImpCasts(), N0->getType(),
4248 Sema::AA_Converting, /*AllowExplicit=*/true)
4249 .get(),
4250 SemaRef);
4251 ExprResult LastIteration64 = WidenIterationCount(
4252 64 /* Bits */, SemaRef.PerformImplicitConversion(
4253 N0->IgnoreImpCasts(), N0->getType(),
4254 Sema::AA_Converting, /*AllowExplicit=*/true)
4255 .get(),
4256 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004257
4258 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4259 return NestedLoopCount;
4260
4261 auto &C = SemaRef.Context;
4262 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4263
4264 Scope *CurScope = DSA.getCurScope();
4265 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004266 if (PreCond.isUsable()) {
4267 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4268 PreCond.get(), IterSpaces[Cnt].PreCond);
4269 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004270 auto N = IterSpaces[Cnt].NumIterations;
4271 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4272 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004273 LastIteration32 = SemaRef.BuildBinOp(
4274 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4275 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4276 Sema::AA_Converting,
4277 /*AllowExplicit=*/true)
4278 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004279 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004280 LastIteration64 = SemaRef.BuildBinOp(
4281 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4282 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4283 Sema::AA_Converting,
4284 /*AllowExplicit=*/true)
4285 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004286 }
4287
4288 // Choose either the 32-bit or 64-bit version.
4289 ExprResult LastIteration = LastIteration64;
4290 if (LastIteration32.isUsable() &&
4291 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4292 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4293 FitsInto(
4294 32 /* Bits */,
4295 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4296 LastIteration64.get(), SemaRef)))
4297 LastIteration = LastIteration32;
4298
4299 if (!LastIteration.isUsable())
4300 return 0;
4301
4302 // Save the number of iterations.
4303 ExprResult NumIterations = LastIteration;
4304 {
4305 LastIteration = SemaRef.BuildBinOp(
4306 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4307 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4308 if (!LastIteration.isUsable())
4309 return 0;
4310 }
4311
4312 // Calculate the last iteration number beforehand instead of doing this on
4313 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4314 llvm::APSInt Result;
4315 bool IsConstant =
4316 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4317 ExprResult CalcLastIteration;
4318 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004319 ExprResult SaveRef =
4320 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004321 LastIteration = SaveRef;
4322
4323 // Prepare SaveRef + 1.
4324 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004325 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004326 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4327 if (!NumIterations.isUsable())
4328 return 0;
4329 }
4330
4331 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4332
Alexander Musmanc6388682014-12-15 07:07:06 +00004333 QualType VType = LastIteration.get()->getType();
4334 // Build variables passed into runtime, nesessary for worksharing directives.
4335 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004336 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4337 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004338 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004339 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4340 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004341 SemaRef.AddInitializerToDecl(
4342 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4343 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4344
4345 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004346 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4347 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004348 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4349 /*DirectInit*/ false,
4350 /*TypeMayContainAuto*/ false);
4351
4352 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4353 // This will be used to implement clause 'lastprivate'.
4354 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004355 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4356 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004357 SemaRef.AddInitializerToDecl(
4358 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4359 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4360
4361 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004362 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4363 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004364 SemaRef.AddInitializerToDecl(
4365 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4366 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4367
4368 // Build expression: UB = min(UB, LastIteration)
4369 // It is nesessary for CodeGen of directives with static scheduling.
4370 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4371 UB.get(), LastIteration.get());
4372 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4373 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4374 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4375 CondOp.get());
4376 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4377 }
4378
4379 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004380 ExprResult IV;
4381 ExprResult Init;
4382 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004383 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4384 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004385 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004386 isOpenMPTaskLoopDirective(DKind) ||
4387 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004388 ? LB.get()
4389 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4390 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4391 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004392 }
4393
Alexander Musmanc6388682014-12-15 07:07:06 +00004394 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004395 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004396 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004397 (isOpenMPWorksharingDirective(DKind) ||
4398 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004399 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4400 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4401 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004402
4403 // Loop increment (IV = IV + 1)
4404 SourceLocation IncLoc;
4405 ExprResult Inc =
4406 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4407 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4408 if (!Inc.isUsable())
4409 return 0;
4410 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004411 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4412 if (!Inc.isUsable())
4413 return 0;
4414
4415 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4416 // Used for directives with static scheduling.
4417 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004418 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4419 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004420 // LB + ST
4421 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4422 if (!NextLB.isUsable())
4423 return 0;
4424 // LB = LB + ST
4425 NextLB =
4426 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4427 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4428 if (!NextLB.isUsable())
4429 return 0;
4430 // UB + ST
4431 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4432 if (!NextUB.isUsable())
4433 return 0;
4434 // UB = UB + ST
4435 NextUB =
4436 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4437 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4438 if (!NextUB.isUsable())
4439 return 0;
4440 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004441
4442 // Build updates and final values of the loop counters.
4443 bool HasErrors = false;
4444 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004445 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004446 Built.Updates.resize(NestedLoopCount);
4447 Built.Finals.resize(NestedLoopCount);
4448 {
4449 ExprResult Div;
4450 // Go from inner nested loop to outer.
4451 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4452 LoopIterationSpace &IS = IterSpaces[Cnt];
4453 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4454 // Build: Iter = (IV / Div) % IS.NumIters
4455 // where Div is product of previous iterations' IS.NumIters.
4456 ExprResult Iter;
4457 if (Div.isUsable()) {
4458 Iter =
4459 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4460 } else {
4461 Iter = IV;
4462 assert((Cnt == (int)NestedLoopCount - 1) &&
4463 "unusable div expected on first iteration only");
4464 }
4465
4466 if (Cnt != 0 && Iter.isUsable())
4467 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4468 IS.NumIterations);
4469 if (!Iter.isUsable()) {
4470 HasErrors = true;
4471 break;
4472 }
4473
Alexey Bataev39f915b82015-05-08 10:41:21 +00004474 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4475 auto *CounterVar = buildDeclRefExpr(
4476 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4477 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4478 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004479 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004480 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004481 if (!Init.isUsable()) {
4482 HasErrors = true;
4483 break;
4484 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004485 ExprResult Update = BuildCounterUpdate(
4486 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4487 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004488 if (!Update.isUsable()) {
4489 HasErrors = true;
4490 break;
4491 }
4492
4493 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4494 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004495 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004496 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004497 if (!Final.isUsable()) {
4498 HasErrors = true;
4499 break;
4500 }
4501
4502 // Build Div for the next iteration: Div <- Div * IS.NumIters
4503 if (Cnt != 0) {
4504 if (Div.isUnset())
4505 Div = IS.NumIterations;
4506 else
4507 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4508 IS.NumIterations);
4509
4510 // Add parentheses (for debugging purposes only).
4511 if (Div.isUsable())
4512 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4513 if (!Div.isUsable()) {
4514 HasErrors = true;
4515 break;
4516 }
4517 }
4518 if (!Update.isUsable() || !Final.isUsable()) {
4519 HasErrors = true;
4520 break;
4521 }
4522 // Save results
4523 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004524 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004525 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004526 Built.Updates[Cnt] = Update.get();
4527 Built.Finals[Cnt] = Final.get();
4528 }
4529 }
4530
4531 if (HasErrors)
4532 return 0;
4533
4534 // Save results
4535 Built.IterationVarRef = IV.get();
4536 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004537 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004538 Built.CalcLastIteration =
4539 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004540 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004541 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004542 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004543 Built.Init = Init.get();
4544 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004545 Built.LB = LB.get();
4546 Built.UB = UB.get();
4547 Built.IL = IL.get();
4548 Built.ST = ST.get();
4549 Built.EUB = EUB.get();
4550 Built.NLB = NextLB.get();
4551 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004552
Alexey Bataevabfc0692014-06-25 06:52:00 +00004553 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004554}
4555
Alexey Bataev10e775f2015-07-30 11:36:16 +00004556static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004557 auto CollapseClauses =
4558 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4559 if (CollapseClauses.begin() != CollapseClauses.end())
4560 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004561 return nullptr;
4562}
4563
Alexey Bataev10e775f2015-07-30 11:36:16 +00004564static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004565 auto OrderedClauses =
4566 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4567 if (OrderedClauses.begin() != OrderedClauses.end())
4568 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004569 return nullptr;
4570}
4571
Alexey Bataev66b15b52015-08-21 11:14:16 +00004572static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4573 const Expr *Safelen) {
4574 llvm::APSInt SimdlenRes, SafelenRes;
4575 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4576 Simdlen->isInstantiationDependent() ||
4577 Simdlen->containsUnexpandedParameterPack())
4578 return false;
4579 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4580 Safelen->isInstantiationDependent() ||
4581 Safelen->containsUnexpandedParameterPack())
4582 return false;
4583 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4584 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4585 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4586 // If both simdlen and safelen clauses are specified, the value of the simdlen
4587 // parameter must be less than or equal to the value of the safelen parameter.
4588 if (SimdlenRes > SafelenRes) {
4589 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4590 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4591 return true;
4592 }
4593 return false;
4594}
4595
Alexey Bataev4acb8592014-07-07 13:01:15 +00004596StmtResult Sema::ActOnOpenMPSimdDirective(
4597 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4598 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004599 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004600 if (!AStmt)
4601 return StmtError();
4602
4603 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004604 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004605 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4606 // define the nested loops number.
4607 unsigned NestedLoopCount = CheckOpenMPLoop(
4608 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4609 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004610 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004611 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004612
Alexander Musmana5f070a2014-10-01 06:03:56 +00004613 assert((CurContext->isDependentContext() || B.builtAll()) &&
4614 "omp simd loop exprs were not built");
4615
Alexander Musman3276a272015-03-21 10:12:56 +00004616 if (!CurContext->isDependentContext()) {
4617 // Finalize the clauses that need pre-built expressions for CodeGen.
4618 for (auto C : Clauses) {
4619 if (auto LC = dyn_cast<OMPLinearClause>(C))
4620 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4621 B.NumIterations, *this, CurScope))
4622 return StmtError();
4623 }
4624 }
4625
Alexey Bataev66b15b52015-08-21 11:14:16 +00004626 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4627 // If both simdlen and safelen clauses are specified, the value of the simdlen
4628 // parameter must be less than or equal to the value of the safelen parameter.
4629 OMPSafelenClause *Safelen = nullptr;
4630 OMPSimdlenClause *Simdlen = nullptr;
4631 for (auto *Clause : Clauses) {
4632 if (Clause->getClauseKind() == OMPC_safelen)
4633 Safelen = cast<OMPSafelenClause>(Clause);
4634 else if (Clause->getClauseKind() == OMPC_simdlen)
4635 Simdlen = cast<OMPSimdlenClause>(Clause);
4636 if (Safelen && Simdlen)
4637 break;
4638 }
4639 if (Simdlen && Safelen &&
4640 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4641 Safelen->getSafelen()))
4642 return StmtError();
4643
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004644 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004645 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4646 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004647}
4648
Alexey Bataev4acb8592014-07-07 13:01:15 +00004649StmtResult Sema::ActOnOpenMPForDirective(
4650 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4651 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004652 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004653 if (!AStmt)
4654 return StmtError();
4655
4656 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004657 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004658 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4659 // define the nested loops number.
4660 unsigned NestedLoopCount = CheckOpenMPLoop(
4661 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4662 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004663 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004664 return StmtError();
4665
Alexander Musmana5f070a2014-10-01 06:03:56 +00004666 assert((CurContext->isDependentContext() || B.builtAll()) &&
4667 "omp for loop exprs were not built");
4668
Alexey Bataev54acd402015-08-04 11:18:19 +00004669 if (!CurContext->isDependentContext()) {
4670 // Finalize the clauses that need pre-built expressions for CodeGen.
4671 for (auto C : Clauses) {
4672 if (auto LC = dyn_cast<OMPLinearClause>(C))
4673 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4674 B.NumIterations, *this, CurScope))
4675 return StmtError();
4676 }
4677 }
4678
Alexey Bataevf29276e2014-06-18 04:14:57 +00004679 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004680 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004681 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004682}
4683
Alexander Musmanf82886e2014-09-18 05:12:34 +00004684StmtResult Sema::ActOnOpenMPForSimdDirective(
4685 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4686 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004687 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004688 if (!AStmt)
4689 return StmtError();
4690
4691 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004692 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004693 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4694 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004695 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004696 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4697 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4698 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004699 if (NestedLoopCount == 0)
4700 return StmtError();
4701
Alexander Musmanc6388682014-12-15 07:07:06 +00004702 assert((CurContext->isDependentContext() || B.builtAll()) &&
4703 "omp for simd loop exprs were not built");
4704
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004705 if (!CurContext->isDependentContext()) {
4706 // Finalize the clauses that need pre-built expressions for CodeGen.
4707 for (auto C : Clauses) {
4708 if (auto LC = dyn_cast<OMPLinearClause>(C))
4709 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4710 B.NumIterations, *this, CurScope))
4711 return StmtError();
4712 }
4713 }
4714
Alexey Bataev66b15b52015-08-21 11:14:16 +00004715 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4716 // If both simdlen and safelen clauses are specified, the value of the simdlen
4717 // parameter must be less than or equal to the value of the safelen parameter.
4718 OMPSafelenClause *Safelen = nullptr;
4719 OMPSimdlenClause *Simdlen = nullptr;
4720 for (auto *Clause : Clauses) {
4721 if (Clause->getClauseKind() == OMPC_safelen)
4722 Safelen = cast<OMPSafelenClause>(Clause);
4723 else if (Clause->getClauseKind() == OMPC_simdlen)
4724 Simdlen = cast<OMPSimdlenClause>(Clause);
4725 if (Safelen && Simdlen)
4726 break;
4727 }
4728 if (Simdlen && Safelen &&
4729 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4730 Safelen->getSafelen()))
4731 return StmtError();
4732
Alexander Musmanf82886e2014-09-18 05:12:34 +00004733 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004734 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4735 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004736}
4737
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004738StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4739 Stmt *AStmt,
4740 SourceLocation StartLoc,
4741 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004742 if (!AStmt)
4743 return StmtError();
4744
4745 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004746 auto BaseStmt = AStmt;
4747 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4748 BaseStmt = CS->getCapturedStmt();
4749 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4750 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004751 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004752 return StmtError();
4753 // All associated statements must be '#pragma omp section' except for
4754 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004755 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004756 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4757 if (SectionStmt)
4758 Diag(SectionStmt->getLocStart(),
4759 diag::err_omp_sections_substmt_not_section);
4760 return StmtError();
4761 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004762 cast<OMPSectionDirective>(SectionStmt)
4763 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004764 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004765 } else {
4766 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4767 return StmtError();
4768 }
4769
4770 getCurFunction()->setHasBranchProtectedScope();
4771
Alexey Bataev25e5b442015-09-15 12:52:43 +00004772 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4773 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004774}
4775
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004776StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4777 SourceLocation StartLoc,
4778 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004779 if (!AStmt)
4780 return StmtError();
4781
4782 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004783
4784 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004785 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004786
Alexey Bataev25e5b442015-09-15 12:52:43 +00004787 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4788 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004789}
4790
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004791StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4792 Stmt *AStmt,
4793 SourceLocation StartLoc,
4794 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004795 if (!AStmt)
4796 return StmtError();
4797
4798 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004799
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004800 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004801
Alexey Bataev3255bf32015-01-19 05:20:46 +00004802 // OpenMP [2.7.3, single Construct, Restrictions]
4803 // The copyprivate clause must not be used with the nowait clause.
4804 OMPClause *Nowait = nullptr;
4805 OMPClause *Copyprivate = nullptr;
4806 for (auto *Clause : Clauses) {
4807 if (Clause->getClauseKind() == OMPC_nowait)
4808 Nowait = Clause;
4809 else if (Clause->getClauseKind() == OMPC_copyprivate)
4810 Copyprivate = Clause;
4811 if (Copyprivate && Nowait) {
4812 Diag(Copyprivate->getLocStart(),
4813 diag::err_omp_single_copyprivate_with_nowait);
4814 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4815 return StmtError();
4816 }
4817 }
4818
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004819 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4820}
4821
Alexander Musman80c22892014-07-17 08:54:58 +00004822StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4823 SourceLocation StartLoc,
4824 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004825 if (!AStmt)
4826 return StmtError();
4827
4828 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004829
4830 getCurFunction()->setHasBranchProtectedScope();
4831
4832 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4833}
4834
Alexey Bataev28c75412015-12-15 08:19:24 +00004835StmtResult Sema::ActOnOpenMPCriticalDirective(
4836 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4837 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004838 if (!AStmt)
4839 return StmtError();
4840
4841 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004842
Alexey Bataev28c75412015-12-15 08:19:24 +00004843 bool ErrorFound = false;
4844 llvm::APSInt Hint;
4845 SourceLocation HintLoc;
4846 bool DependentHint = false;
4847 for (auto *C : Clauses) {
4848 if (C->getClauseKind() == OMPC_hint) {
4849 if (!DirName.getName()) {
4850 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4851 ErrorFound = true;
4852 }
4853 Expr *E = cast<OMPHintClause>(C)->getHint();
4854 if (E->isTypeDependent() || E->isValueDependent() ||
4855 E->isInstantiationDependent())
4856 DependentHint = true;
4857 else {
4858 Hint = E->EvaluateKnownConstInt(Context);
4859 HintLoc = C->getLocStart();
4860 }
4861 }
4862 }
4863 if (ErrorFound)
4864 return StmtError();
4865 auto Pair = DSAStack->getCriticalWithHint(DirName);
4866 if (Pair.first && DirName.getName() && !DependentHint) {
4867 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4868 Diag(StartLoc, diag::err_omp_critical_with_hint);
4869 if (HintLoc.isValid()) {
4870 Diag(HintLoc, diag::note_omp_critical_hint_here)
4871 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4872 } else
4873 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4874 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4875 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4876 << 1
4877 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4878 /*Radix=*/10, /*Signed=*/false);
4879 } else
4880 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4881 }
4882 }
4883
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004884 getCurFunction()->setHasBranchProtectedScope();
4885
Alexey Bataev28c75412015-12-15 08:19:24 +00004886 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4887 Clauses, AStmt);
4888 if (!Pair.first && DirName.getName() && !DependentHint)
4889 DSAStack->addCriticalWithHint(Dir, Hint);
4890 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004891}
4892
Alexey Bataev4acb8592014-07-07 13:01:15 +00004893StmtResult Sema::ActOnOpenMPParallelForDirective(
4894 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4895 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004896 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004897 if (!AStmt)
4898 return StmtError();
4899
Alexey Bataev4acb8592014-07-07 13:01:15 +00004900 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4901 // 1.2.2 OpenMP Language Terminology
4902 // Structured block - An executable statement with a single entry at the
4903 // top and a single exit at the bottom.
4904 // The point of exit cannot be a branch out of the structured block.
4905 // longjmp() and throw() must not violate the entry/exit criteria.
4906 CS->getCapturedDecl()->setNothrow();
4907
Alexander Musmanc6388682014-12-15 07:07:06 +00004908 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004909 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4910 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004911 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004912 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4913 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4914 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004915 if (NestedLoopCount == 0)
4916 return StmtError();
4917
Alexander Musmana5f070a2014-10-01 06:03:56 +00004918 assert((CurContext->isDependentContext() || B.builtAll()) &&
4919 "omp parallel for loop exprs were not built");
4920
Alexey Bataev54acd402015-08-04 11:18:19 +00004921 if (!CurContext->isDependentContext()) {
4922 // Finalize the clauses that need pre-built expressions for CodeGen.
4923 for (auto C : Clauses) {
4924 if (auto LC = dyn_cast<OMPLinearClause>(C))
4925 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4926 B.NumIterations, *this, CurScope))
4927 return StmtError();
4928 }
4929 }
4930
Alexey Bataev4acb8592014-07-07 13:01:15 +00004931 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004932 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004933 NestedLoopCount, Clauses, AStmt, B,
4934 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004935}
4936
Alexander Musmane4e893b2014-09-23 09:33:00 +00004937StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4938 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4939 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004940 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004941 if (!AStmt)
4942 return StmtError();
4943
Alexander Musmane4e893b2014-09-23 09:33:00 +00004944 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4945 // 1.2.2 OpenMP Language Terminology
4946 // Structured block - An executable statement with a single entry at the
4947 // top and a single exit at the bottom.
4948 // The point of exit cannot be a branch out of the structured block.
4949 // longjmp() and throw() must not violate the entry/exit criteria.
4950 CS->getCapturedDecl()->setNothrow();
4951
Alexander Musmanc6388682014-12-15 07:07:06 +00004952 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004953 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4954 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004955 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004956 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4957 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4958 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004959 if (NestedLoopCount == 0)
4960 return StmtError();
4961
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004962 if (!CurContext->isDependentContext()) {
4963 // Finalize the clauses that need pre-built expressions for CodeGen.
4964 for (auto C : Clauses) {
4965 if (auto LC = dyn_cast<OMPLinearClause>(C))
4966 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4967 B.NumIterations, *this, CurScope))
4968 return StmtError();
4969 }
4970 }
4971
Alexey Bataev66b15b52015-08-21 11:14:16 +00004972 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4973 // If both simdlen and safelen clauses are specified, the value of the simdlen
4974 // parameter must be less than or equal to the value of the safelen parameter.
4975 OMPSafelenClause *Safelen = nullptr;
4976 OMPSimdlenClause *Simdlen = nullptr;
4977 for (auto *Clause : Clauses) {
4978 if (Clause->getClauseKind() == OMPC_safelen)
4979 Safelen = cast<OMPSafelenClause>(Clause);
4980 else if (Clause->getClauseKind() == OMPC_simdlen)
4981 Simdlen = cast<OMPSimdlenClause>(Clause);
4982 if (Safelen && Simdlen)
4983 break;
4984 }
4985 if (Simdlen && Safelen &&
4986 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4987 Safelen->getSafelen()))
4988 return StmtError();
4989
Alexander Musmane4e893b2014-09-23 09:33:00 +00004990 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004991 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004992 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004993}
4994
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004995StmtResult
4996Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4997 Stmt *AStmt, SourceLocation StartLoc,
4998 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004999 if (!AStmt)
5000 return StmtError();
5001
5002 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005003 auto BaseStmt = AStmt;
5004 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5005 BaseStmt = CS->getCapturedStmt();
5006 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5007 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005008 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005009 return StmtError();
5010 // All associated statements must be '#pragma omp section' except for
5011 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005012 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005013 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5014 if (SectionStmt)
5015 Diag(SectionStmt->getLocStart(),
5016 diag::err_omp_parallel_sections_substmt_not_section);
5017 return StmtError();
5018 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005019 cast<OMPSectionDirective>(SectionStmt)
5020 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005021 }
5022 } else {
5023 Diag(AStmt->getLocStart(),
5024 diag::err_omp_parallel_sections_not_compound_stmt);
5025 return StmtError();
5026 }
5027
5028 getCurFunction()->setHasBranchProtectedScope();
5029
Alexey Bataev25e5b442015-09-15 12:52:43 +00005030 return OMPParallelSectionsDirective::Create(
5031 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005032}
5033
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005034StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5035 Stmt *AStmt, SourceLocation StartLoc,
5036 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005037 if (!AStmt)
5038 return StmtError();
5039
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005040 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5041 // 1.2.2 OpenMP Language Terminology
5042 // Structured block - An executable statement with a single entry at the
5043 // top and a single exit at the bottom.
5044 // The point of exit cannot be a branch out of the structured block.
5045 // longjmp() and throw() must not violate the entry/exit criteria.
5046 CS->getCapturedDecl()->setNothrow();
5047
5048 getCurFunction()->setHasBranchProtectedScope();
5049
Alexey Bataev25e5b442015-09-15 12:52:43 +00005050 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5051 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005052}
5053
Alexey Bataev68446b72014-07-18 07:47:19 +00005054StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5055 SourceLocation EndLoc) {
5056 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5057}
5058
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005059StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5060 SourceLocation EndLoc) {
5061 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5062}
5063
Alexey Bataev2df347a2014-07-18 10:17:07 +00005064StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5065 SourceLocation EndLoc) {
5066 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5067}
5068
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005069StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5070 SourceLocation StartLoc,
5071 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005072 if (!AStmt)
5073 return StmtError();
5074
5075 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005076
5077 getCurFunction()->setHasBranchProtectedScope();
5078
5079 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5080}
5081
Alexey Bataev6125da92014-07-21 11:26:11 +00005082StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5083 SourceLocation StartLoc,
5084 SourceLocation EndLoc) {
5085 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5086 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5087}
5088
Alexey Bataev346265e2015-09-25 10:37:12 +00005089StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5090 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005091 SourceLocation StartLoc,
5092 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005093 OMPClause *DependFound = nullptr;
5094 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005095 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005096 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005097 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005098 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005099 for (auto *C : Clauses) {
5100 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5101 DependFound = C;
5102 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5103 if (DependSourceClause) {
5104 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5105 << getOpenMPDirectiveName(OMPD_ordered)
5106 << getOpenMPClauseName(OMPC_depend) << 2;
5107 ErrorFound = true;
5108 } else
5109 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005110 if (DependSinkClause) {
5111 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5112 << 0;
5113 ErrorFound = true;
5114 }
5115 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5116 if (DependSourceClause) {
5117 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5118 << 1;
5119 ErrorFound = true;
5120 }
5121 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005122 }
5123 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005124 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005125 else if (C->getClauseKind() == OMPC_simd)
5126 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005127 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005128 if (!ErrorFound && !SC &&
5129 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005130 // OpenMP [2.8.1,simd Construct, Restrictions]
5131 // An ordered construct with the simd clause is the only OpenMP construct
5132 // that can appear in the simd region.
5133 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005134 ErrorFound = true;
5135 } else if (DependFound && (TC || SC)) {
5136 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5137 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5138 ErrorFound = true;
5139 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5140 Diag(DependFound->getLocStart(),
5141 diag::err_omp_ordered_directive_without_param);
5142 ErrorFound = true;
5143 } else if (TC || Clauses.empty()) {
5144 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5145 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5146 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5147 << (TC != nullptr);
5148 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5149 ErrorFound = true;
5150 }
5151 }
5152 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005153 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005154
5155 if (AStmt) {
5156 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5157
5158 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005159 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005160
5161 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005162}
5163
Alexey Bataev1d160b12015-03-13 12:27:31 +00005164namespace {
5165/// \brief Helper class for checking expression in 'omp atomic [update]'
5166/// construct.
5167class OpenMPAtomicUpdateChecker {
5168 /// \brief Error results for atomic update expressions.
5169 enum ExprAnalysisErrorCode {
5170 /// \brief A statement is not an expression statement.
5171 NotAnExpression,
5172 /// \brief Expression is not builtin binary or unary operation.
5173 NotABinaryOrUnaryExpression,
5174 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5175 NotAnUnaryIncDecExpression,
5176 /// \brief An expression is not of scalar type.
5177 NotAScalarType,
5178 /// \brief A binary operation is not an assignment operation.
5179 NotAnAssignmentOp,
5180 /// \brief RHS part of the binary operation is not a binary expression.
5181 NotABinaryExpression,
5182 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5183 /// expression.
5184 NotABinaryOperator,
5185 /// \brief RHS binary operation does not have reference to the updated LHS
5186 /// part.
5187 NotAnUpdateExpression,
5188 /// \brief No errors is found.
5189 NoError
5190 };
5191 /// \brief Reference to Sema.
5192 Sema &SemaRef;
5193 /// \brief A location for note diagnostics (when error is found).
5194 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005195 /// \brief 'x' lvalue part of the source atomic expression.
5196 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005197 /// \brief 'expr' rvalue part of the source atomic expression.
5198 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005199 /// \brief Helper expression of the form
5200 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5201 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5202 Expr *UpdateExpr;
5203 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5204 /// important for non-associative operations.
5205 bool IsXLHSInRHSPart;
5206 BinaryOperatorKind Op;
5207 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005208 /// \brief true if the source expression is a postfix unary operation, false
5209 /// if it is a prefix unary operation.
5210 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005211
5212public:
5213 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005214 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005215 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005216 /// \brief Check specified statement that it is suitable for 'atomic update'
5217 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005218 /// expression. If DiagId and NoteId == 0, then only check is performed
5219 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005220 /// \param DiagId Diagnostic which should be emitted if error is found.
5221 /// \param NoteId Diagnostic note for the main error message.
5222 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005223 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005224 /// \brief Return the 'x' lvalue part of the source atomic expression.
5225 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005226 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5227 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005228 /// \brief Return the update expression used in calculation of the updated
5229 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5230 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5231 Expr *getUpdateExpr() const { return UpdateExpr; }
5232 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5233 /// false otherwise.
5234 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5235
Alexey Bataevb78ca832015-04-01 03:33:17 +00005236 /// \brief true if the source expression is a postfix unary operation, false
5237 /// if it is a prefix unary operation.
5238 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5239
Alexey Bataev1d160b12015-03-13 12:27:31 +00005240private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005241 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5242 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005243};
5244} // namespace
5245
5246bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5247 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5248 ExprAnalysisErrorCode ErrorFound = NoError;
5249 SourceLocation ErrorLoc, NoteLoc;
5250 SourceRange ErrorRange, NoteRange;
5251 // Allowed constructs are:
5252 // x = x binop expr;
5253 // x = expr binop x;
5254 if (AtomicBinOp->getOpcode() == BO_Assign) {
5255 X = AtomicBinOp->getLHS();
5256 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5257 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5258 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5259 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5260 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005261 Op = AtomicInnerBinOp->getOpcode();
5262 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005263 auto *LHS = AtomicInnerBinOp->getLHS();
5264 auto *RHS = AtomicInnerBinOp->getRHS();
5265 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5266 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5267 /*Canonical=*/true);
5268 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5269 /*Canonical=*/true);
5270 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5271 /*Canonical=*/true);
5272 if (XId == LHSId) {
5273 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005274 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005275 } else if (XId == RHSId) {
5276 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005277 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005278 } else {
5279 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5280 ErrorRange = AtomicInnerBinOp->getSourceRange();
5281 NoteLoc = X->getExprLoc();
5282 NoteRange = X->getSourceRange();
5283 ErrorFound = NotAnUpdateExpression;
5284 }
5285 } else {
5286 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5287 ErrorRange = AtomicInnerBinOp->getSourceRange();
5288 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5289 NoteRange = SourceRange(NoteLoc, NoteLoc);
5290 ErrorFound = NotABinaryOperator;
5291 }
5292 } else {
5293 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5294 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5295 ErrorFound = NotABinaryExpression;
5296 }
5297 } else {
5298 ErrorLoc = AtomicBinOp->getExprLoc();
5299 ErrorRange = AtomicBinOp->getSourceRange();
5300 NoteLoc = AtomicBinOp->getOperatorLoc();
5301 NoteRange = SourceRange(NoteLoc, NoteLoc);
5302 ErrorFound = NotAnAssignmentOp;
5303 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005304 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005305 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5306 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5307 return true;
5308 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005309 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005310 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005311}
5312
5313bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5314 unsigned NoteId) {
5315 ExprAnalysisErrorCode ErrorFound = NoError;
5316 SourceLocation ErrorLoc, NoteLoc;
5317 SourceRange ErrorRange, NoteRange;
5318 // Allowed constructs are:
5319 // x++;
5320 // x--;
5321 // ++x;
5322 // --x;
5323 // x binop= expr;
5324 // x = x binop expr;
5325 // x = expr binop x;
5326 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5327 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5328 if (AtomicBody->getType()->isScalarType() ||
5329 AtomicBody->isInstantiationDependent()) {
5330 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5331 AtomicBody->IgnoreParenImpCasts())) {
5332 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005333 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005334 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005335 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005336 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005337 X = AtomicCompAssignOp->getLHS();
5338 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005339 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5340 AtomicBody->IgnoreParenImpCasts())) {
5341 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005342 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5343 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005344 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005345 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5346 // Check for Unary Operation
5347 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005348 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005349 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5350 OpLoc = AtomicUnaryOp->getOperatorLoc();
5351 X = AtomicUnaryOp->getSubExpr();
5352 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5353 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005354 } else {
5355 ErrorFound = NotAnUnaryIncDecExpression;
5356 ErrorLoc = AtomicUnaryOp->getExprLoc();
5357 ErrorRange = AtomicUnaryOp->getSourceRange();
5358 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5359 NoteRange = SourceRange(NoteLoc, NoteLoc);
5360 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005361 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005362 ErrorFound = NotABinaryOrUnaryExpression;
5363 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5364 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5365 }
5366 } else {
5367 ErrorFound = NotAScalarType;
5368 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5369 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5370 }
5371 } else {
5372 ErrorFound = NotAnExpression;
5373 NoteLoc = ErrorLoc = S->getLocStart();
5374 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5375 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005376 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005377 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5378 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5379 return true;
5380 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005381 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005382 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005383 // Build an update expression of form 'OpaqueValueExpr(x) binop
5384 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5385 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5386 auto *OVEX = new (SemaRef.getASTContext())
5387 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5388 auto *OVEExpr = new (SemaRef.getASTContext())
5389 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5390 auto Update =
5391 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5392 IsXLHSInRHSPart ? OVEExpr : OVEX);
5393 if (Update.isInvalid())
5394 return true;
5395 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5396 Sema::AA_Casting);
5397 if (Update.isInvalid())
5398 return true;
5399 UpdateExpr = Update.get();
5400 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005401 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005402}
5403
Alexey Bataev0162e452014-07-22 10:10:35 +00005404StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5405 Stmt *AStmt,
5406 SourceLocation StartLoc,
5407 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005408 if (!AStmt)
5409 return StmtError();
5410
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005411 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005412 // 1.2.2 OpenMP Language Terminology
5413 // Structured block - An executable statement with a single entry at the
5414 // top and a single exit at the bottom.
5415 // The point of exit cannot be a branch out of the structured block.
5416 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005417 OpenMPClauseKind AtomicKind = OMPC_unknown;
5418 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005419 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005420 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005421 C->getClauseKind() == OMPC_update ||
5422 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005423 if (AtomicKind != OMPC_unknown) {
5424 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5425 << SourceRange(C->getLocStart(), C->getLocEnd());
5426 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5427 << getOpenMPClauseName(AtomicKind);
5428 } else {
5429 AtomicKind = C->getClauseKind();
5430 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005431 }
5432 }
5433 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005434
Alexey Bataev459dec02014-07-24 06:46:57 +00005435 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005436 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5437 Body = EWC->getSubExpr();
5438
Alexey Bataev62cec442014-11-18 10:14:22 +00005439 Expr *X = nullptr;
5440 Expr *V = nullptr;
5441 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005442 Expr *UE = nullptr;
5443 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005444 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005445 // OpenMP [2.12.6, atomic Construct]
5446 // In the next expressions:
5447 // * x and v (as applicable) are both l-value expressions with scalar type.
5448 // * During the execution of an atomic region, multiple syntactic
5449 // occurrences of x must designate the same storage location.
5450 // * Neither of v and expr (as applicable) may access the storage location
5451 // designated by x.
5452 // * Neither of x and expr (as applicable) may access the storage location
5453 // designated by v.
5454 // * expr is an expression with scalar type.
5455 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5456 // * binop, binop=, ++, and -- are not overloaded operators.
5457 // * The expression x binop expr must be numerically equivalent to x binop
5458 // (expr). This requirement is satisfied if the operators in expr have
5459 // precedence greater than binop, or by using parentheses around expr or
5460 // subexpressions of expr.
5461 // * The expression expr binop x must be numerically equivalent to (expr)
5462 // binop x. This requirement is satisfied if the operators in expr have
5463 // precedence equal to or greater than binop, or by using parentheses around
5464 // expr or subexpressions of expr.
5465 // * For forms that allow multiple occurrences of x, the number of times
5466 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005467 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005468 enum {
5469 NotAnExpression,
5470 NotAnAssignmentOp,
5471 NotAScalarType,
5472 NotAnLValue,
5473 NoError
5474 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005475 SourceLocation ErrorLoc, NoteLoc;
5476 SourceRange ErrorRange, NoteRange;
5477 // If clause is read:
5478 // v = x;
5479 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5480 auto AtomicBinOp =
5481 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5482 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5483 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5484 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5485 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5486 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5487 if (!X->isLValue() || !V->isLValue()) {
5488 auto NotLValueExpr = X->isLValue() ? V : X;
5489 ErrorFound = NotAnLValue;
5490 ErrorLoc = AtomicBinOp->getExprLoc();
5491 ErrorRange = AtomicBinOp->getSourceRange();
5492 NoteLoc = NotLValueExpr->getExprLoc();
5493 NoteRange = NotLValueExpr->getSourceRange();
5494 }
5495 } else if (!X->isInstantiationDependent() ||
5496 !V->isInstantiationDependent()) {
5497 auto NotScalarExpr =
5498 (X->isInstantiationDependent() || X->getType()->isScalarType())
5499 ? V
5500 : X;
5501 ErrorFound = NotAScalarType;
5502 ErrorLoc = AtomicBinOp->getExprLoc();
5503 ErrorRange = AtomicBinOp->getSourceRange();
5504 NoteLoc = NotScalarExpr->getExprLoc();
5505 NoteRange = NotScalarExpr->getSourceRange();
5506 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005507 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005508 ErrorFound = NotAnAssignmentOp;
5509 ErrorLoc = AtomicBody->getExprLoc();
5510 ErrorRange = AtomicBody->getSourceRange();
5511 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5512 : AtomicBody->getExprLoc();
5513 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5514 : AtomicBody->getSourceRange();
5515 }
5516 } else {
5517 ErrorFound = NotAnExpression;
5518 NoteLoc = ErrorLoc = Body->getLocStart();
5519 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005520 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005521 if (ErrorFound != NoError) {
5522 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5523 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005524 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5525 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005526 return StmtError();
5527 } else if (CurContext->isDependentContext())
5528 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005529 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005530 enum {
5531 NotAnExpression,
5532 NotAnAssignmentOp,
5533 NotAScalarType,
5534 NotAnLValue,
5535 NoError
5536 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005537 SourceLocation ErrorLoc, NoteLoc;
5538 SourceRange ErrorRange, NoteRange;
5539 // If clause is write:
5540 // x = expr;
5541 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5542 auto AtomicBinOp =
5543 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5544 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005545 X = AtomicBinOp->getLHS();
5546 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005547 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5548 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5549 if (!X->isLValue()) {
5550 ErrorFound = NotAnLValue;
5551 ErrorLoc = AtomicBinOp->getExprLoc();
5552 ErrorRange = AtomicBinOp->getSourceRange();
5553 NoteLoc = X->getExprLoc();
5554 NoteRange = X->getSourceRange();
5555 }
5556 } else if (!X->isInstantiationDependent() ||
5557 !E->isInstantiationDependent()) {
5558 auto NotScalarExpr =
5559 (X->isInstantiationDependent() || X->getType()->isScalarType())
5560 ? E
5561 : X;
5562 ErrorFound = NotAScalarType;
5563 ErrorLoc = AtomicBinOp->getExprLoc();
5564 ErrorRange = AtomicBinOp->getSourceRange();
5565 NoteLoc = NotScalarExpr->getExprLoc();
5566 NoteRange = NotScalarExpr->getSourceRange();
5567 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005568 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005569 ErrorFound = NotAnAssignmentOp;
5570 ErrorLoc = AtomicBody->getExprLoc();
5571 ErrorRange = AtomicBody->getSourceRange();
5572 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5573 : AtomicBody->getExprLoc();
5574 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5575 : AtomicBody->getSourceRange();
5576 }
5577 } else {
5578 ErrorFound = NotAnExpression;
5579 NoteLoc = ErrorLoc = Body->getLocStart();
5580 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005581 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005582 if (ErrorFound != NoError) {
5583 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5584 << ErrorRange;
5585 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5586 << NoteRange;
5587 return StmtError();
5588 } else if (CurContext->isDependentContext())
5589 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005590 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005591 // If clause is update:
5592 // x++;
5593 // x--;
5594 // ++x;
5595 // --x;
5596 // x binop= expr;
5597 // x = x binop expr;
5598 // x = expr binop x;
5599 OpenMPAtomicUpdateChecker Checker(*this);
5600 if (Checker.checkStatement(
5601 Body, (AtomicKind == OMPC_update)
5602 ? diag::err_omp_atomic_update_not_expression_statement
5603 : diag::err_omp_atomic_not_expression_statement,
5604 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005605 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005606 if (!CurContext->isDependentContext()) {
5607 E = Checker.getExpr();
5608 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005609 UE = Checker.getUpdateExpr();
5610 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005611 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005612 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005613 enum {
5614 NotAnAssignmentOp,
5615 NotACompoundStatement,
5616 NotTwoSubstatements,
5617 NotASpecificExpression,
5618 NoError
5619 } ErrorFound = NoError;
5620 SourceLocation ErrorLoc, NoteLoc;
5621 SourceRange ErrorRange, NoteRange;
5622 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5623 // If clause is a capture:
5624 // v = x++;
5625 // v = x--;
5626 // v = ++x;
5627 // v = --x;
5628 // v = x binop= expr;
5629 // v = x = x binop expr;
5630 // v = x = expr binop x;
5631 auto *AtomicBinOp =
5632 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5633 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5634 V = AtomicBinOp->getLHS();
5635 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5636 OpenMPAtomicUpdateChecker Checker(*this);
5637 if (Checker.checkStatement(
5638 Body, diag::err_omp_atomic_capture_not_expression_statement,
5639 diag::note_omp_atomic_update))
5640 return StmtError();
5641 E = Checker.getExpr();
5642 X = Checker.getX();
5643 UE = Checker.getUpdateExpr();
5644 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5645 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005646 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005647 ErrorLoc = AtomicBody->getExprLoc();
5648 ErrorRange = AtomicBody->getSourceRange();
5649 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5650 : AtomicBody->getExprLoc();
5651 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5652 : AtomicBody->getSourceRange();
5653 ErrorFound = NotAnAssignmentOp;
5654 }
5655 if (ErrorFound != NoError) {
5656 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5657 << ErrorRange;
5658 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5659 return StmtError();
5660 } else if (CurContext->isDependentContext()) {
5661 UE = V = E = X = nullptr;
5662 }
5663 } else {
5664 // If clause is a capture:
5665 // { v = x; x = expr; }
5666 // { v = x; x++; }
5667 // { v = x; x--; }
5668 // { v = x; ++x; }
5669 // { v = x; --x; }
5670 // { v = x; x binop= expr; }
5671 // { v = x; x = x binop expr; }
5672 // { v = x; x = expr binop x; }
5673 // { x++; v = x; }
5674 // { x--; v = x; }
5675 // { ++x; v = x; }
5676 // { --x; v = x; }
5677 // { x binop= expr; v = x; }
5678 // { x = x binop expr; v = x; }
5679 // { x = expr binop x; v = x; }
5680 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5681 // Check that this is { expr1; expr2; }
5682 if (CS->size() == 2) {
5683 auto *First = CS->body_front();
5684 auto *Second = CS->body_back();
5685 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5686 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5687 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5688 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5689 // Need to find what subexpression is 'v' and what is 'x'.
5690 OpenMPAtomicUpdateChecker Checker(*this);
5691 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5692 BinaryOperator *BinOp = nullptr;
5693 if (IsUpdateExprFound) {
5694 BinOp = dyn_cast<BinaryOperator>(First);
5695 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5696 }
5697 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5698 // { v = x; x++; }
5699 // { v = x; x--; }
5700 // { v = x; ++x; }
5701 // { v = x; --x; }
5702 // { v = x; x binop= expr; }
5703 // { v = x; x = x binop expr; }
5704 // { v = x; x = expr binop x; }
5705 // Check that the first expression has form v = x.
5706 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5707 llvm::FoldingSetNodeID XId, PossibleXId;
5708 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5709 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5710 IsUpdateExprFound = XId == PossibleXId;
5711 if (IsUpdateExprFound) {
5712 V = BinOp->getLHS();
5713 X = Checker.getX();
5714 E = Checker.getExpr();
5715 UE = Checker.getUpdateExpr();
5716 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005717 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005718 }
5719 }
5720 if (!IsUpdateExprFound) {
5721 IsUpdateExprFound = !Checker.checkStatement(First);
5722 BinOp = nullptr;
5723 if (IsUpdateExprFound) {
5724 BinOp = dyn_cast<BinaryOperator>(Second);
5725 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5726 }
5727 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5728 // { x++; v = x; }
5729 // { x--; v = x; }
5730 // { ++x; v = x; }
5731 // { --x; v = x; }
5732 // { x binop= expr; v = x; }
5733 // { x = x binop expr; v = x; }
5734 // { x = expr binop x; v = x; }
5735 // Check that the second expression has form v = x.
5736 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5737 llvm::FoldingSetNodeID XId, PossibleXId;
5738 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5739 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5740 IsUpdateExprFound = XId == PossibleXId;
5741 if (IsUpdateExprFound) {
5742 V = BinOp->getLHS();
5743 X = Checker.getX();
5744 E = Checker.getExpr();
5745 UE = Checker.getUpdateExpr();
5746 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005747 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005748 }
5749 }
5750 }
5751 if (!IsUpdateExprFound) {
5752 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005753 auto *FirstExpr = dyn_cast<Expr>(First);
5754 auto *SecondExpr = dyn_cast<Expr>(Second);
5755 if (!FirstExpr || !SecondExpr ||
5756 !(FirstExpr->isInstantiationDependent() ||
5757 SecondExpr->isInstantiationDependent())) {
5758 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5759 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005760 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005761 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5762 : First->getLocStart();
5763 NoteRange = ErrorRange = FirstBinOp
5764 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005765 : SourceRange(ErrorLoc, ErrorLoc);
5766 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005767 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5768 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5769 ErrorFound = NotAnAssignmentOp;
5770 NoteLoc = ErrorLoc = SecondBinOp
5771 ? SecondBinOp->getOperatorLoc()
5772 : Second->getLocStart();
5773 NoteRange = ErrorRange =
5774 SecondBinOp ? SecondBinOp->getSourceRange()
5775 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005776 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005777 auto *PossibleXRHSInFirst =
5778 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5779 auto *PossibleXLHSInSecond =
5780 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5781 llvm::FoldingSetNodeID X1Id, X2Id;
5782 PossibleXRHSInFirst->Profile(X1Id, Context,
5783 /*Canonical=*/true);
5784 PossibleXLHSInSecond->Profile(X2Id, Context,
5785 /*Canonical=*/true);
5786 IsUpdateExprFound = X1Id == X2Id;
5787 if (IsUpdateExprFound) {
5788 V = FirstBinOp->getLHS();
5789 X = SecondBinOp->getLHS();
5790 E = SecondBinOp->getRHS();
5791 UE = nullptr;
5792 IsXLHSInRHSPart = false;
5793 IsPostfixUpdate = true;
5794 } else {
5795 ErrorFound = NotASpecificExpression;
5796 ErrorLoc = FirstBinOp->getExprLoc();
5797 ErrorRange = FirstBinOp->getSourceRange();
5798 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5799 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5800 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005801 }
5802 }
5803 }
5804 }
5805 } else {
5806 NoteLoc = ErrorLoc = Body->getLocStart();
5807 NoteRange = ErrorRange =
5808 SourceRange(Body->getLocStart(), Body->getLocStart());
5809 ErrorFound = NotTwoSubstatements;
5810 }
5811 } else {
5812 NoteLoc = ErrorLoc = Body->getLocStart();
5813 NoteRange = ErrorRange =
5814 SourceRange(Body->getLocStart(), Body->getLocStart());
5815 ErrorFound = NotACompoundStatement;
5816 }
5817 if (ErrorFound != NoError) {
5818 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5819 << ErrorRange;
5820 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5821 return StmtError();
5822 } else if (CurContext->isDependentContext()) {
5823 UE = V = E = X = nullptr;
5824 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005825 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005826 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005827
5828 getCurFunction()->setHasBranchProtectedScope();
5829
Alexey Bataev62cec442014-11-18 10:14:22 +00005830 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005831 X, V, E, UE, IsXLHSInRHSPart,
5832 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005833}
5834
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005835StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5836 Stmt *AStmt,
5837 SourceLocation StartLoc,
5838 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005839 if (!AStmt)
5840 return StmtError();
5841
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005842 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5843 // 1.2.2 OpenMP Language Terminology
5844 // Structured block - An executable statement with a single entry at the
5845 // top and a single exit at the bottom.
5846 // The point of exit cannot be a branch out of the structured block.
5847 // longjmp() and throw() must not violate the entry/exit criteria.
5848 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005849
Alexey Bataev13314bf2014-10-09 04:18:56 +00005850 // OpenMP [2.16, Nesting of Regions]
5851 // If specified, a teams construct must be contained within a target
5852 // construct. That target construct must contain no statements or directives
5853 // outside of the teams construct.
5854 if (DSAStack->hasInnerTeamsRegion()) {
5855 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5856 bool OMPTeamsFound = true;
5857 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5858 auto I = CS->body_begin();
5859 while (I != CS->body_end()) {
5860 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5861 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5862 OMPTeamsFound = false;
5863 break;
5864 }
5865 ++I;
5866 }
5867 assert(I != CS->body_end() && "Not found statement");
5868 S = *I;
5869 }
5870 if (!OMPTeamsFound) {
5871 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5872 Diag(DSAStack->getInnerTeamsRegionLoc(),
5873 diag::note_omp_nested_teams_construct_here);
5874 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5875 << isa<OMPExecutableDirective>(S);
5876 return StmtError();
5877 }
5878 }
5879
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005880 getCurFunction()->setHasBranchProtectedScope();
5881
5882 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5883}
5884
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005885StmtResult
5886Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5887 Stmt *AStmt, SourceLocation StartLoc,
5888 SourceLocation EndLoc) {
5889 if (!AStmt)
5890 return StmtError();
5891
5892 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5893 // 1.2.2 OpenMP Language Terminology
5894 // Structured block - An executable statement with a single entry at the
5895 // top and a single exit at the bottom.
5896 // The point of exit cannot be a branch out of the structured block.
5897 // longjmp() and throw() must not violate the entry/exit criteria.
5898 CS->getCapturedDecl()->setNothrow();
5899
5900 getCurFunction()->setHasBranchProtectedScope();
5901
5902 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5903 AStmt);
5904}
5905
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005906StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5907 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5908 SourceLocation EndLoc,
5909 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5910 if (!AStmt)
5911 return StmtError();
5912
5913 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5914 // 1.2.2 OpenMP Language Terminology
5915 // Structured block - An executable statement with a single entry at the
5916 // top and a single exit at the bottom.
5917 // The point of exit cannot be a branch out of the structured block.
5918 // longjmp() and throw() must not violate the entry/exit criteria.
5919 CS->getCapturedDecl()->setNothrow();
5920
5921 OMPLoopDirective::HelperExprs B;
5922 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5923 // define the nested loops number.
5924 unsigned NestedLoopCount =
5925 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5926 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5927 VarsWithImplicitDSA, B);
5928 if (NestedLoopCount == 0)
5929 return StmtError();
5930
5931 assert((CurContext->isDependentContext() || B.builtAll()) &&
5932 "omp target parallel for loop exprs were not built");
5933
5934 if (!CurContext->isDependentContext()) {
5935 // Finalize the clauses that need pre-built expressions for CodeGen.
5936 for (auto C : Clauses) {
5937 if (auto LC = dyn_cast<OMPLinearClause>(C))
5938 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5939 B.NumIterations, *this, CurScope))
5940 return StmtError();
5941 }
5942 }
5943
5944 getCurFunction()->setHasBranchProtectedScope();
5945 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5946 NestedLoopCount, Clauses, AStmt,
5947 B, DSAStack->isCancelRegion());
5948}
5949
Samuel Antaodf67fc42016-01-19 19:15:56 +00005950/// \brief Check for existence of a map clause in the list of clauses.
5951static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5952 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5953 I != E; ++I) {
5954 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5955 return true;
5956 }
5957 }
5958
5959 return false;
5960}
5961
Michael Wong65f367f2015-07-21 13:44:28 +00005962StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5963 Stmt *AStmt,
5964 SourceLocation StartLoc,
5965 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005966 if (!AStmt)
5967 return StmtError();
5968
5969 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5970
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005971 // OpenMP [2.10.1, Restrictions, p. 97]
5972 // At least one map clause must appear on the directive.
5973 if (!HasMapClause(Clauses)) {
5974 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5975 getOpenMPDirectiveName(OMPD_target_data);
5976 return StmtError();
5977 }
5978
Michael Wong65f367f2015-07-21 13:44:28 +00005979 getCurFunction()->setHasBranchProtectedScope();
5980
5981 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5982 AStmt);
5983}
5984
Samuel Antaodf67fc42016-01-19 19:15:56 +00005985StmtResult
5986Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5987 SourceLocation StartLoc,
5988 SourceLocation EndLoc) {
5989 // OpenMP [2.10.2, Restrictions, p. 99]
5990 // At least one map clause must appear on the directive.
5991 if (!HasMapClause(Clauses)) {
5992 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5993 << getOpenMPDirectiveName(OMPD_target_enter_data);
5994 return StmtError();
5995 }
5996
5997 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5998 Clauses);
5999}
6000
Samuel Antao72590762016-01-19 20:04:50 +00006001StmtResult
6002Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6003 SourceLocation StartLoc,
6004 SourceLocation EndLoc) {
6005 // OpenMP [2.10.3, Restrictions, p. 102]
6006 // At least one map clause must appear on the directive.
6007 if (!HasMapClause(Clauses)) {
6008 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6009 << getOpenMPDirectiveName(OMPD_target_exit_data);
6010 return StmtError();
6011 }
6012
6013 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6014}
6015
Alexey Bataev13314bf2014-10-09 04:18:56 +00006016StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6017 Stmt *AStmt, SourceLocation StartLoc,
6018 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006019 if (!AStmt)
6020 return StmtError();
6021
Alexey Bataev13314bf2014-10-09 04:18:56 +00006022 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6023 // 1.2.2 OpenMP Language Terminology
6024 // Structured block - An executable statement with a single entry at the
6025 // top and a single exit at the bottom.
6026 // The point of exit cannot be a branch out of the structured block.
6027 // longjmp() and throw() must not violate the entry/exit criteria.
6028 CS->getCapturedDecl()->setNothrow();
6029
6030 getCurFunction()->setHasBranchProtectedScope();
6031
6032 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6033}
6034
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006035StmtResult
6036Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6037 SourceLocation EndLoc,
6038 OpenMPDirectiveKind CancelRegion) {
6039 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6040 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6041 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6042 << getOpenMPDirectiveName(CancelRegion);
6043 return StmtError();
6044 }
6045 if (DSAStack->isParentNowaitRegion()) {
6046 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6047 return StmtError();
6048 }
6049 if (DSAStack->isParentOrderedRegion()) {
6050 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6051 return StmtError();
6052 }
6053 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6054 CancelRegion);
6055}
6056
Alexey Bataev87933c72015-09-18 08:07:34 +00006057StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6058 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006059 SourceLocation EndLoc,
6060 OpenMPDirectiveKind CancelRegion) {
6061 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6062 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6063 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6064 << getOpenMPDirectiveName(CancelRegion);
6065 return StmtError();
6066 }
6067 if (DSAStack->isParentNowaitRegion()) {
6068 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6069 return StmtError();
6070 }
6071 if (DSAStack->isParentOrderedRegion()) {
6072 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6073 return StmtError();
6074 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006075 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006076 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6077 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006078}
6079
Alexey Bataev382967a2015-12-08 12:06:20 +00006080static bool checkGrainsizeNumTasksClauses(Sema &S,
6081 ArrayRef<OMPClause *> Clauses) {
6082 OMPClause *PrevClause = nullptr;
6083 bool ErrorFound = false;
6084 for (auto *C : Clauses) {
6085 if (C->getClauseKind() == OMPC_grainsize ||
6086 C->getClauseKind() == OMPC_num_tasks) {
6087 if (!PrevClause)
6088 PrevClause = C;
6089 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6090 S.Diag(C->getLocStart(),
6091 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6092 << getOpenMPClauseName(C->getClauseKind())
6093 << getOpenMPClauseName(PrevClause->getClauseKind());
6094 S.Diag(PrevClause->getLocStart(),
6095 diag::note_omp_previous_grainsize_num_tasks)
6096 << getOpenMPClauseName(PrevClause->getClauseKind());
6097 ErrorFound = true;
6098 }
6099 }
6100 }
6101 return ErrorFound;
6102}
6103
Alexey Bataev49f6e782015-12-01 04:18:41 +00006104StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6105 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6106 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006107 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006108 if (!AStmt)
6109 return StmtError();
6110
6111 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6112 OMPLoopDirective::HelperExprs B;
6113 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6114 // define the nested loops number.
6115 unsigned NestedLoopCount =
6116 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006117 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006118 VarsWithImplicitDSA, B);
6119 if (NestedLoopCount == 0)
6120 return StmtError();
6121
6122 assert((CurContext->isDependentContext() || B.builtAll()) &&
6123 "omp for loop exprs were not built");
6124
Alexey Bataev382967a2015-12-08 12:06:20 +00006125 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6126 // The grainsize clause and num_tasks clause are mutually exclusive and may
6127 // not appear on the same taskloop directive.
6128 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6129 return StmtError();
6130
Alexey Bataev49f6e782015-12-01 04:18:41 +00006131 getCurFunction()->setHasBranchProtectedScope();
6132 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6133 NestedLoopCount, Clauses, AStmt, B);
6134}
6135
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006136StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6137 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6138 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006139 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006140 if (!AStmt)
6141 return StmtError();
6142
6143 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6144 OMPLoopDirective::HelperExprs B;
6145 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6146 // define the nested loops number.
6147 unsigned NestedLoopCount =
6148 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6149 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6150 VarsWithImplicitDSA, B);
6151 if (NestedLoopCount == 0)
6152 return StmtError();
6153
6154 assert((CurContext->isDependentContext() || B.builtAll()) &&
6155 "omp for loop exprs were not built");
6156
Alexey Bataev5a3af132016-03-29 08:58:54 +00006157 if (!CurContext->isDependentContext()) {
6158 // Finalize the clauses that need pre-built expressions for CodeGen.
6159 for (auto C : Clauses) {
6160 if (auto LC = dyn_cast<OMPLinearClause>(C))
6161 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6162 B.NumIterations, *this, CurScope))
6163 return StmtError();
6164 }
6165 }
6166
Alexey Bataev382967a2015-12-08 12:06:20 +00006167 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6168 // The grainsize clause and num_tasks clause are mutually exclusive and may
6169 // not appear on the same taskloop directive.
6170 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6171 return StmtError();
6172
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006173 getCurFunction()->setHasBranchProtectedScope();
6174 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6175 NestedLoopCount, Clauses, AStmt, B);
6176}
6177
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006178StmtResult Sema::ActOnOpenMPDistributeDirective(
6179 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6180 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006181 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006182 if (!AStmt)
6183 return StmtError();
6184
6185 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6186 OMPLoopDirective::HelperExprs B;
6187 // In presence of clause 'collapse' with number of loops, it will
6188 // define the nested loops number.
6189 unsigned NestedLoopCount =
6190 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6191 nullptr /*ordered not a clause on distribute*/, AStmt,
6192 *this, *DSAStack, VarsWithImplicitDSA, B);
6193 if (NestedLoopCount == 0)
6194 return StmtError();
6195
6196 assert((CurContext->isDependentContext() || B.builtAll()) &&
6197 "omp for loop exprs were not built");
6198
6199 getCurFunction()->setHasBranchProtectedScope();
6200 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6201 NestedLoopCount, Clauses, AStmt, B);
6202}
6203
Alexey Bataeved09d242014-05-28 05:53:51 +00006204OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006205 SourceLocation StartLoc,
6206 SourceLocation LParenLoc,
6207 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006208 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006209 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006210 case OMPC_final:
6211 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6212 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006213 case OMPC_num_threads:
6214 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6215 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006216 case OMPC_safelen:
6217 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6218 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006219 case OMPC_simdlen:
6220 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6221 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006222 case OMPC_collapse:
6223 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6224 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006225 case OMPC_ordered:
6226 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6227 break;
Michael Wonge710d542015-08-07 16:16:36 +00006228 case OMPC_device:
6229 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6230 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006231 case OMPC_num_teams:
6232 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6233 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006234 case OMPC_thread_limit:
6235 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6236 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006237 case OMPC_priority:
6238 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6239 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006240 case OMPC_grainsize:
6241 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6242 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006243 case OMPC_num_tasks:
6244 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6245 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006246 case OMPC_hint:
6247 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6248 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006249 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006250 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006251 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006252 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006253 case OMPC_private:
6254 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006255 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006256 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006257 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006258 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006259 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006260 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006261 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006262 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006263 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006264 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006265 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006266 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006267 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006268 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006269 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006270 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006271 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006272 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006273 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006274 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006275 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006276 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006277 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006278 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006279 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006280 llvm_unreachable("Clause is not allowed.");
6281 }
6282 return Res;
6283}
6284
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006285OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6286 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006287 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006288 SourceLocation NameModifierLoc,
6289 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006290 SourceLocation EndLoc) {
6291 Expr *ValExpr = Condition;
6292 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6293 !Condition->isInstantiationDependent() &&
6294 !Condition->containsUnexpandedParameterPack()) {
6295 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006296 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006297 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006298 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006299
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006300 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006301 }
6302
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006303 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6304 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006305}
6306
Alexey Bataev3778b602014-07-17 07:32:53 +00006307OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6308 SourceLocation StartLoc,
6309 SourceLocation LParenLoc,
6310 SourceLocation EndLoc) {
6311 Expr *ValExpr = Condition;
6312 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6313 !Condition->isInstantiationDependent() &&
6314 !Condition->containsUnexpandedParameterPack()) {
6315 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6316 Condition->getExprLoc(), Condition);
6317 if (Val.isInvalid())
6318 return nullptr;
6319
6320 ValExpr = Val.get();
6321 }
6322
6323 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6324}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006325ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6326 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006327 if (!Op)
6328 return ExprError();
6329
6330 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6331 public:
6332 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006333 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006334 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6335 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006336 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6337 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006338 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6339 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006340 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6341 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006342 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6343 QualType T,
6344 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006345 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6346 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006347 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6348 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006349 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006350 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006351 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006352 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6353 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006354 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6355 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006356 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6357 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006358 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006359 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006360 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006361 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6362 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006363 llvm_unreachable("conversion functions are permitted");
6364 }
6365 } ConvertDiagnoser;
6366 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6367}
6368
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006369static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006370 OpenMPClauseKind CKind,
6371 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006372 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6373 !ValExpr->isInstantiationDependent()) {
6374 SourceLocation Loc = ValExpr->getExprLoc();
6375 ExprResult Value =
6376 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6377 if (Value.isInvalid())
6378 return false;
6379
6380 ValExpr = Value.get();
6381 // The expression must evaluate to a non-negative integer value.
6382 llvm::APSInt Result;
6383 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006384 Result.isSigned() &&
6385 !((!StrictlyPositive && Result.isNonNegative()) ||
6386 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006387 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006388 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6389 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006390 return false;
6391 }
6392 }
6393 return true;
6394}
6395
Alexey Bataev568a8332014-03-06 06:15:19 +00006396OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6397 SourceLocation StartLoc,
6398 SourceLocation LParenLoc,
6399 SourceLocation EndLoc) {
6400 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006401
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006402 // OpenMP [2.5, Restrictions]
6403 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006404 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6405 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006406 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006407
Alexey Bataeved09d242014-05-28 05:53:51 +00006408 return new (Context)
6409 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006410}
6411
Alexey Bataev62c87d22014-03-21 04:51:18 +00006412ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006413 OpenMPClauseKind CKind,
6414 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006415 if (!E)
6416 return ExprError();
6417 if (E->isValueDependent() || E->isTypeDependent() ||
6418 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006419 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006420 llvm::APSInt Result;
6421 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6422 if (ICE.isInvalid())
6423 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006424 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6425 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006426 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006427 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6428 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006429 return ExprError();
6430 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006431 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6432 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6433 << E->getSourceRange();
6434 return ExprError();
6435 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006436 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6437 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006438 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006439 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006440 return ICE;
6441}
6442
6443OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6444 SourceLocation LParenLoc,
6445 SourceLocation EndLoc) {
6446 // OpenMP [2.8.1, simd construct, Description]
6447 // The parameter of the safelen clause must be a constant
6448 // positive integer expression.
6449 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6450 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006451 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006452 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006453 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006454}
6455
Alexey Bataev66b15b52015-08-21 11:14:16 +00006456OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6457 SourceLocation LParenLoc,
6458 SourceLocation EndLoc) {
6459 // OpenMP [2.8.1, simd construct, Description]
6460 // The parameter of the simdlen clause must be a constant
6461 // positive integer expression.
6462 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6463 if (Simdlen.isInvalid())
6464 return nullptr;
6465 return new (Context)
6466 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6467}
6468
Alexander Musman64d33f12014-06-04 07:53:32 +00006469OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6470 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006471 SourceLocation LParenLoc,
6472 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006473 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006474 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006475 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006476 // The parameter of the collapse clause must be a constant
6477 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006478 ExprResult NumForLoopsResult =
6479 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6480 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006481 return nullptr;
6482 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006483 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006484}
6485
Alexey Bataev10e775f2015-07-30 11:36:16 +00006486OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6487 SourceLocation EndLoc,
6488 SourceLocation LParenLoc,
6489 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006490 // OpenMP [2.7.1, loop construct, Description]
6491 // OpenMP [2.8.1, simd construct, Description]
6492 // OpenMP [2.9.6, distribute construct, Description]
6493 // The parameter of the ordered clause must be a constant
6494 // positive integer expression if any.
6495 if (NumForLoops && LParenLoc.isValid()) {
6496 ExprResult NumForLoopsResult =
6497 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6498 if (NumForLoopsResult.isInvalid())
6499 return nullptr;
6500 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006501 } else
6502 NumForLoops = nullptr;
6503 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006504 return new (Context)
6505 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6506}
6507
Alexey Bataeved09d242014-05-28 05:53:51 +00006508OMPClause *Sema::ActOnOpenMPSimpleClause(
6509 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6510 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006511 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006512 switch (Kind) {
6513 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006514 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006515 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6516 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006517 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006518 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006519 Res = ActOnOpenMPProcBindClause(
6520 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6521 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006522 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006523 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006524 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006525 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006526 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006527 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006528 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006529 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006530 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006531 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006532 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006533 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006534 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006535 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006536 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006537 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006538 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006539 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006540 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006541 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006542 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006543 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006544 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006545 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006546 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006547 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006548 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006549 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006550 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006551 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006552 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006553 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006554 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006555 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006556 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006557 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006558 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006559 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006560 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006561 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006562 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006563 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006564 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006565 llvm_unreachable("Clause is not allowed.");
6566 }
6567 return Res;
6568}
6569
Alexey Bataev6402bca2015-12-28 07:25:51 +00006570static std::string
6571getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6572 ArrayRef<unsigned> Exclude = llvm::None) {
6573 std::string Values;
6574 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6575 unsigned Skipped = Exclude.size();
6576 auto S = Exclude.begin(), E = Exclude.end();
6577 for (unsigned i = First; i < Last; ++i) {
6578 if (std::find(S, E, i) != E) {
6579 --Skipped;
6580 continue;
6581 }
6582 Values += "'";
6583 Values += getOpenMPSimpleClauseTypeName(K, i);
6584 Values += "'";
6585 if (i == Bound - Skipped)
6586 Values += " or ";
6587 else if (i != Bound + 1 - Skipped)
6588 Values += ", ";
6589 }
6590 return Values;
6591}
6592
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006593OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6594 SourceLocation KindKwLoc,
6595 SourceLocation StartLoc,
6596 SourceLocation LParenLoc,
6597 SourceLocation EndLoc) {
6598 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006599 static_assert(OMPC_DEFAULT_unknown > 0,
6600 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006601 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006602 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6603 /*Last=*/OMPC_DEFAULT_unknown)
6604 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006605 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006606 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006607 switch (Kind) {
6608 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006609 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006610 break;
6611 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006612 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006613 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006614 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006615 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006616 break;
6617 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006618 return new (Context)
6619 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006620}
6621
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006622OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6623 SourceLocation KindKwLoc,
6624 SourceLocation StartLoc,
6625 SourceLocation LParenLoc,
6626 SourceLocation EndLoc) {
6627 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006628 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006629 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6630 /*Last=*/OMPC_PROC_BIND_unknown)
6631 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006632 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006633 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006634 return new (Context)
6635 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006636}
6637
Alexey Bataev56dafe82014-06-20 07:16:17 +00006638OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006639 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006640 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006641 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006642 SourceLocation EndLoc) {
6643 OMPClause *Res = nullptr;
6644 switch (Kind) {
6645 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006646 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6647 assert(Argument.size() == NumberOfElements &&
6648 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006649 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006650 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6651 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6652 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6653 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6654 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006655 break;
6656 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006657 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6658 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6659 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6660 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006661 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006662 case OMPC_dist_schedule:
6663 Res = ActOnOpenMPDistScheduleClause(
6664 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6665 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6666 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006667 case OMPC_defaultmap:
6668 enum { Modifier, DefaultmapKind };
6669 Res = ActOnOpenMPDefaultmapClause(
6670 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6671 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6672 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6673 ArgumentLoc[DefaultmapKind], EndLoc);
6674 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006675 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006676 case OMPC_num_threads:
6677 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006678 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006679 case OMPC_collapse:
6680 case OMPC_default:
6681 case OMPC_proc_bind:
6682 case OMPC_private:
6683 case OMPC_firstprivate:
6684 case OMPC_lastprivate:
6685 case OMPC_shared:
6686 case OMPC_reduction:
6687 case OMPC_linear:
6688 case OMPC_aligned:
6689 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006690 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006691 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006692 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006693 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006694 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006695 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006696 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006697 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006698 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006699 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006700 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006701 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006702 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006703 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006704 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006705 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006706 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006707 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006708 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006709 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006710 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006711 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006712 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006713 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006714 case OMPC_unknown:
6715 llvm_unreachable("Clause is not allowed.");
6716 }
6717 return Res;
6718}
6719
Alexey Bataev6402bca2015-12-28 07:25:51 +00006720static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6721 OpenMPScheduleClauseModifier M2,
6722 SourceLocation M1Loc, SourceLocation M2Loc) {
6723 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6724 SmallVector<unsigned, 2> Excluded;
6725 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6726 Excluded.push_back(M2);
6727 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6728 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6729 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6730 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6731 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6732 << getListOfPossibleValues(OMPC_schedule,
6733 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6734 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6735 Excluded)
6736 << getOpenMPClauseName(OMPC_schedule);
6737 return true;
6738 }
6739 return false;
6740}
6741
Alexey Bataev56dafe82014-06-20 07:16:17 +00006742OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006743 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006744 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006745 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6746 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6747 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6748 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6749 return nullptr;
6750 // OpenMP, 2.7.1, Loop Construct, Restrictions
6751 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6752 // but not both.
6753 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6754 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6755 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6756 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6757 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6758 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6759 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6760 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6761 return nullptr;
6762 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006763 if (Kind == OMPC_SCHEDULE_unknown) {
6764 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006765 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6766 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6767 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6768 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6769 Exclude);
6770 } else {
6771 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6772 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006773 }
6774 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6775 << Values << getOpenMPClauseName(OMPC_schedule);
6776 return nullptr;
6777 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006778 // OpenMP, 2.7.1, Loop Construct, Restrictions
6779 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6780 // schedule(guided).
6781 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6782 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6783 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6784 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6785 diag::err_omp_schedule_nonmonotonic_static);
6786 return nullptr;
6787 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006788 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00006789 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006790 if (ChunkSize) {
6791 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6792 !ChunkSize->isInstantiationDependent() &&
6793 !ChunkSize->containsUnexpandedParameterPack()) {
6794 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6795 ExprResult Val =
6796 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6797 if (Val.isInvalid())
6798 return nullptr;
6799
6800 ValExpr = Val.get();
6801
6802 // OpenMP [2.7.1, Restrictions]
6803 // chunk_size must be a loop invariant integer expression with a positive
6804 // value.
6805 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006806 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6807 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6808 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006809 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006810 return nullptr;
6811 }
6812 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006813 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
6814 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
6815 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006816 }
6817 }
6818 }
6819
Alexey Bataev6402bca2015-12-28 07:25:51 +00006820 return new (Context)
6821 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00006822 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006823}
6824
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006825OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6826 SourceLocation StartLoc,
6827 SourceLocation EndLoc) {
6828 OMPClause *Res = nullptr;
6829 switch (Kind) {
6830 case OMPC_ordered:
6831 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6832 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006833 case OMPC_nowait:
6834 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6835 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006836 case OMPC_untied:
6837 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6838 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006839 case OMPC_mergeable:
6840 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6841 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006842 case OMPC_read:
6843 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6844 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006845 case OMPC_write:
6846 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6847 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006848 case OMPC_update:
6849 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6850 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006851 case OMPC_capture:
6852 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6853 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006854 case OMPC_seq_cst:
6855 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6856 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006857 case OMPC_threads:
6858 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6859 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006860 case OMPC_simd:
6861 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6862 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006863 case OMPC_nogroup:
6864 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6865 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006866 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006867 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006868 case OMPC_num_threads:
6869 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006870 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006871 case OMPC_collapse:
6872 case OMPC_schedule:
6873 case OMPC_private:
6874 case OMPC_firstprivate:
6875 case OMPC_lastprivate:
6876 case OMPC_shared:
6877 case OMPC_reduction:
6878 case OMPC_linear:
6879 case OMPC_aligned:
6880 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006881 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006882 case OMPC_default:
6883 case OMPC_proc_bind:
6884 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006885 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006886 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006887 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006888 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006889 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006890 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006891 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006892 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006893 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006894 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006895 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006896 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006897 case OMPC_unknown:
6898 llvm_unreachable("Clause is not allowed.");
6899 }
6900 return Res;
6901}
6902
Alexey Bataev236070f2014-06-20 11:19:47 +00006903OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6904 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006905 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006906 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6907}
6908
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006909OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6910 SourceLocation EndLoc) {
6911 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6912}
6913
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006914OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6915 SourceLocation EndLoc) {
6916 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6917}
6918
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006919OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6920 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006921 return new (Context) OMPReadClause(StartLoc, EndLoc);
6922}
6923
Alexey Bataevdea47612014-07-23 07:46:59 +00006924OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6925 SourceLocation EndLoc) {
6926 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6927}
6928
Alexey Bataev67a4f222014-07-23 10:25:33 +00006929OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6930 SourceLocation EndLoc) {
6931 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6932}
6933
Alexey Bataev459dec02014-07-24 06:46:57 +00006934OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6935 SourceLocation EndLoc) {
6936 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6937}
6938
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006939OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6940 SourceLocation EndLoc) {
6941 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6942}
6943
Alexey Bataev346265e2015-09-25 10:37:12 +00006944OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6945 SourceLocation EndLoc) {
6946 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6947}
6948
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006949OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6950 SourceLocation EndLoc) {
6951 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6952}
6953
Alexey Bataevb825de12015-12-07 10:51:44 +00006954OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6955 SourceLocation EndLoc) {
6956 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6957}
6958
Alexey Bataevc5e02582014-06-16 07:08:35 +00006959OMPClause *Sema::ActOnOpenMPVarListClause(
6960 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6961 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6962 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006963 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006964 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6965 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6966 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006967 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006968 switch (Kind) {
6969 case OMPC_private:
6970 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6971 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006972 case OMPC_firstprivate:
6973 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6974 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006975 case OMPC_lastprivate:
6976 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6977 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006978 case OMPC_shared:
6979 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6980 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006981 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006982 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6983 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006984 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006985 case OMPC_linear:
6986 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006987 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006988 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006989 case OMPC_aligned:
6990 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6991 ColonLoc, EndLoc);
6992 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006993 case OMPC_copyin:
6994 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6995 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006996 case OMPC_copyprivate:
6997 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6998 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006999 case OMPC_flush:
7000 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7001 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007002 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007003 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7004 StartLoc, LParenLoc, EndLoc);
7005 break;
7006 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007007 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7008 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7009 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007010 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007011 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007012 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007013 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007014 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007015 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007016 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007017 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007018 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007019 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007020 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007021 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007022 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007023 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007024 case OMPC_threadprivate:
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:
Michael Wonge710d542015-08-07 16:16:36 +00007030 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007031 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007032 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007033 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007034 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007035 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007036 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007037 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007038 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007039 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007040 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007041 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007042 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007043 llvm_unreachable("Clause is not allowed.");
7044 }
7045 return Res;
7046}
7047
Alexey Bataev90c228f2016-02-08 09:29:13 +00007048ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007049 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007050 ExprResult Res = BuildDeclRefExpr(
7051 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7052 if (!Res.isUsable())
7053 return ExprError();
7054 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7055 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7056 if (!Res.isUsable())
7057 return ExprError();
7058 }
7059 if (VK != VK_LValue && Res.get()->isGLValue()) {
7060 Res = DefaultLvalueConversion(Res.get());
7061 if (!Res.isUsable())
7062 return ExprError();
7063 }
7064 return Res;
7065}
7066
Alexey Bataev60da77e2016-02-29 05:54:20 +00007067static std::pair<ValueDecl *, bool>
7068getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7069 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007070 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7071 RefExpr->containsUnexpandedParameterPack())
7072 return std::make_pair(nullptr, true);
7073
Alexey Bataevd985eda2016-02-10 11:29:16 +00007074 // OpenMP [3.1, C/C++]
7075 // A list item is a variable name.
7076 // OpenMP [2.9.3.3, Restrictions, p.1]
7077 // A variable that is part of another variable (as an array or
7078 // structure element) cannot appear in a private clause.
7079 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007080 enum {
7081 NoArrayExpr = -1,
7082 ArraySubscript = 0,
7083 OMPArraySection = 1
7084 } IsArrayExpr = NoArrayExpr;
7085 if (AllowArraySection) {
7086 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7087 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7088 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7089 Base = TempASE->getBase()->IgnoreParenImpCasts();
7090 RefExpr = Base;
7091 IsArrayExpr = ArraySubscript;
7092 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7093 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7094 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7095 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7096 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7097 Base = TempASE->getBase()->IgnoreParenImpCasts();
7098 RefExpr = Base;
7099 IsArrayExpr = OMPArraySection;
7100 }
7101 }
7102 ELoc = RefExpr->getExprLoc();
7103 ERange = RefExpr->getSourceRange();
7104 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007105 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7106 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7107 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7108 (S.getCurrentThisType().isNull() || !ME ||
7109 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7110 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007111 if (IsArrayExpr != NoArrayExpr)
7112 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7113 << ERange;
7114 else {
7115 S.Diag(ELoc,
7116 AllowArraySection
7117 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7118 : diag::err_omp_expected_var_name_member_expr)
7119 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7120 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007121 return std::make_pair(nullptr, false);
7122 }
7123 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7124}
7125
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007126OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7127 SourceLocation StartLoc,
7128 SourceLocation LParenLoc,
7129 SourceLocation EndLoc) {
7130 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007131 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007132 for (auto &RefExpr : VarList) {
7133 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007134 SourceLocation ELoc;
7135 SourceRange ERange;
7136 Expr *SimpleRefExpr = RefExpr;
7137 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007138 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007139 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007140 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007141 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007142 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007143 ValueDecl *D = Res.first;
7144 if (!D)
7145 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007146
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007147 QualType Type = D->getType();
7148 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007149
7150 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7151 // A variable that appears in a private clause must not have an incomplete
7152 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007153 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007154 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007155 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007156
Alexey Bataev758e55e2013-09-06 18:03:48 +00007157 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7158 // in a Construct]
7159 // Variables with the predetermined data-sharing attributes may not be
7160 // listed in data-sharing attributes clauses, except for the cases
7161 // listed below. For these exceptions only, listing a predetermined
7162 // variable in a data-sharing attribute clause is allowed and overrides
7163 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007164 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007165 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007166 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7167 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007168 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007169 continue;
7170 }
7171
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007172 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007173 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007174 DSAStack->getCurrentDirective() == OMPD_task) {
7175 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7176 << getOpenMPClauseName(OMPC_private) << Type
7177 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7178 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007179 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007180 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007181 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007182 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007183 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007184 continue;
7185 }
7186
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007187 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7188 // A list item cannot appear in both a map clause and a data-sharing
7189 // attribute clause on the same construct
7190 if (DSAStack->getCurrentDirective() == OMPD_target) {
7191 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7192 [&](Expr *RE) -> bool {return true;})) {
7193 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7194 << getOpenMPClauseName(OMPC_private)
7195 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7196 ReportOriginalDSA(*this, DSAStack, D, DVar);
7197 continue;
7198 }
7199 }
7200
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007201 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7202 // A variable of class type (or array thereof) that appears in a private
7203 // clause requires an accessible, unambiguous default constructor for the
7204 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007205 // Generate helper private variable and initialize it with the default
7206 // value. The address of the original variable is replaced by the address of
7207 // the new private variable in CodeGen. This new variable is not added to
7208 // IdResolver, so the code in the OpenMP region uses original variable for
7209 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007210 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007211 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7212 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007213 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007214 if (VDPrivate->isInvalidDecl())
7215 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007216 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007217 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007218
Alexey Bataev90c228f2016-02-08 09:29:13 +00007219 DeclRefExpr *Ref = nullptr;
7220 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007221 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007222 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7223 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007224 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007225 }
7226
Alexey Bataeved09d242014-05-28 05:53:51 +00007227 if (Vars.empty())
7228 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007229
Alexey Bataev03b340a2014-10-21 03:16:40 +00007230 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7231 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007232}
7233
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007234namespace {
7235class DiagsUninitializedSeveretyRAII {
7236private:
7237 DiagnosticsEngine &Diags;
7238 SourceLocation SavedLoc;
7239 bool IsIgnored;
7240
7241public:
7242 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7243 bool IsIgnored)
7244 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7245 if (!IsIgnored) {
7246 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7247 /*Map*/ diag::Severity::Ignored, Loc);
7248 }
7249 }
7250 ~DiagsUninitializedSeveretyRAII() {
7251 if (!IsIgnored)
7252 Diags.popMappings(SavedLoc);
7253 }
7254};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007255}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007256
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007257OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7258 SourceLocation StartLoc,
7259 SourceLocation LParenLoc,
7260 SourceLocation EndLoc) {
7261 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007262 SmallVector<Expr *, 8> PrivateCopies;
7263 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007264 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007265 bool IsImplicitClause =
7266 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7267 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7268
Alexey Bataeved09d242014-05-28 05:53:51 +00007269 for (auto &RefExpr : VarList) {
7270 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007271 SourceLocation ELoc;
7272 SourceRange ERange;
7273 Expr *SimpleRefExpr = RefExpr;
7274 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007275 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007276 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007277 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007278 PrivateCopies.push_back(nullptr);
7279 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007280 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007281 ValueDecl *D = Res.first;
7282 if (!D)
7283 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007284
Alexey Bataev60da77e2016-02-29 05:54:20 +00007285 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007286 QualType Type = D->getType();
7287 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007288
7289 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7290 // A variable that appears in a private clause must not have an incomplete
7291 // type or a reference type.
7292 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007293 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007294 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007295 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007296
7297 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7298 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007299 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007300 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007301 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007302
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007303 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007304 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007305 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007306 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007307 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007308 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007309 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7310 // A list item that specifies a given variable may not appear in more
7311 // than one clause on the same directive, except that a variable may be
7312 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007313 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007314 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007315 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007316 << getOpenMPClauseName(DVar.CKind)
7317 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007318 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007319 continue;
7320 }
7321
7322 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7323 // in a Construct]
7324 // Variables with the predetermined data-sharing attributes may not be
7325 // listed in data-sharing attributes clauses, except for the cases
7326 // listed below. For these exceptions only, listing a predetermined
7327 // variable in a data-sharing attribute clause is allowed and overrides
7328 // the variable's predetermined data-sharing attributes.
7329 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7330 // in a Construct, C/C++, p.2]
7331 // Variables with const-qualified type having no mutable member may be
7332 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007333 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007334 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7335 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007336 << getOpenMPClauseName(DVar.CKind)
7337 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007338 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007339 continue;
7340 }
7341
Alexey Bataevf29276e2014-06-18 04:14:57 +00007342 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007343 // OpenMP [2.9.3.4, Restrictions, p.2]
7344 // A list item that is private within a parallel region must not appear
7345 // in a firstprivate clause on a worksharing construct if any of the
7346 // worksharing regions arising from the worksharing construct ever bind
7347 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007348 if (isOpenMPWorksharingDirective(CurrDir) &&
7349 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007350 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007351 if (DVar.CKind != OMPC_shared &&
7352 (isOpenMPParallelDirective(DVar.DKind) ||
7353 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007354 Diag(ELoc, diag::err_omp_required_access)
7355 << getOpenMPClauseName(OMPC_firstprivate)
7356 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007357 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007358 continue;
7359 }
7360 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007361 // OpenMP [2.9.3.4, Restrictions, p.3]
7362 // A list item that appears in a reduction clause of a parallel construct
7363 // must not appear in a firstprivate clause on a worksharing or task
7364 // construct if any of the worksharing or task regions arising from the
7365 // worksharing or task construct ever bind to any of the parallel regions
7366 // arising from the parallel construct.
7367 // OpenMP [2.9.3.4, Restrictions, p.4]
7368 // A list item that appears in a reduction clause in worksharing
7369 // construct must not appear in a firstprivate clause in a task construct
7370 // encountered during execution of any of the worksharing regions arising
7371 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007372 if (CurrDir == OMPD_task) {
7373 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007374 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007375 [](OpenMPDirectiveKind K) -> bool {
7376 return isOpenMPParallelDirective(K) ||
7377 isOpenMPWorksharingDirective(K);
7378 },
7379 false);
7380 if (DVar.CKind == OMPC_reduction &&
7381 (isOpenMPParallelDirective(DVar.DKind) ||
7382 isOpenMPWorksharingDirective(DVar.DKind))) {
7383 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7384 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007385 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007386 continue;
7387 }
7388 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007389
7390 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7391 // A list item that is private within a teams region must not appear in a
7392 // firstprivate clause on a distribute construct if any of the distribute
7393 // regions arising from the distribute construct ever bind to any of the
7394 // teams regions arising from the teams construct.
7395 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7396 // A list item that appears in a reduction clause of a teams construct
7397 // must not appear in a firstprivate clause on a distribute construct if
7398 // any of the distribute regions arising from the distribute construct
7399 // ever bind to any of the teams regions arising from the teams construct.
7400 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7401 // A list item may appear in a firstprivate or lastprivate clause but not
7402 // both.
7403 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007404 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007405 [](OpenMPDirectiveKind K) -> bool {
7406 return isOpenMPTeamsDirective(K);
7407 },
7408 false);
7409 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7410 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007411 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007412 continue;
7413 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007414 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007415 [](OpenMPDirectiveKind K) -> bool {
7416 return isOpenMPTeamsDirective(K);
7417 },
7418 false);
7419 if (DVar.CKind == OMPC_reduction &&
7420 isOpenMPTeamsDirective(DVar.DKind)) {
7421 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007422 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007423 continue;
7424 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007425 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007426 if (DVar.CKind == OMPC_lastprivate) {
7427 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007428 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007429 continue;
7430 }
7431 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007432 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7433 // A list item cannot appear in both a map clause and a data-sharing
7434 // attribute clause on the same construct
7435 if (CurrDir == OMPD_target) {
7436 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7437 [&](Expr *RE) -> bool {return true;})) {
7438 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7439 << getOpenMPClauseName(OMPC_firstprivate)
7440 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7441 ReportOriginalDSA(*this, DSAStack, D, DVar);
7442 continue;
7443 }
7444 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007445 }
7446
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007447 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007448 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007449 DSAStack->getCurrentDirective() == OMPD_task) {
7450 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7451 << getOpenMPClauseName(OMPC_firstprivate) << Type
7452 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7453 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007454 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007455 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007456 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007457 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007458 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007459 continue;
7460 }
7461
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007462 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007463 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7464 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007465 // Generate helper private variable and initialize it with the value of the
7466 // original variable. The address of the original variable is replaced by
7467 // the address of the new private variable in the CodeGen. This new variable
7468 // is not added to IdResolver, so the code in the OpenMP region uses
7469 // original variable for proper diagnostics and variable capturing.
7470 Expr *VDInitRefExpr = nullptr;
7471 // For arrays generate initializer for single element and replace it by the
7472 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007473 if (Type->isArrayType()) {
7474 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007475 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007476 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007477 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007478 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007479 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007480 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007481 InitializedEntity Entity =
7482 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007483 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7484
7485 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7486 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7487 if (Result.isInvalid())
7488 VDPrivate->setInvalidDecl();
7489 else
7490 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007491 // Remove temp variable declaration.
7492 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007493 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007494 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7495 ".firstprivate.temp");
7496 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7497 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007498 AddInitializerToDecl(VDPrivate,
7499 DefaultLvalueConversion(VDInitRefExpr).get(),
7500 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007501 }
7502 if (VDPrivate->isInvalidDecl()) {
7503 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007504 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007505 diag::note_omp_task_predetermined_firstprivate_here);
7506 }
7507 continue;
7508 }
7509 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007510 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007511 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7512 RefExpr->getExprLoc());
7513 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007514 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007515 if (TopDVar.CKind == OMPC_lastprivate)
7516 Ref = TopDVar.PrivateCopy;
7517 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007518 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007519 if (!IsOpenMPCapturedDecl(D))
7520 ExprCaptures.push_back(Ref->getDecl());
7521 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007522 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007523 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7524 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007525 PrivateCopies.push_back(VDPrivateRefExpr);
7526 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007527 }
7528
Alexey Bataeved09d242014-05-28 05:53:51 +00007529 if (Vars.empty())
7530 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007531
7532 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007533 Vars, PrivateCopies, Inits,
7534 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007535}
7536
Alexander Musman1bb328c2014-06-04 13:06:39 +00007537OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7538 SourceLocation StartLoc,
7539 SourceLocation LParenLoc,
7540 SourceLocation EndLoc) {
7541 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007542 SmallVector<Expr *, 8> SrcExprs;
7543 SmallVector<Expr *, 8> DstExprs;
7544 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007545 SmallVector<Decl *, 4> ExprCaptures;
7546 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007547 for (auto &RefExpr : VarList) {
7548 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007549 SourceLocation ELoc;
7550 SourceRange ERange;
7551 Expr *SimpleRefExpr = RefExpr;
7552 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007553 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007554 // It will be analyzed later.
7555 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007556 SrcExprs.push_back(nullptr);
7557 DstExprs.push_back(nullptr);
7558 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007559 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007560 ValueDecl *D = Res.first;
7561 if (!D)
7562 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007563
Alexey Bataev74caaf22016-02-20 04:09:36 +00007564 QualType Type = D->getType();
7565 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007566
7567 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7568 // A variable that appears in a lastprivate clause must not have an
7569 // incomplete type or a reference type.
7570 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007571 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007572 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007573 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007574
7575 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7576 // in a Construct]
7577 // Variables with the predetermined data-sharing attributes may not be
7578 // listed in data-sharing attributes clauses, except for the cases
7579 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007580 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007581 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7582 DVar.CKind != OMPC_firstprivate &&
7583 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7584 Diag(ELoc, diag::err_omp_wrong_dsa)
7585 << getOpenMPClauseName(DVar.CKind)
7586 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007587 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007588 continue;
7589 }
7590
Alexey Bataevf29276e2014-06-18 04:14:57 +00007591 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7592 // OpenMP [2.14.3.5, Restrictions, p.2]
7593 // A list item that is private within a parallel region, or that appears in
7594 // the reduction clause of a parallel construct, must not appear in a
7595 // lastprivate clause on a worksharing construct if any of the corresponding
7596 // worksharing regions ever binds to any of the corresponding parallel
7597 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007598 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007599 if (isOpenMPWorksharingDirective(CurrDir) &&
7600 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007601 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007602 if (DVar.CKind != OMPC_shared) {
7603 Diag(ELoc, diag::err_omp_required_access)
7604 << getOpenMPClauseName(OMPC_lastprivate)
7605 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007606 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007607 continue;
7608 }
7609 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007610
7611 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7612 // A list item may appear in a firstprivate or lastprivate clause but not
7613 // both.
7614 if (CurrDir == OMPD_distribute) {
7615 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7616 if (DVar.CKind == OMPC_firstprivate) {
7617 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7618 ReportOriginalDSA(*this, DSAStack, D, DVar);
7619 continue;
7620 }
7621 }
7622
Alexander Musman1bb328c2014-06-04 13:06:39 +00007623 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007624 // A variable of class type (or array thereof) that appears in a
7625 // lastprivate clause requires an accessible, unambiguous default
7626 // constructor for the class type, unless the list item is also specified
7627 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007628 // A variable of class type (or array thereof) that appears in a
7629 // lastprivate clause requires an accessible, unambiguous copy assignment
7630 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007631 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007632 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007633 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007634 D->hasAttrs() ? &D->getAttrs() : nullptr);
7635 auto *PseudoSrcExpr =
7636 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007637 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007638 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007639 D->hasAttrs() ? &D->getAttrs() : nullptr);
7640 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007641 // For arrays generate assignment operation for single element and replace
7642 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007643 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007644 PseudoDstExpr, PseudoSrcExpr);
7645 if (AssignmentOp.isInvalid())
7646 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007647 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007648 /*DiscardedValue=*/true);
7649 if (AssignmentOp.isInvalid())
7650 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007651
Alexey Bataev74caaf22016-02-20 04:09:36 +00007652 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007653 if (!VD) {
7654 if (TopDVar.CKind == OMPC_firstprivate)
7655 Ref = TopDVar.PrivateCopy;
7656 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007657 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007658 if (!IsOpenMPCapturedDecl(D))
7659 ExprCaptures.push_back(Ref->getDecl());
7660 }
7661 if (TopDVar.CKind == OMPC_firstprivate ||
7662 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00007663 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007664 ExprResult RefRes = DefaultLvalueConversion(Ref);
7665 if (!RefRes.isUsable())
7666 continue;
7667 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007668 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
7669 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007670 if (!PostUpdateRes.isUsable())
7671 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00007672 ExprPostUpdates.push_back(
7673 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007674 }
7675 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00007676 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00007677 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
7678 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00007679 SrcExprs.push_back(PseudoSrcExpr);
7680 DstExprs.push_back(PseudoDstExpr);
7681 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007682 }
7683
7684 if (Vars.empty())
7685 return nullptr;
7686
7687 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00007688 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007689 buildPreInits(Context, ExprCaptures),
7690 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00007691}
7692
Alexey Bataev758e55e2013-09-06 18:03:48 +00007693OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7694 SourceLocation StartLoc,
7695 SourceLocation LParenLoc,
7696 SourceLocation EndLoc) {
7697 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007698 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007699 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007700 SourceLocation ELoc;
7701 SourceRange ERange;
7702 Expr *SimpleRefExpr = RefExpr;
7703 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007704 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007705 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007706 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007707 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007708 ValueDecl *D = Res.first;
7709 if (!D)
7710 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007711
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007712 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007713 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7714 // in a Construct]
7715 // Variables with the predetermined data-sharing attributes may not be
7716 // listed in data-sharing attributes clauses, except for the cases
7717 // listed below. For these exceptions only, listing a predetermined
7718 // variable in a data-sharing attribute clause is allowed and overrides
7719 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007720 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007721 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7722 DVar.RefExpr) {
7723 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7724 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007725 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007726 continue;
7727 }
7728
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007729 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00007730 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00007731 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007732 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00007733 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007734 }
7735
Alexey Bataeved09d242014-05-28 05:53:51 +00007736 if (Vars.empty())
7737 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007738
7739 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7740}
7741
Alexey Bataevc5e02582014-06-16 07:08:35 +00007742namespace {
7743class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7744 DSAStackTy *Stack;
7745
7746public:
7747 bool VisitDeclRefExpr(DeclRefExpr *E) {
7748 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007749 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007750 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7751 return false;
7752 if (DVar.CKind != OMPC_unknown)
7753 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007754 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007755 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007756 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007757 return true;
7758 return false;
7759 }
7760 return false;
7761 }
7762 bool VisitStmt(Stmt *S) {
7763 for (auto Child : S->children()) {
7764 if (Child && Visit(Child))
7765 return true;
7766 }
7767 return false;
7768 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007769 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007770};
Alexey Bataev23b69422014-06-18 07:08:49 +00007771} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007772
Alexey Bataev60da77e2016-02-29 05:54:20 +00007773namespace {
7774// Transform MemberExpression for specified FieldDecl of current class to
7775// DeclRefExpr to specified OMPCapturedExprDecl.
7776class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
7777 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
7778 ValueDecl *Field;
7779 DeclRefExpr *CapturedExpr;
7780
7781public:
7782 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
7783 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
7784
7785 ExprResult TransformMemberExpr(MemberExpr *E) {
7786 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
7787 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00007788 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00007789 return CapturedExpr;
7790 }
7791 return BaseTransform::TransformMemberExpr(E);
7792 }
7793 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
7794};
7795} // namespace
7796
Alexey Bataeva839ddd2016-03-17 10:19:46 +00007797template <typename T>
7798static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
7799 const llvm::function_ref<T(ValueDecl *)> &Gen) {
7800 for (auto &Set : Lookups) {
7801 for (auto *D : Set) {
7802 if (auto Res = Gen(cast<ValueDecl>(D)))
7803 return Res;
7804 }
7805 }
7806 return T();
7807}
7808
7809static ExprResult
7810buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
7811 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
7812 const DeclarationNameInfo &ReductionId, QualType Ty,
7813 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
7814 if (ReductionIdScopeSpec.isInvalid())
7815 return ExprError();
7816 SmallVector<UnresolvedSet<8>, 4> Lookups;
7817 if (S) {
7818 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
7819 Lookup.suppressDiagnostics();
7820 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
7821 auto *D = Lookup.getRepresentativeDecl();
7822 do {
7823 S = S->getParent();
7824 } while (S && !S->isDeclScope(D));
7825 if (S)
7826 S = S->getParent();
7827 Lookups.push_back(UnresolvedSet<8>());
7828 Lookups.back().append(Lookup.begin(), Lookup.end());
7829 Lookup.clear();
7830 }
7831 } else if (auto *ULE =
7832 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
7833 Lookups.push_back(UnresolvedSet<8>());
7834 Decl *PrevD = nullptr;
7835 for(auto *D : ULE->decls()) {
7836 if (D == PrevD)
7837 Lookups.push_back(UnresolvedSet<8>());
7838 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
7839 Lookups.back().addDecl(DRD);
7840 PrevD = D;
7841 }
7842 }
7843 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
7844 Ty->containsUnexpandedParameterPack() ||
7845 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
7846 return !D->isInvalidDecl() &&
7847 (D->getType()->isDependentType() ||
7848 D->getType()->isInstantiationDependentType() ||
7849 D->getType()->containsUnexpandedParameterPack());
7850 })) {
7851 UnresolvedSet<8> ResSet;
7852 for (auto &Set : Lookups) {
7853 ResSet.append(Set.begin(), Set.end());
7854 // The last item marks the end of all declarations at the specified scope.
7855 ResSet.addDecl(Set[Set.size() - 1]);
7856 }
7857 return UnresolvedLookupExpr::Create(
7858 SemaRef.Context, /*NamingClass=*/nullptr,
7859 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
7860 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
7861 }
7862 if (auto *VD = filterLookupForUDR<ValueDecl *>(
7863 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
7864 if (!D->isInvalidDecl() &&
7865 SemaRef.Context.hasSameType(D->getType(), Ty))
7866 return D;
7867 return nullptr;
7868 }))
7869 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
7870 if (auto *VD = filterLookupForUDR<ValueDecl *>(
7871 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
7872 if (!D->isInvalidDecl() &&
7873 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
7874 !Ty.isMoreQualifiedThan(D->getType()))
7875 return D;
7876 return nullptr;
7877 })) {
7878 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
7879 /*DetectVirtual=*/false);
7880 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
7881 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
7882 VD->getType().getUnqualifiedType()))) {
7883 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
7884 /*DiagID=*/0) !=
7885 Sema::AR_inaccessible) {
7886 SemaRef.BuildBasePathArray(Paths, BasePath);
7887 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
7888 }
7889 }
7890 }
7891 }
7892 if (ReductionIdScopeSpec.isSet()) {
7893 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
7894 return ExprError();
7895 }
7896 return ExprEmpty();
7897}
7898
Alexey Bataevc5e02582014-06-16 07:08:35 +00007899OMPClause *Sema::ActOnOpenMPReductionClause(
7900 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7901 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00007902 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
7903 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00007904 auto DN = ReductionId.getName();
7905 auto OOK = DN.getCXXOverloadedOperator();
7906 BinaryOperatorKind BOK = BO_Comma;
7907
7908 // OpenMP [2.14.3.6, reduction clause]
7909 // C
7910 // reduction-identifier is either an identifier or one of the following
7911 // operators: +, -, *, &, |, ^, && and ||
7912 // C++
7913 // reduction-identifier is either an id-expression or one of the following
7914 // operators: +, -, *, &, |, ^, && and ||
7915 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7916 switch (OOK) {
7917 case OO_Plus:
7918 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007919 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007920 break;
7921 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007922 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007923 break;
7924 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007925 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007926 break;
7927 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007928 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007929 break;
7930 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007931 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007932 break;
7933 case OO_AmpAmp:
7934 BOK = BO_LAnd;
7935 break;
7936 case OO_PipePipe:
7937 BOK = BO_LOr;
7938 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007939 case OO_New:
7940 case OO_Delete:
7941 case OO_Array_New:
7942 case OO_Array_Delete:
7943 case OO_Slash:
7944 case OO_Percent:
7945 case OO_Tilde:
7946 case OO_Exclaim:
7947 case OO_Equal:
7948 case OO_Less:
7949 case OO_Greater:
7950 case OO_LessEqual:
7951 case OO_GreaterEqual:
7952 case OO_PlusEqual:
7953 case OO_MinusEqual:
7954 case OO_StarEqual:
7955 case OO_SlashEqual:
7956 case OO_PercentEqual:
7957 case OO_CaretEqual:
7958 case OO_AmpEqual:
7959 case OO_PipeEqual:
7960 case OO_LessLess:
7961 case OO_GreaterGreater:
7962 case OO_LessLessEqual:
7963 case OO_GreaterGreaterEqual:
7964 case OO_EqualEqual:
7965 case OO_ExclaimEqual:
7966 case OO_PlusPlus:
7967 case OO_MinusMinus:
7968 case OO_Comma:
7969 case OO_ArrowStar:
7970 case OO_Arrow:
7971 case OO_Call:
7972 case OO_Subscript:
7973 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007974 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007975 case NUM_OVERLOADED_OPERATORS:
7976 llvm_unreachable("Unexpected reduction identifier");
7977 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007978 if (auto II = DN.getAsIdentifierInfo()) {
7979 if (II->isStr("max"))
7980 BOK = BO_GT;
7981 else if (II->isStr("min"))
7982 BOK = BO_LT;
7983 }
7984 break;
7985 }
7986 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00007987 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00007988 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007989 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007990
7991 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007992 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007993 SmallVector<Expr *, 8> LHSs;
7994 SmallVector<Expr *, 8> RHSs;
7995 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00007996 SmallVector<Decl *, 4> ExprCaptures;
7997 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00007998 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
7999 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008000 for (auto RefExpr : VarList) {
8001 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008002 // OpenMP [2.1, C/C++]
8003 // A list item is a variable or array section, subject to the restrictions
8004 // specified in Section 2.4 on page 42 and in each of the sections
8005 // describing clauses and directives for which a list appears.
8006 // OpenMP [2.14.3.3, Restrictions, p.1]
8007 // A variable that is part of another variable (as an array or
8008 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008009 if (!FirstIter && IR != ER)
8010 ++IR;
8011 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008012 SourceLocation ELoc;
8013 SourceRange ERange;
8014 Expr *SimpleRefExpr = RefExpr;
8015 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8016 /*AllowArraySection=*/true);
8017 if (Res.second) {
8018 // It will be analyzed later.
8019 Vars.push_back(RefExpr);
8020 Privates.push_back(nullptr);
8021 LHSs.push_back(nullptr);
8022 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008023 // Try to find 'declare reduction' corresponding construct before using
8024 // builtin/overloaded operators.
8025 QualType Type = Context.DependentTy;
8026 CXXCastPath BasePath;
8027 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8028 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8029 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8030 if (CurContext->isDependentContext() &&
8031 (DeclareReductionRef.isUnset() ||
8032 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8033 ReductionOps.push_back(DeclareReductionRef.get());
8034 else
8035 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008036 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008037 ValueDecl *D = Res.first;
8038 if (!D)
8039 continue;
8040
Alexey Bataeva1764212015-09-30 09:22:36 +00008041 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008042 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8043 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8044 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008045 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008046 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008047 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8048 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8049 Type = ATy->getElementType();
8050 else
8051 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008052 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008053 } else
8054 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8055 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008056
Alexey Bataevc5e02582014-06-16 07:08:35 +00008057 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8058 // A variable that appears in a private clause must not have an incomplete
8059 // type or a reference type.
8060 if (RequireCompleteType(ELoc, Type,
8061 diag::err_omp_reduction_incomplete_type))
8062 continue;
8063 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008064 // A list item that appears in a reduction clause must not be
8065 // const-qualified.
8066 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008067 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008068 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008069 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008070 bool IsDecl = !VD ||
8071 VD->isThisDeclarationADefinition(Context) ==
8072 VarDecl::DeclarationOnly;
8073 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008074 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008075 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008076 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008077 continue;
8078 }
8079 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8080 // If a list-item is a reference type then it must bind to the same object
8081 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008082 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008083 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008084 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008085 DSARefChecker Check(DSAStack);
8086 if (Check.Visit(VDDef->getInit())) {
8087 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8088 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8089 continue;
8090 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008091 }
8092 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008093
Alexey Bataevc5e02582014-06-16 07:08:35 +00008094 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8095 // in a Construct]
8096 // Variables with the predetermined data-sharing attributes may not be
8097 // listed in data-sharing attributes clauses, except for the cases
8098 // listed below. For these exceptions only, listing a predetermined
8099 // variable in a data-sharing attribute clause is allowed and overrides
8100 // the variable's predetermined data-sharing attributes.
8101 // OpenMP [2.14.3.6, Restrictions, p.3]
8102 // Any number of reduction clauses can be specified on the directive,
8103 // but a list item can appear only once in the reduction clauses for that
8104 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008105 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008106 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008107 if (DVar.CKind == OMPC_reduction) {
8108 Diag(ELoc, diag::err_omp_once_referenced)
8109 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008110 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008111 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008112 } else if (DVar.CKind != OMPC_unknown) {
8113 Diag(ELoc, diag::err_omp_wrong_dsa)
8114 << getOpenMPClauseName(DVar.CKind)
8115 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008116 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008117 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008118 }
8119
8120 // OpenMP [2.14.3.6, Restrictions, p.1]
8121 // A list item that appears in a reduction clause of a worksharing
8122 // construct must be shared in the parallel regions to which any of the
8123 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008124 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8125 if (isOpenMPWorksharingDirective(CurrDir) &&
8126 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008127 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008128 if (DVar.CKind != OMPC_shared) {
8129 Diag(ELoc, diag::err_omp_required_access)
8130 << getOpenMPClauseName(OMPC_reduction)
8131 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008132 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008133 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008134 }
8135 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008136
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008137 // Try to find 'declare reduction' corresponding construct before using
8138 // builtin/overloaded operators.
8139 CXXCastPath BasePath;
8140 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8141 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8142 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8143 if (DeclareReductionRef.isInvalid())
8144 continue;
8145 if (CurContext->isDependentContext() &&
8146 (DeclareReductionRef.isUnset() ||
8147 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8148 Vars.push_back(RefExpr);
8149 Privates.push_back(nullptr);
8150 LHSs.push_back(nullptr);
8151 RHSs.push_back(nullptr);
8152 ReductionOps.push_back(DeclareReductionRef.get());
8153 continue;
8154 }
8155 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8156 // Not allowed reduction identifier is found.
8157 Diag(ReductionId.getLocStart(),
8158 diag::err_omp_unknown_reduction_identifier)
8159 << Type << ReductionIdRange;
8160 continue;
8161 }
8162
8163 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8164 // The type of a list item that appears in a reduction clause must be valid
8165 // for the reduction-identifier. For a max or min reduction in C, the type
8166 // of the list item must be an allowed arithmetic data type: char, int,
8167 // float, double, or _Bool, possibly modified with long, short, signed, or
8168 // unsigned. For a max or min reduction in C++, the type of the list item
8169 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8170 // double, or bool, possibly modified with long, short, signed, or unsigned.
8171 if (DeclareReductionRef.isUnset()) {
8172 if ((BOK == BO_GT || BOK == BO_LT) &&
8173 !(Type->isScalarType() ||
8174 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8175 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8176 << getLangOpts().CPlusPlus;
8177 if (!ASE && !OASE) {
8178 bool IsDecl = !VD ||
8179 VD->isThisDeclarationADefinition(Context) ==
8180 VarDecl::DeclarationOnly;
8181 Diag(D->getLocation(),
8182 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8183 << D;
8184 }
8185 continue;
8186 }
8187 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8188 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8189 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8190 if (!ASE && !OASE) {
8191 bool IsDecl = !VD ||
8192 VD->isThisDeclarationADefinition(Context) ==
8193 VarDecl::DeclarationOnly;
8194 Diag(D->getLocation(),
8195 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8196 << D;
8197 }
8198 continue;
8199 }
8200 }
8201
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008202 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008203 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008204 D->hasAttrs() ? &D->getAttrs() : nullptr);
8205 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8206 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008207 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008208 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008209 (!ASE &&
8210 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008211 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008212 // Create pseudo array type for private copy. The size for this array will
8213 // be generated during codegen.
8214 // For array subscripts or single variables Private Ty is the same as Type
8215 // (type of the variable or single array element).
8216 PrivateTy = Context.getVariableArrayType(
8217 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8218 Context.getSizeType(), VK_RValue),
8219 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008220 } else if (!ASE && !OASE &&
8221 Context.getAsArrayType(D->getType().getNonReferenceType()))
8222 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008223 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008224 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8225 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008226 // Add initializer for private variable.
8227 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008228 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8229 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8230 if (DeclareReductionRef.isUsable()) {
8231 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8232 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8233 if (DRD->getInitializer()) {
8234 Init = DRDRef;
8235 RHSVD->setInit(DRDRef);
8236 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008237 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008238 } else {
8239 switch (BOK) {
8240 case BO_Add:
8241 case BO_Xor:
8242 case BO_Or:
8243 case BO_LOr:
8244 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8245 if (Type->isScalarType() || Type->isAnyComplexType())
8246 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8247 break;
8248 case BO_Mul:
8249 case BO_LAnd:
8250 if (Type->isScalarType() || Type->isAnyComplexType()) {
8251 // '*' and '&&' reduction ops - initializer is '1'.
8252 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008253 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008254 break;
8255 case BO_And: {
8256 // '&' reduction op - initializer is '~0'.
8257 QualType OrigType = Type;
8258 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8259 Type = ComplexTy->getElementType();
8260 if (Type->isRealFloatingType()) {
8261 llvm::APFloat InitValue =
8262 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8263 /*isIEEE=*/true);
8264 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8265 Type, ELoc);
8266 } else if (Type->isScalarType()) {
8267 auto Size = Context.getTypeSize(Type);
8268 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8269 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8270 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8271 }
8272 if (Init && OrigType->isAnyComplexType()) {
8273 // Init = 0xFFFF + 0xFFFFi;
8274 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8275 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8276 }
8277 Type = OrigType;
8278 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008279 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008280 case BO_LT:
8281 case BO_GT: {
8282 // 'min' reduction op - initializer is 'Largest representable number in
8283 // the reduction list item type'.
8284 // 'max' reduction op - initializer is 'Least representable number in
8285 // the reduction list item type'.
8286 if (Type->isIntegerType() || Type->isPointerType()) {
8287 bool IsSigned = Type->hasSignedIntegerRepresentation();
8288 auto Size = Context.getTypeSize(Type);
8289 QualType IntTy =
8290 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8291 llvm::APInt InitValue =
8292 (BOK != BO_LT)
8293 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8294 : llvm::APInt::getMinValue(Size)
8295 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8296 : llvm::APInt::getMaxValue(Size);
8297 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8298 if (Type->isPointerType()) {
8299 // Cast to pointer type.
8300 auto CastExpr = BuildCStyleCastExpr(
8301 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8302 SourceLocation(), Init);
8303 if (CastExpr.isInvalid())
8304 continue;
8305 Init = CastExpr.get();
8306 }
8307 } else if (Type->isRealFloatingType()) {
8308 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8309 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8310 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8311 Type, ELoc);
8312 }
8313 break;
8314 }
8315 case BO_PtrMemD:
8316 case BO_PtrMemI:
8317 case BO_MulAssign:
8318 case BO_Div:
8319 case BO_Rem:
8320 case BO_Sub:
8321 case BO_Shl:
8322 case BO_Shr:
8323 case BO_LE:
8324 case BO_GE:
8325 case BO_EQ:
8326 case BO_NE:
8327 case BO_AndAssign:
8328 case BO_XorAssign:
8329 case BO_OrAssign:
8330 case BO_Assign:
8331 case BO_AddAssign:
8332 case BO_SubAssign:
8333 case BO_DivAssign:
8334 case BO_RemAssign:
8335 case BO_ShlAssign:
8336 case BO_ShrAssign:
8337 case BO_Comma:
8338 llvm_unreachable("Unexpected reduction operation");
8339 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008340 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008341 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008342 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8343 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008344 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008345 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008346 if (RHSVD->isInvalidDecl())
8347 continue;
8348 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008349 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8350 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008351 bool IsDecl =
8352 !VD ||
8353 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8354 Diag(D->getLocation(),
8355 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8356 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008357 continue;
8358 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008359 // Store initializer for single element in private copy. Will be used during
8360 // codegen.
8361 PrivateVD->setInit(RHSVD->getInit());
8362 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008363 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008364 ExprResult ReductionOp;
8365 if (DeclareReductionRef.isUsable()) {
8366 QualType RedTy = DeclareReductionRef.get()->getType();
8367 QualType PtrRedTy = Context.getPointerType(RedTy);
8368 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8369 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8370 if (!BasePath.empty()) {
8371 LHS = DefaultLvalueConversion(LHS.get());
8372 RHS = DefaultLvalueConversion(RHS.get());
8373 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8374 CK_UncheckedDerivedToBase, LHS.get(),
8375 &BasePath, LHS.get()->getValueKind());
8376 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8377 CK_UncheckedDerivedToBase, RHS.get(),
8378 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008379 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008380 FunctionProtoType::ExtProtoInfo EPI;
8381 QualType Params[] = {PtrRedTy, PtrRedTy};
8382 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8383 auto *OVE = new (Context) OpaqueValueExpr(
8384 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8385 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8386 Expr *Args[] = {LHS.get(), RHS.get()};
8387 ReductionOp = new (Context)
8388 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8389 } else {
8390 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8391 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8392 if (ReductionOp.isUsable()) {
8393 if (BOK != BO_LT && BOK != BO_GT) {
8394 ReductionOp =
8395 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8396 BO_Assign, LHSDRE, ReductionOp.get());
8397 } else {
8398 auto *ConditionalOp = new (Context) ConditionalOperator(
8399 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8400 RHSDRE, Type, VK_LValue, OK_Ordinary);
8401 ReductionOp =
8402 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8403 BO_Assign, LHSDRE, ConditionalOp);
8404 }
8405 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8406 }
8407 if (ReductionOp.isInvalid())
8408 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008409 }
8410
Alexey Bataev60da77e2016-02-29 05:54:20 +00008411 DeclRefExpr *Ref = nullptr;
8412 Expr *VarsExpr = RefExpr->IgnoreParens();
8413 if (!VD) {
8414 if (ASE || OASE) {
8415 TransformExprToCaptures RebuildToCapture(*this, D);
8416 VarsExpr =
8417 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8418 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008419 } else {
8420 VarsExpr = Ref =
8421 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008422 }
8423 if (!IsOpenMPCapturedDecl(D)) {
8424 ExprCaptures.push_back(Ref->getDecl());
8425 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8426 ExprResult RefRes = DefaultLvalueConversion(Ref);
8427 if (!RefRes.isUsable())
8428 continue;
8429 ExprResult PostUpdateRes =
8430 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8431 SimpleRefExpr, RefRes.get());
8432 if (!PostUpdateRes.isUsable())
8433 continue;
8434 ExprPostUpdates.push_back(
8435 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008436 }
8437 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008438 }
8439 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8440 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008441 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008442 LHSs.push_back(LHSDRE);
8443 RHSs.push_back(RHSDRE);
8444 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008445 }
8446
8447 if (Vars.empty())
8448 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008449
Alexey Bataevc5e02582014-06-16 07:08:35 +00008450 return OMPReductionClause::Create(
8451 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008452 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008453 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8454 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008455}
8456
Alexey Bataev182227b2015-08-20 10:54:39 +00008457OMPClause *Sema::ActOnOpenMPLinearClause(
8458 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8459 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8460 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008461 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008462 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008463 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008464 SmallVector<Decl *, 4> ExprCaptures;
8465 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataev182227b2015-08-20 10:54:39 +00008466 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8467 LinKind == OMPC_LINEAR_unknown) {
8468 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8469 LinKind = OMPC_LINEAR_val;
8470 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008471 for (auto &RefExpr : VarList) {
8472 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008473 SourceLocation ELoc;
8474 SourceRange ERange;
8475 Expr *SimpleRefExpr = RefExpr;
8476 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8477 /*AllowArraySection=*/false);
8478 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008479 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008480 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008481 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008482 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008483 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008484 ValueDecl *D = Res.first;
8485 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008486 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008487
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008488 QualType Type = D->getType();
8489 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008490
8491 // OpenMP [2.14.3.7, linear clause]
8492 // A list-item cannot appear in more than one linear clause.
8493 // A list-item that appears in a linear clause cannot appear in any
8494 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008495 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008496 if (DVar.RefExpr) {
8497 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8498 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008499 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008500 continue;
8501 }
8502
8503 // A variable must not have an incomplete type or a reference type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008504 if (RequireCompleteType(ELoc, Type,
8505 diag::err_omp_linear_incomplete_type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008506 continue;
Alexey Bataev1185e192015-08-20 12:15:57 +00008507 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008508 !Type->isReferenceType()) {
Alexey Bataev1185e192015-08-20 12:15:57 +00008509 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008510 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
Alexey Bataev1185e192015-08-20 12:15:57 +00008511 continue;
8512 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008513 Type = Type.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008514
8515 // A list item must not be const-qualified.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008516 if (Type.isConstant(Context)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008517 Diag(ELoc, diag::err_omp_const_variable)
8518 << getOpenMPClauseName(OMPC_linear);
8519 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008520 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008521 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008522 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008523 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008524 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008525 continue;
8526 }
8527
8528 // A list item must be of integral or pointer type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008529 Type = Type.getUnqualifiedType().getCanonicalType();
8530 const auto *Ty = Type.getTypePtrOrNull();
Alexander Musman8dba6642014-04-22 13:09:42 +00008531 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8532 !Ty->isPointerType())) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008533 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
Alexander Musman8dba6642014-04-22 13:09:42 +00008534 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008535 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008536 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008537 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008538 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008539 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008540 continue;
8541 }
8542
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008543 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008544 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8545 D->hasAttrs() ? &D->getAttrs() : nullptr);
8546 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008547 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008548 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008549 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008550 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008551 if (!VD) {
8552 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8553 if (!IsOpenMPCapturedDecl(D)) {
8554 ExprCaptures.push_back(Ref->getDecl());
8555 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8556 ExprResult RefRes = DefaultLvalueConversion(Ref);
8557 if (!RefRes.isUsable())
8558 continue;
8559 ExprResult PostUpdateRes =
8560 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8561 SimpleRefExpr, RefRes.get());
8562 if (!PostUpdateRes.isUsable())
8563 continue;
8564 ExprPostUpdates.push_back(
8565 IgnoredValueConversions(PostUpdateRes.get()).get());
8566 }
8567 }
8568 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008569 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008570 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008571 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008572 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008573 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008574 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8575 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8576
8577 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
8578 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008579 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008580 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008581 }
8582
8583 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008584 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008585
8586 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008587 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008588 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8589 !Step->isInstantiationDependent() &&
8590 !Step->containsUnexpandedParameterPack()) {
8591 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008592 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008593 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008594 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008595 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008596
Alexander Musman3276a272015-03-21 10:12:56 +00008597 // Build var to save the step value.
8598 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008599 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008600 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008601 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008602 ExprResult CalcStep =
8603 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008604 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008605
Alexander Musman8dba6642014-04-22 13:09:42 +00008606 // Warn about zero linear step (it would be probably better specified as
8607 // making corresponding variables 'const').
8608 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008609 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8610 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008611 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8612 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008613 if (!IsConstant && CalcStep.isUsable()) {
8614 // Calculate the step beforehand instead of doing this on each iteration.
8615 // (This is not used if the number of iterations may be kfold-ed).
8616 CalcStepExpr = CalcStep.get();
8617 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008618 }
8619
Alexey Bataev182227b2015-08-20 10:54:39 +00008620 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8621 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008622 StepExpr, CalcStepExpr,
8623 buildPreInits(Context, ExprCaptures),
8624 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00008625}
8626
Alexey Bataev5a3af132016-03-29 08:58:54 +00008627static bool
8628FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8629 Expr *NumIterations, Sema &SemaRef, Scope *S) {
Alexander Musman3276a272015-03-21 10:12:56 +00008630 // Walk the vars and build update/final expressions for the CodeGen.
8631 SmallVector<Expr *, 8> Updates;
8632 SmallVector<Expr *, 8> Finals;
8633 Expr *Step = Clause.getStep();
8634 Expr *CalcStep = Clause.getCalcStep();
8635 // OpenMP [2.14.3.7, linear clause]
8636 // If linear-step is not specified it is assumed to be 1.
8637 if (Step == nullptr)
8638 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008639 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00008640 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008641 }
Alexander Musman3276a272015-03-21 10:12:56 +00008642 bool HasErrors = false;
8643 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008644 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008645 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008646 for (auto &RefExpr : Clause.varlists()) {
8647 Expr *InitExpr = *CurInit;
8648
8649 // Build privatized reference to the current linear var.
8650 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008651 Expr *CapturedRef;
8652 if (LinKind == OMPC_LINEAR_uval)
8653 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8654 else
8655 CapturedRef =
8656 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8657 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8658 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008659
8660 // Build update: Var = InitExpr + IV * Step
8661 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008662 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008663 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008664 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8665 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008666
8667 // Build final: Var = InitExpr + NumIterations * Step
8668 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008669 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008670 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008671 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8672 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008673 if (!Update.isUsable() || !Final.isUsable()) {
8674 Updates.push_back(nullptr);
8675 Finals.push_back(nullptr);
8676 HasErrors = true;
8677 } else {
8678 Updates.push_back(Update.get());
8679 Finals.push_back(Final.get());
8680 }
Richard Trieucc3949d2016-02-18 22:34:54 +00008681 ++CurInit;
8682 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008683 }
8684 Clause.setUpdates(Updates);
8685 Clause.setFinals(Finals);
8686 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008687}
8688
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008689OMPClause *Sema::ActOnOpenMPAlignedClause(
8690 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8691 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8692
8693 SmallVector<Expr *, 8> Vars;
8694 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00008695 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8696 SourceLocation ELoc;
8697 SourceRange ERange;
8698 Expr *SimpleRefExpr = RefExpr;
8699 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8700 /*AllowArraySection=*/false);
8701 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008702 // It will be analyzed later.
8703 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008704 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00008705 ValueDecl *D = Res.first;
8706 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008707 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008708
Alexey Bataev1efd1662016-03-29 10:59:56 +00008709 QualType QType = D->getType();
8710 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008711
8712 // OpenMP [2.8.1, simd construct, Restrictions]
8713 // The type of list items appearing in the aligned clause must be
8714 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008715 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008716 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00008717 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008718 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00008719 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008720 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00008721 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008722 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008723 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008724 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00008725 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008726 continue;
8727 }
8728
8729 // OpenMP [2.8.1, simd construct, Restrictions]
8730 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00008731 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
8732 Diag(ELoc, diag::err_omp_aligned_twice) << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008733 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8734 << getOpenMPClauseName(OMPC_aligned);
8735 continue;
8736 }
8737
Alexey Bataev1efd1662016-03-29 10:59:56 +00008738 DeclRefExpr *Ref = nullptr;
8739 if (!VD && IsOpenMPCapturedDecl(D))
8740 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8741 Vars.push_back(DefaultFunctionArrayConversion(
8742 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
8743 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008744 }
8745
8746 // OpenMP [2.8.1, simd construct, Description]
8747 // The parameter of the aligned clause, alignment, must be a constant
8748 // positive integer expression.
8749 // If no optional parameter is specified, implementation-defined default
8750 // alignments for SIMD instructions on the target platforms are assumed.
8751 if (Alignment != nullptr) {
8752 ExprResult AlignResult =
8753 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8754 if (AlignResult.isInvalid())
8755 return nullptr;
8756 Alignment = AlignResult.get();
8757 }
8758 if (Vars.empty())
8759 return nullptr;
8760
8761 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8762 EndLoc, Vars, Alignment);
8763}
8764
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008765OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8766 SourceLocation StartLoc,
8767 SourceLocation LParenLoc,
8768 SourceLocation EndLoc) {
8769 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008770 SmallVector<Expr *, 8> SrcExprs;
8771 SmallVector<Expr *, 8> DstExprs;
8772 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008773 for (auto &RefExpr : VarList) {
8774 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8775 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008776 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008777 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008778 SrcExprs.push_back(nullptr);
8779 DstExprs.push_back(nullptr);
8780 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008781 continue;
8782 }
8783
Alexey Bataeved09d242014-05-28 05:53:51 +00008784 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008785 // OpenMP [2.1, C/C++]
8786 // A list item is a variable name.
8787 // OpenMP [2.14.4.1, Restrictions, p.1]
8788 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008789 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008790 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008791 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8792 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008793 continue;
8794 }
8795
8796 Decl *D = DE->getDecl();
8797 VarDecl *VD = cast<VarDecl>(D);
8798
8799 QualType Type = VD->getType();
8800 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8801 // It will be analyzed later.
8802 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008803 SrcExprs.push_back(nullptr);
8804 DstExprs.push_back(nullptr);
8805 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008806 continue;
8807 }
8808
8809 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8810 // A list item that appears in a copyin clause must be threadprivate.
8811 if (!DSAStack->isThreadPrivate(VD)) {
8812 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008813 << getOpenMPClauseName(OMPC_copyin)
8814 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008815 continue;
8816 }
8817
8818 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8819 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008820 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008821 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008822 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008823 auto *SrcVD =
8824 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8825 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008826 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008827 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8828 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008829 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8830 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008831 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008832 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008833 // For arrays generate assignment operation for single element and replace
8834 // it by the original array element in CodeGen.
8835 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8836 PseudoDstExpr, PseudoSrcExpr);
8837 if (AssignmentOp.isInvalid())
8838 continue;
8839 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8840 /*DiscardedValue=*/true);
8841 if (AssignmentOp.isInvalid())
8842 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008843
8844 DSAStack->addDSA(VD, DE, OMPC_copyin);
8845 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008846 SrcExprs.push_back(PseudoSrcExpr);
8847 DstExprs.push_back(PseudoDstExpr);
8848 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008849 }
8850
Alexey Bataeved09d242014-05-28 05:53:51 +00008851 if (Vars.empty())
8852 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008853
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008854 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8855 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008856}
8857
Alexey Bataevbae9a792014-06-27 10:37:06 +00008858OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8859 SourceLocation StartLoc,
8860 SourceLocation LParenLoc,
8861 SourceLocation EndLoc) {
8862 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008863 SmallVector<Expr *, 8> SrcExprs;
8864 SmallVector<Expr *, 8> DstExprs;
8865 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008866 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00008867 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8868 SourceLocation ELoc;
8869 SourceRange ERange;
8870 Expr *SimpleRefExpr = RefExpr;
8871 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8872 /*AllowArraySection=*/false);
8873 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008874 // It will be analyzed later.
8875 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008876 SrcExprs.push_back(nullptr);
8877 DstExprs.push_back(nullptr);
8878 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008879 }
Alexey Bataeve122da12016-03-17 10:50:17 +00008880 ValueDecl *D = Res.first;
8881 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00008882 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008883
Alexey Bataeve122da12016-03-17 10:50:17 +00008884 QualType Type = D->getType();
8885 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008886
8887 // OpenMP [2.14.4.2, Restrictions, p.2]
8888 // A list item that appears in a copyprivate clause may not appear in a
8889 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00008890 if (!VD || !DSAStack->isThreadPrivate(VD)) {
8891 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008892 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8893 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008894 Diag(ELoc, diag::err_omp_wrong_dsa)
8895 << getOpenMPClauseName(DVar.CKind)
8896 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00008897 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008898 continue;
8899 }
8900
8901 // OpenMP [2.11.4.2, Restrictions, p.1]
8902 // All list items that appear in a copyprivate clause must be either
8903 // threadprivate or private in the enclosing context.
8904 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00008905 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008906 if (DVar.CKind == OMPC_shared) {
8907 Diag(ELoc, diag::err_omp_required_access)
8908 << getOpenMPClauseName(OMPC_copyprivate)
8909 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00008910 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008911 continue;
8912 }
8913 }
8914 }
8915
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008916 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008917 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008918 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008919 << getOpenMPClauseName(OMPC_copyprivate) << Type
8920 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008921 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00008922 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008923 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00008924 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008925 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00008926 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008927 continue;
8928 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008929
Alexey Bataevbae9a792014-06-27 10:37:06 +00008930 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8931 // A variable of class type (or array thereof) that appears in a
8932 // copyin clause requires an accessible, unambiguous copy assignment
8933 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008934 Type = Context.getBaseElementType(Type.getNonReferenceType())
8935 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008936 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00008937 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
8938 D->hasAttrs() ? &D->getAttrs() : nullptr);
8939 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008940 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00008941 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
8942 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008943 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00008944 buildDeclRefExpr(*this, DstVD, Type, ELoc);
8945 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00008946 PseudoDstExpr, PseudoSrcExpr);
8947 if (AssignmentOp.isInvalid())
8948 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00008949 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00008950 /*DiscardedValue=*/true);
8951 if (AssignmentOp.isInvalid())
8952 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008953
8954 // No need to mark vars as copyprivate, they are already threadprivate or
8955 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00008956 assert(VD || IsOpenMPCapturedDecl(D));
8957 Vars.push_back(
8958 VD ? RefExpr->IgnoreParens()
8959 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00008960 SrcExprs.push_back(PseudoSrcExpr);
8961 DstExprs.push_back(PseudoDstExpr);
8962 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008963 }
8964
8965 if (Vars.empty())
8966 return nullptr;
8967
Alexey Bataeva63048e2015-03-23 06:18:07 +00008968 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8969 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008970}
8971
Alexey Bataev6125da92014-07-21 11:26:11 +00008972OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8973 SourceLocation StartLoc,
8974 SourceLocation LParenLoc,
8975 SourceLocation EndLoc) {
8976 if (VarList.empty())
8977 return nullptr;
8978
8979 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8980}
Alexey Bataevdea47612014-07-23 07:46:59 +00008981
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008982OMPClause *
8983Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8984 SourceLocation DepLoc, SourceLocation ColonLoc,
8985 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8986 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008987 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008988 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008989 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008990 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008991 return nullptr;
8992 }
8993 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008994 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8995 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008996 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008997 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008998 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8999 /*Last=*/OMPC_DEPEND_unknown, Except)
9000 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009001 return nullptr;
9002 }
9003 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009004 llvm::APSInt DepCounter(/*BitWidth=*/32);
9005 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9006 if (DepKind == OMPC_DEPEND_sink) {
9007 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9008 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9009 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009010 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009011 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009012 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9013 DSAStack->getParentOrderedRegionParam()) {
9014 for (auto &RefExpr : VarList) {
9015 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9016 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9017 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9018 // It will be analyzed later.
9019 Vars.push_back(RefExpr);
9020 continue;
9021 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009022
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009023 SourceLocation ELoc = RefExpr->getExprLoc();
9024 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9025 if (DepKind == OMPC_DEPEND_sink) {
9026 if (DepCounter >= TotalDepCount) {
9027 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9028 continue;
9029 }
9030 ++DepCounter;
9031 // OpenMP [2.13.9, Summary]
9032 // depend(dependence-type : vec), where dependence-type is:
9033 // 'sink' and where vec is the iteration vector, which has the form:
9034 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9035 // where n is the value specified by the ordered clause in the loop
9036 // directive, xi denotes the loop iteration variable of the i-th nested
9037 // loop associated with the loop directive, and di is a constant
9038 // non-negative integer.
9039 SimpleExpr = SimpleExpr->IgnoreImplicit();
9040 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9041 if (!DE) {
9042 OverloadedOperatorKind OOK = OO_None;
9043 SourceLocation OOLoc;
9044 Expr *LHS, *RHS;
9045 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9046 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9047 OOLoc = BO->getOperatorLoc();
9048 LHS = BO->getLHS()->IgnoreParenImpCasts();
9049 RHS = BO->getRHS()->IgnoreParenImpCasts();
9050 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9051 OOK = OCE->getOperator();
9052 OOLoc = OCE->getOperatorLoc();
9053 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9054 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9055 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9056 OOK = MCE->getMethodDecl()
9057 ->getNameInfo()
9058 .getName()
9059 .getCXXOverloadedOperator();
9060 OOLoc = MCE->getCallee()->getExprLoc();
9061 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9062 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9063 } else {
9064 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9065 continue;
9066 }
9067 DE = dyn_cast<DeclRefExpr>(LHS);
9068 if (!DE) {
9069 Diag(LHS->getExprLoc(),
9070 diag::err_omp_depend_sink_expected_loop_iteration)
9071 << DSAStack->getParentLoopControlVariable(
9072 DepCounter.getZExtValue());
9073 continue;
9074 }
9075 if (OOK != OO_Plus && OOK != OO_Minus) {
9076 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9077 continue;
9078 }
9079 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9080 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9081 if (Res.isInvalid())
9082 continue;
9083 }
9084 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9085 if (!CurContext->isDependentContext() &&
9086 DSAStack->getParentOrderedRegionParam() &&
9087 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
9088 Diag(DE->getExprLoc(),
9089 diag::err_omp_depend_sink_expected_loop_iteration)
9090 << DSAStack->getParentLoopControlVariable(
9091 DepCounter.getZExtValue());
9092 continue;
9093 }
9094 } else {
9095 // OpenMP [2.11.1.1, Restrictions, p.3]
9096 // A variable that is part of another variable (such as a field of a
9097 // structure) but is not an array element or an array section cannot
9098 // appear in a depend clause.
9099 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9100 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9101 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9102 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9103 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009104 (ASE &&
9105 !ASE->getBase()
9106 ->getType()
9107 .getNonReferenceType()
9108 ->isPointerType() &&
9109 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009110 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9111 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009112 continue;
9113 }
9114 }
9115
9116 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9117 }
9118
9119 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9120 TotalDepCount > VarList.size() &&
9121 DSAStack->getParentOrderedRegionParam()) {
9122 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9123 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9124 }
9125 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9126 Vars.empty())
9127 return nullptr;
9128 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009129
9130 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9131 DepLoc, ColonLoc, Vars);
9132}
Michael Wonge710d542015-08-07 16:16:36 +00009133
9134OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9135 SourceLocation LParenLoc,
9136 SourceLocation EndLoc) {
9137 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009138
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009139 // OpenMP [2.9.1, Restrictions]
9140 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009141 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9142 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009143 return nullptr;
9144
Michael Wonge710d542015-08-07 16:16:36 +00009145 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9146}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009147
9148static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9149 DSAStackTy *Stack, CXXRecordDecl *RD) {
9150 if (!RD || RD->isInvalidDecl())
9151 return true;
9152
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009153 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9154 if (auto *CTD = CTSD->getSpecializedTemplate())
9155 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009156 auto QTy = SemaRef.Context.getRecordType(RD);
9157 if (RD->isDynamicClass()) {
9158 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9159 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9160 return false;
9161 }
9162 auto *DC = RD;
9163 bool IsCorrect = true;
9164 for (auto *I : DC->decls()) {
9165 if (I) {
9166 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9167 if (MD->isStatic()) {
9168 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9169 SemaRef.Diag(MD->getLocation(),
9170 diag::note_omp_static_member_in_target);
9171 IsCorrect = false;
9172 }
9173 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9174 if (VD->isStaticDataMember()) {
9175 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9176 SemaRef.Diag(VD->getLocation(),
9177 diag::note_omp_static_member_in_target);
9178 IsCorrect = false;
9179 }
9180 }
9181 }
9182 }
9183
9184 for (auto &I : RD->bases()) {
9185 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9186 I.getType()->getAsCXXRecordDecl()))
9187 IsCorrect = false;
9188 }
9189 return IsCorrect;
9190}
9191
9192static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9193 DSAStackTy *Stack, QualType QTy) {
9194 NamedDecl *ND;
9195 if (QTy->isIncompleteType(&ND)) {
9196 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9197 return false;
9198 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9199 if (!RD->isInvalidDecl() &&
9200 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9201 return false;
9202 }
9203 return true;
9204}
9205
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009206/// \brief Return true if it can be proven that the provided array expression
9207/// (array section or array subscript) does NOT specify the whole size of the
9208/// array whose base type is \a BaseQTy.
9209static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9210 const Expr *E,
9211 QualType BaseQTy) {
9212 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9213
9214 // If this is an array subscript, it refers to the whole size if the size of
9215 // the dimension is constant and equals 1. Also, an array section assumes the
9216 // format of an array subscript if no colon is used.
9217 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9218 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9219 return ATy->getSize().getSExtValue() != 1;
9220 // Size can't be evaluated statically.
9221 return false;
9222 }
9223
9224 assert(OASE && "Expecting array section if not an array subscript.");
9225 auto *LowerBound = OASE->getLowerBound();
9226 auto *Length = OASE->getLength();
9227
9228 // If there is a lower bound that does not evaluates to zero, we are not
9229 // convering the whole dimension.
9230 if (LowerBound) {
9231 llvm::APSInt ConstLowerBound;
9232 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9233 return false; // Can't get the integer value as a constant.
9234 if (ConstLowerBound.getSExtValue())
9235 return true;
9236 }
9237
9238 // If we don't have a length we covering the whole dimension.
9239 if (!Length)
9240 return false;
9241
9242 // If the base is a pointer, we don't have a way to get the size of the
9243 // pointee.
9244 if (BaseQTy->isPointerType())
9245 return false;
9246
9247 // We can only check if the length is the same as the size of the dimension
9248 // if we have a constant array.
9249 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9250 if (!CATy)
9251 return false;
9252
9253 llvm::APSInt ConstLength;
9254 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9255 return false; // Can't get the integer value as a constant.
9256
9257 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9258}
9259
9260// Return true if it can be proven that the provided array expression (array
9261// section or array subscript) does NOT specify a single element of the array
9262// whose base type is \a BaseQTy.
9263static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9264 const Expr *E,
9265 QualType BaseQTy) {
9266 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9267
9268 // An array subscript always refer to a single element. Also, an array section
9269 // assumes the format of an array subscript if no colon is used.
9270 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9271 return false;
9272
9273 assert(OASE && "Expecting array section if not an array subscript.");
9274 auto *Length = OASE->getLength();
9275
9276 // If we don't have a length we have to check if the array has unitary size
9277 // for this dimension. Also, we should always expect a length if the base type
9278 // is pointer.
9279 if (!Length) {
9280 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9281 return ATy->getSize().getSExtValue() != 1;
9282 // We cannot assume anything.
9283 return false;
9284 }
9285
9286 // Check if the length evaluates to 1.
9287 llvm::APSInt ConstLength;
9288 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9289 return false; // Can't get the integer value as a constant.
9290
9291 return ConstLength.getSExtValue() != 1;
9292}
9293
Samuel Antao5de996e2016-01-22 20:21:36 +00009294// Return the expression of the base of the map clause or null if it cannot
9295// be determined and do all the necessary checks to see if the expression is
9296// valid as a standalone map clause expression.
9297static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
9298 SourceLocation ELoc = E->getExprLoc();
9299 SourceRange ERange = E->getSourceRange();
9300
9301 // The base of elements of list in a map clause have to be either:
9302 // - a reference to variable or field.
9303 // - a member expression.
9304 // - an array expression.
9305 //
9306 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9307 // reference to 'r'.
9308 //
9309 // If we have:
9310 //
9311 // struct SS {
9312 // Bla S;
9313 // foo() {
9314 // #pragma omp target map (S.Arr[:12]);
9315 // }
9316 // }
9317 //
9318 // We want to retrieve the member expression 'this->S';
9319
9320 Expr *RelevantExpr = nullptr;
9321
Samuel Antao5de996e2016-01-22 20:21:36 +00009322 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9323 // If a list item is an array section, it must specify contiguous storage.
9324 //
9325 // For this restriction it is sufficient that we make sure only references
9326 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009327 // exist except in the rightmost expression (unless they cover the whole
9328 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009329 //
9330 // r.ArrS[3:5].Arr[6:7]
9331 //
9332 // r.ArrS[3:5].x
9333 //
9334 // but these would be valid:
9335 // r.ArrS[3].Arr[6:7]
9336 //
9337 // r.ArrS[3].x
9338
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009339 bool AllowUnitySizeArraySection = true;
9340 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009341
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009342 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009343 E = E->IgnoreParenImpCasts();
9344
9345 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9346 if (!isa<VarDecl>(CurE->getDecl()))
9347 break;
9348
9349 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009350
9351 // If we got a reference to a declaration, we should not expect any array
9352 // section before that.
9353 AllowUnitySizeArraySection = false;
9354 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009355 continue;
9356 }
9357
9358 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9359 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9360
9361 if (isa<CXXThisExpr>(BaseE))
9362 // We found a base expression: this->Val.
9363 RelevantExpr = CurE;
9364 else
9365 E = BaseE;
9366
9367 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9368 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9369 << CurE->getSourceRange();
9370 break;
9371 }
9372
9373 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9374
9375 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9376 // A bit-field cannot appear in a map clause.
9377 //
9378 if (FD->isBitField()) {
9379 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9380 << CurE->getSourceRange();
9381 break;
9382 }
9383
9384 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9385 // If the type of a list item is a reference to a type T then the type
9386 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009387 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009388
9389 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9390 // A list item cannot be a variable that is a member of a structure with
9391 // a union type.
9392 //
9393 if (auto *RT = CurType->getAs<RecordType>())
9394 if (RT->isUnionType()) {
9395 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9396 << CurE->getSourceRange();
9397 break;
9398 }
9399
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009400 // If we got a member expression, we should not expect any array section
9401 // before that:
9402 //
9403 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9404 // If a list item is an element of a structure, only the rightmost symbol
9405 // of the variable reference can be an array section.
9406 //
9407 AllowUnitySizeArraySection = false;
9408 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009409 continue;
9410 }
9411
9412 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9413 E = CurE->getBase()->IgnoreParenImpCasts();
9414
9415 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9416 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9417 << 0 << CurE->getSourceRange();
9418 break;
9419 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009420
9421 // If we got an array subscript that express the whole dimension we
9422 // can have any array expressions before. If it only expressing part of
9423 // the dimension, we can only have unitary-size array expressions.
9424 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9425 E->getType()))
9426 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009427 continue;
9428 }
9429
9430 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009431 E = CurE->getBase()->IgnoreParenImpCasts();
9432
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009433 auto CurType =
9434 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9435
Samuel Antao5de996e2016-01-22 20:21:36 +00009436 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9437 // If the type of a list item is a reference to a type T then the type
9438 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009439 if (CurType->isReferenceType())
9440 CurType = CurType->getPointeeType();
9441
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009442 bool IsPointer = CurType->isAnyPointerType();
9443
9444 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009445 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9446 << 0 << CurE->getSourceRange();
9447 break;
9448 }
9449
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009450 bool NotWhole =
9451 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9452 bool NotUnity =
9453 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9454
9455 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9456 // Any array section is currently allowed.
9457 //
9458 // If this array section refers to the whole dimension we can still
9459 // accept other array sections before this one, except if the base is a
9460 // pointer. Otherwise, only unitary sections are accepted.
9461 if (NotWhole || IsPointer)
9462 AllowWholeSizeArraySection = false;
9463 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9464 (AllowWholeSizeArraySection && NotWhole)) {
9465 // A unity or whole array section is not allowed and that is not
9466 // compatible with the properties of the current array section.
9467 SemaRef.Diag(
9468 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9469 << CurE->getSourceRange();
9470 break;
9471 }
Samuel Antao5de996e2016-01-22 20:21:36 +00009472 continue;
9473 }
9474
9475 // If nothing else worked, this is not a valid map clause expression.
9476 SemaRef.Diag(ELoc,
9477 diag::err_omp_expected_named_var_member_or_array_expression)
9478 << ERange;
9479 break;
9480 }
9481
9482 return RelevantExpr;
9483}
9484
9485// Return true if expression E associated with value VD has conflicts with other
9486// map information.
9487static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9488 Expr *E, bool CurrentRegionOnly) {
9489 assert(VD && E);
9490
9491 // Types used to organize the components of a valid map clause.
9492 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9493 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9494
9495 // Helper to extract the components in the map clause expression E and store
9496 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9497 // it has already passed the single clause checks.
9498 auto ExtractMapExpressionComponents = [](Expr *TE,
9499 MapExpressionComponents &MEC) {
9500 while (true) {
9501 TE = TE->IgnoreParenImpCasts();
9502
9503 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9504 MEC.push_back(
9505 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9506 break;
9507 }
9508
9509 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9510 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9511
9512 MEC.push_back(MapExpressionComponent(
9513 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9514 if (isa<CXXThisExpr>(BaseE))
9515 break;
9516
9517 TE = BaseE;
9518 continue;
9519 }
9520
9521 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9522 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9523 TE = CurE->getBase()->IgnoreParenImpCasts();
9524 continue;
9525 }
9526
9527 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9528 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9529 TE = CurE->getBase()->IgnoreParenImpCasts();
9530 continue;
9531 }
9532
9533 llvm_unreachable(
9534 "Expecting only valid map clause expressions at this point!");
9535 }
9536 };
9537
9538 SourceLocation ELoc = E->getExprLoc();
9539 SourceRange ERange = E->getSourceRange();
9540
9541 // In order to easily check the conflicts we need to match each component of
9542 // the expression under test with the components of the expressions that are
9543 // already in the stack.
9544
9545 MapExpressionComponents CurComponents;
9546 ExtractMapExpressionComponents(E, CurComponents);
9547
9548 assert(!CurComponents.empty() && "Map clause expression with no components!");
9549 assert(CurComponents.back().second == VD &&
9550 "Map clause expression with unexpected base!");
9551
9552 // Variables to help detecting enclosing problems in data environment nests.
9553 bool IsEnclosedByDataEnvironmentExpr = false;
9554 Expr *EnclosingExpr = nullptr;
9555
9556 bool FoundError =
9557 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9558 MapExpressionComponents StackComponents;
9559 ExtractMapExpressionComponents(RE, StackComponents);
9560 assert(!StackComponents.empty() &&
9561 "Map clause expression with no components!");
9562 assert(StackComponents.back().second == VD &&
9563 "Map clause expression with unexpected base!");
9564
9565 // Expressions must start from the same base. Here we detect at which
9566 // point both expressions diverge from each other and see if we can
9567 // detect if the memory referred to both expressions is contiguous and
9568 // do not overlap.
9569 auto CI = CurComponents.rbegin();
9570 auto CE = CurComponents.rend();
9571 auto SI = StackComponents.rbegin();
9572 auto SE = StackComponents.rend();
9573 for (; CI != CE && SI != SE; ++CI, ++SI) {
9574
9575 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9576 // At most one list item can be an array item derived from a given
9577 // variable in map clauses of the same construct.
9578 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9579 isa<OMPArraySectionExpr>(CI->first)) &&
9580 (isa<ArraySubscriptExpr>(SI->first) ||
9581 isa<OMPArraySectionExpr>(SI->first))) {
9582 SemaRef.Diag(CI->first->getExprLoc(),
9583 diag::err_omp_multiple_array_items_in_map_clause)
9584 << CI->first->getSourceRange();
9585 ;
9586 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9587 << SI->first->getSourceRange();
9588 return true;
9589 }
9590
9591 // Do both expressions have the same kind?
9592 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9593 break;
9594
9595 // Are we dealing with different variables/fields?
9596 if (CI->second != SI->second)
9597 break;
9598 }
9599
9600 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9601 // List items of map clauses in the same construct must not share
9602 // original storage.
9603 //
9604 // If the expressions are exactly the same or one is a subset of the
9605 // other, it means they are sharing storage.
9606 if (CI == CE && SI == SE) {
9607 if (CurrentRegionOnly) {
9608 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9609 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9610 << RE->getSourceRange();
9611 return true;
9612 } else {
9613 // If we find the same expression in the enclosing data environment,
9614 // that is legal.
9615 IsEnclosedByDataEnvironmentExpr = true;
9616 return false;
9617 }
9618 }
9619
9620 QualType DerivedType = std::prev(CI)->first->getType();
9621 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9622
9623 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9624 // If the type of a list item is a reference to a type T then the type
9625 // will be considered to be T for all purposes of this clause.
9626 if (DerivedType->isReferenceType())
9627 DerivedType = DerivedType->getPointeeType();
9628
9629 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9630 // A variable for which the type is pointer and an array section
9631 // derived from that variable must not appear as list items of map
9632 // clauses of the same construct.
9633 //
9634 // Also, cover one of the cases in:
9635 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9636 // If any part of the original storage of a list item has corresponding
9637 // storage in the device data environment, all of the original storage
9638 // must have corresponding storage in the device data environment.
9639 //
9640 if (DerivedType->isAnyPointerType()) {
9641 if (CI == CE || SI == SE) {
9642 SemaRef.Diag(
9643 DerivedLoc,
9644 diag::err_omp_pointer_mapped_along_with_derived_section)
9645 << DerivedLoc;
9646 } else {
9647 assert(CI != CE && SI != SE);
9648 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9649 << DerivedLoc;
9650 }
9651 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9652 << RE->getSourceRange();
9653 return true;
9654 }
9655
9656 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9657 // List items of map clauses in the same construct must not share
9658 // original storage.
9659 //
9660 // An expression is a subset of the other.
9661 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9662 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9663 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9664 << RE->getSourceRange();
9665 return true;
9666 }
9667
9668 // The current expression uses the same base as other expression in the
9669 // data environment but does not contain it completelly.
9670 if (!CurrentRegionOnly && SI != SE)
9671 EnclosingExpr = RE;
9672
9673 // The current expression is a subset of the expression in the data
9674 // environment.
9675 IsEnclosedByDataEnvironmentExpr |=
9676 (!CurrentRegionOnly && CI != CE && SI == SE);
9677
9678 return false;
9679 });
9680
9681 if (CurrentRegionOnly)
9682 return FoundError;
9683
9684 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9685 // If any part of the original storage of a list item has corresponding
9686 // storage in the device data environment, all of the original storage must
9687 // have corresponding storage in the device data environment.
9688 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9689 // If a list item is an element of a structure, and a different element of
9690 // the structure has a corresponding list item in the device data environment
9691 // prior to a task encountering the construct associated with the map clause,
9692 // then the list item must also have a correspnding list item in the device
9693 // data environment prior to the task encountering the construct.
9694 //
9695 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9696 SemaRef.Diag(ELoc,
9697 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9698 << ERange;
9699 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9700 << EnclosingExpr->getSourceRange();
9701 return true;
9702 }
9703
9704 return FoundError;
9705}
9706
Samuel Antao23abd722016-01-19 20:40:49 +00009707OMPClause *
9708Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9709 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9710 SourceLocation MapLoc, SourceLocation ColonLoc,
9711 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9712 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009713 SmallVector<Expr *, 4> Vars;
9714
9715 for (auto &RE : VarList) {
9716 assert(RE && "Null expr in omp map");
9717 if (isa<DependentScopeDeclRefExpr>(RE)) {
9718 // It will be analyzed later.
9719 Vars.push_back(RE);
9720 continue;
9721 }
9722 SourceLocation ELoc = RE->getExprLoc();
9723
Kelvin Li0bff7af2015-11-23 05:32:03 +00009724 auto *VE = RE->IgnoreParenLValueCasts();
9725
9726 if (VE->isValueDependent() || VE->isTypeDependent() ||
9727 VE->isInstantiationDependent() ||
9728 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009729 // We can only analyze this information once the missing information is
9730 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009731 Vars.push_back(RE);
9732 continue;
9733 }
9734
9735 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009736
Samuel Antao5de996e2016-01-22 20:21:36 +00009737 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9738 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9739 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009740 continue;
9741 }
9742
Samuel Antao5de996e2016-01-22 20:21:36 +00009743 // Obtain the array or member expression bases if required.
9744 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9745 if (!BE)
9746 continue;
9747
9748 // If the base is a reference to a variable, we rely on that variable for
9749 // the following checks. If it is a 'this' expression we rely on the field.
9750 ValueDecl *D = nullptr;
9751 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9752 D = DRE->getDecl();
9753 } else {
9754 auto *ME = cast<MemberExpr>(BE);
9755 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9756 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009757 }
9758 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009759
Samuel Antao5de996e2016-01-22 20:21:36 +00009760 auto *VD = dyn_cast<VarDecl>(D);
9761 auto *FD = dyn_cast<FieldDecl>(D);
9762
9763 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009764 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009765
9766 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9767 // threadprivate variables cannot appear in a map clause.
9768 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009769 auto DVar = DSAStack->getTopDSA(VD, false);
9770 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9771 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9772 continue;
9773 }
9774
Samuel Antao5de996e2016-01-22 20:21:36 +00009775 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9776 // A list item cannot appear in both a map clause and a data-sharing
9777 // attribute clause on the same construct.
9778 //
9779 // TODO: Implement this check - it cannot currently be tested because of
9780 // missing implementation of the other data sharing clauses in target
9781 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009782
Samuel Antao5de996e2016-01-22 20:21:36 +00009783 // Check conflicts with other map clause expressions. We check the conflicts
9784 // with the current construct separately from the enclosing data
9785 // environment, because the restrictions are different.
9786 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9787 /*CurrentRegionOnly=*/true))
9788 break;
9789 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9790 /*CurrentRegionOnly=*/false))
9791 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009792
Samuel Antao5de996e2016-01-22 20:21:36 +00009793 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9794 // If the type of a list item is a reference to a type T then the type will
9795 // be considered to be T for all purposes of this clause.
9796 QualType Type = D->getType();
9797 if (Type->isReferenceType())
9798 Type = Type->getPointeeType();
9799
9800 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009801 // A list item must have a mappable type.
9802 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9803 DSAStack, Type))
9804 continue;
9805
Samuel Antaodf67fc42016-01-19 19:15:56 +00009806 // target enter data
9807 // OpenMP [2.10.2, Restrictions, p. 99]
9808 // A map-type must be specified in all map clauses and must be either
9809 // to or alloc.
9810 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9811 if (DKind == OMPD_target_enter_data &&
9812 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9813 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009814 << (IsMapTypeImplicit ? 1 : 0)
9815 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009816 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009817 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009818 }
9819
Samuel Antao72590762016-01-19 20:04:50 +00009820 // target exit_data
9821 // OpenMP [2.10.3, Restrictions, p. 102]
9822 // A map-type must be specified in all map clauses and must be either
9823 // from, release, or delete.
9824 DKind = DSAStack->getCurrentDirective();
9825 if (DKind == OMPD_target_exit_data &&
9826 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9827 MapType == OMPC_MAP_delete)) {
9828 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009829 << (IsMapTypeImplicit ? 1 : 0)
9830 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009831 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009832 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009833 }
9834
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009835 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9836 // A list item cannot appear in both a map clause and a data-sharing
9837 // attribute clause on the same construct
9838 if (DKind == OMPD_target && VD) {
9839 auto DVar = DSAStack->getTopDSA(VD, false);
9840 if (isOpenMPPrivate(DVar.CKind)) {
9841 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
9842 << getOpenMPClauseName(DVar.CKind)
9843 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9844 ReportOriginalDSA(*this, DSAStack, D, DVar);
9845 continue;
9846 }
9847 }
9848
Kelvin Li0bff7af2015-11-23 05:32:03 +00009849 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009850 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009851 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009852
Samuel Antao5de996e2016-01-22 20:21:36 +00009853 // We need to produce a map clause even if we don't have variables so that
9854 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009855 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009856 MapTypeModifier, MapType, IsMapTypeImplicit,
9857 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009858}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009859
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00009860QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
9861 TypeResult ParsedType) {
9862 assert(ParsedType.isUsable());
9863
9864 QualType ReductionType = GetTypeFromParser(ParsedType.get());
9865 if (ReductionType.isNull())
9866 return QualType();
9867
9868 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
9869 // A type name in a declare reduction directive cannot be a function type, an
9870 // array type, a reference type, or a type qualified with const, volatile or
9871 // restrict.
9872 if (ReductionType.hasQualifiers()) {
9873 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
9874 return QualType();
9875 }
9876
9877 if (ReductionType->isFunctionType()) {
9878 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
9879 return QualType();
9880 }
9881 if (ReductionType->isReferenceType()) {
9882 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
9883 return QualType();
9884 }
9885 if (ReductionType->isArrayType()) {
9886 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
9887 return QualType();
9888 }
9889 return ReductionType;
9890}
9891
9892Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
9893 Scope *S, DeclContext *DC, DeclarationName Name,
9894 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
9895 AccessSpecifier AS, Decl *PrevDeclInScope) {
9896 SmallVector<Decl *, 8> Decls;
9897 Decls.reserve(ReductionTypes.size());
9898
9899 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
9900 ForRedeclaration);
9901 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
9902 // A reduction-identifier may not be re-declared in the current scope for the
9903 // same type or for a type that is compatible according to the base language
9904 // rules.
9905 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
9906 OMPDeclareReductionDecl *PrevDRD = nullptr;
9907 bool InCompoundScope = true;
9908 if (S != nullptr) {
9909 // Find previous declaration with the same name not referenced in other
9910 // declarations.
9911 FunctionScopeInfo *ParentFn = getEnclosingFunction();
9912 InCompoundScope =
9913 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
9914 LookupName(Lookup, S);
9915 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
9916 /*AllowInlineNamespace=*/false);
9917 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
9918 auto Filter = Lookup.makeFilter();
9919 while (Filter.hasNext()) {
9920 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
9921 if (InCompoundScope) {
9922 auto I = UsedAsPrevious.find(PrevDecl);
9923 if (I == UsedAsPrevious.end())
9924 UsedAsPrevious[PrevDecl] = false;
9925 if (auto *D = PrevDecl->getPrevDeclInScope())
9926 UsedAsPrevious[D] = true;
9927 }
9928 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
9929 PrevDecl->getLocation();
9930 }
9931 Filter.done();
9932 if (InCompoundScope) {
9933 for (auto &PrevData : UsedAsPrevious) {
9934 if (!PrevData.second) {
9935 PrevDRD = PrevData.first;
9936 break;
9937 }
9938 }
9939 }
9940 } else if (PrevDeclInScope != nullptr) {
9941 auto *PrevDRDInScope = PrevDRD =
9942 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
9943 do {
9944 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
9945 PrevDRDInScope->getLocation();
9946 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
9947 } while (PrevDRDInScope != nullptr);
9948 }
9949 for (auto &TyData : ReductionTypes) {
9950 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
9951 bool Invalid = false;
9952 if (I != PreviousRedeclTypes.end()) {
9953 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
9954 << TyData.first;
9955 Diag(I->second, diag::note_previous_definition);
9956 Invalid = true;
9957 }
9958 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
9959 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
9960 Name, TyData.first, PrevDRD);
9961 DC->addDecl(DRD);
9962 DRD->setAccess(AS);
9963 Decls.push_back(DRD);
9964 if (Invalid)
9965 DRD->setInvalidDecl();
9966 else
9967 PrevDRD = DRD;
9968 }
9969
9970 return DeclGroupPtrTy::make(
9971 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
9972}
9973
9974void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
9975 auto *DRD = cast<OMPDeclareReductionDecl>(D);
9976
9977 // Enter new function scope.
9978 PushFunctionScope();
9979 getCurFunction()->setHasBranchProtectedScope();
9980 getCurFunction()->setHasOMPDeclareReductionCombiner();
9981
9982 if (S != nullptr)
9983 PushDeclContext(S, DRD);
9984 else
9985 CurContext = DRD;
9986
9987 PushExpressionEvaluationContext(PotentiallyEvaluated);
9988
9989 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009990 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
9991 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
9992 // uses semantics of argument handles by value, but it should be passed by
9993 // reference. C lang does not support references, so pass all parameters as
9994 // pointers.
9995 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00009996 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009997 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00009998 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
9999 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10000 // uses semantics of argument handles by value, but it should be passed by
10001 // reference. C lang does not support references, so pass all parameters as
10002 // pointers.
10003 // Create 'T omp_out;' variable.
10004 auto *OmpOutParm =
10005 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10006 if (S != nullptr) {
10007 PushOnScopeChains(OmpInParm, S);
10008 PushOnScopeChains(OmpOutParm, S);
10009 } else {
10010 DRD->addDecl(OmpInParm);
10011 DRD->addDecl(OmpOutParm);
10012 }
10013}
10014
10015void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10016 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10017 DiscardCleanupsInEvaluationContext();
10018 PopExpressionEvaluationContext();
10019
10020 PopDeclContext();
10021 PopFunctionScopeInfo();
10022
10023 if (Combiner != nullptr)
10024 DRD->setCombiner(Combiner);
10025 else
10026 DRD->setInvalidDecl();
10027}
10028
10029void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10030 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10031
10032 // Enter new function scope.
10033 PushFunctionScope();
10034 getCurFunction()->setHasBranchProtectedScope();
10035
10036 if (S != nullptr)
10037 PushDeclContext(S, DRD);
10038 else
10039 CurContext = DRD;
10040
10041 PushExpressionEvaluationContext(PotentiallyEvaluated);
10042
10043 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010044 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10045 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10046 // uses semantics of argument handles by value, but it should be passed by
10047 // reference. C lang does not support references, so pass all parameters as
10048 // pointers.
10049 // Create 'T omp_priv;' variable.
10050 auto *OmpPrivParm =
10051 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010052 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10053 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10054 // uses semantics of argument handles by value, but it should be passed by
10055 // reference. C lang does not support references, so pass all parameters as
10056 // pointers.
10057 // Create 'T omp_orig;' variable.
10058 auto *OmpOrigParm =
10059 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010060 if (S != nullptr) {
10061 PushOnScopeChains(OmpPrivParm, S);
10062 PushOnScopeChains(OmpOrigParm, S);
10063 } else {
10064 DRD->addDecl(OmpPrivParm);
10065 DRD->addDecl(OmpOrigParm);
10066 }
10067}
10068
10069void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10070 Expr *Initializer) {
10071 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10072 DiscardCleanupsInEvaluationContext();
10073 PopExpressionEvaluationContext();
10074
10075 PopDeclContext();
10076 PopFunctionScopeInfo();
10077
10078 if (Initializer != nullptr)
10079 DRD->setInitializer(Initializer);
10080 else
10081 DRD->setInvalidDecl();
10082}
10083
10084Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10085 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10086 for (auto *D : DeclReductions.get()) {
10087 if (IsValid) {
10088 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10089 if (S != nullptr)
10090 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10091 } else
10092 D->setInvalidDecl();
10093 }
10094 return DeclReductions;
10095}
10096
Kelvin Li099bb8c2015-11-24 20:50:12 +000010097OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10098 SourceLocation StartLoc,
10099 SourceLocation LParenLoc,
10100 SourceLocation EndLoc) {
10101 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010102
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010103 // OpenMP [teams Constrcut, Restrictions]
10104 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010105 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10106 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010107 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010108
10109 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10110}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010111
10112OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10113 SourceLocation StartLoc,
10114 SourceLocation LParenLoc,
10115 SourceLocation EndLoc) {
10116 Expr *ValExpr = ThreadLimit;
10117
10118 // OpenMP [teams Constrcut, Restrictions]
10119 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010120 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10121 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010122 return nullptr;
10123
10124 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10125 EndLoc);
10126}
Alexey Bataeva0569352015-12-01 10:17:31 +000010127
10128OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10129 SourceLocation StartLoc,
10130 SourceLocation LParenLoc,
10131 SourceLocation EndLoc) {
10132 Expr *ValExpr = Priority;
10133
10134 // OpenMP [2.9.1, task Constrcut]
10135 // The priority-value is a non-negative numerical scalar expression.
10136 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10137 /*StrictlyPositive=*/false))
10138 return nullptr;
10139
10140 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10141}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010142
10143OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10144 SourceLocation StartLoc,
10145 SourceLocation LParenLoc,
10146 SourceLocation EndLoc) {
10147 Expr *ValExpr = Grainsize;
10148
10149 // OpenMP [2.9.2, taskloop Constrcut]
10150 // The parameter of the grainsize clause must be a positive integer
10151 // expression.
10152 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10153 /*StrictlyPositive=*/true))
10154 return nullptr;
10155
10156 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10157}
Alexey Bataev382967a2015-12-08 12:06:20 +000010158
10159OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10160 SourceLocation StartLoc,
10161 SourceLocation LParenLoc,
10162 SourceLocation EndLoc) {
10163 Expr *ValExpr = NumTasks;
10164
10165 // OpenMP [2.9.2, taskloop Constrcut]
10166 // The parameter of the num_tasks clause must be a positive integer
10167 // expression.
10168 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10169 /*StrictlyPositive=*/true))
10170 return nullptr;
10171
10172 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10173}
10174
Alexey Bataev28c75412015-12-15 08:19:24 +000010175OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10176 SourceLocation LParenLoc,
10177 SourceLocation EndLoc) {
10178 // OpenMP [2.13.2, critical construct, Description]
10179 // ... where hint-expression is an integer constant expression that evaluates
10180 // to a valid lock hint.
10181 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10182 if (HintExpr.isInvalid())
10183 return nullptr;
10184 return new (Context)
10185 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10186}
10187
Carlo Bertollib4adf552016-01-15 18:50:31 +000010188OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10189 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10190 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10191 SourceLocation EndLoc) {
10192 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10193 std::string Values;
10194 Values += "'";
10195 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10196 Values += "'";
10197 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10198 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10199 return nullptr;
10200 }
10201 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010202 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010203 if (ChunkSize) {
10204 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10205 !ChunkSize->isInstantiationDependent() &&
10206 !ChunkSize->containsUnexpandedParameterPack()) {
10207 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10208 ExprResult Val =
10209 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10210 if (Val.isInvalid())
10211 return nullptr;
10212
10213 ValExpr = Val.get();
10214
10215 // OpenMP [2.7.1, Restrictions]
10216 // chunk_size must be a loop invariant integer expression with a positive
10217 // value.
10218 llvm::APSInt Result;
10219 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10220 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10221 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10222 << "dist_schedule" << ChunkSize->getSourceRange();
10223 return nullptr;
10224 }
10225 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010226 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10227 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10228 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010229 }
10230 }
10231 }
10232
10233 return new (Context)
10234 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010235 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010236}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010237
10238OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10239 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10240 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10241 SourceLocation KindLoc, SourceLocation EndLoc) {
10242 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10243 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10244 Kind != OMPC_DEFAULTMAP_scalar) {
10245 std::string Value;
10246 SourceLocation Loc;
10247 Value += "'";
10248 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10249 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10250 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10251 Loc = MLoc;
10252 } else {
10253 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10254 OMPC_DEFAULTMAP_scalar);
10255 Loc = KindLoc;
10256 }
10257 Value += "'";
10258 Diag(Loc, diag::err_omp_unexpected_clause_value)
10259 << Value << getOpenMPClauseName(OMPC_defaultmap);
10260 return nullptr;
10261 }
10262
10263 return new (Context)
10264 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10265}