blob: 287bb3681840260a3fa732a53a9d8a22a6414b7a [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000018#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000025#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000027#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000028#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000031#include "clang/Sema/Scope.h"
32#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000033#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000034using namespace clang;
35
Alexey Bataev758e55e2013-09-06 18:03:48 +000036//===----------------------------------------------------------------------===//
37// Stack of data-sharing attributes for variables
38//===----------------------------------------------------------------------===//
39
40namespace {
41/// \brief Default data sharing attributes, which can be applied to directive.
42enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000043 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000046};
Alexey Bataev7ff55242014-06-19 09:13:45 +000047
Alexey Bataevf29276e2014-06-18 04:14:57 +000048template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000049 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000050 bool operator()(T Kind) {
51 for (auto KindEl : Arr)
52 if (KindEl == Kind)
53 return true;
54 return false;
55 }
56
57private:
58 ArrayRef<T> Arr;
59};
Alexey Bataev23b69422014-06-18 07:08:49 +000060struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000061 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000062 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000063};
64
65typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
66typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000067
68/// \brief Stack for tracking declarations used in OpenMP directives and
69/// clauses and their data-sharing attributes.
70class DSAStackTy {
71public:
72 struct DSAVarData {
73 OpenMPDirectiveKind DKind;
74 OpenMPClauseKind CKind;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000075 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000076 DeclRefExpr *PrivateCopy;
Alexey Bataevbae9a792014-06-27 10:37:06 +000077 SourceLocation ImplicitDSALoc;
78 DSAVarData()
79 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
Alexey Bataev90c228f2016-02-08 09:29:13 +000080 PrivateCopy(nullptr), ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000081 };
Alexey Bataeved09d242014-05-28 05:53:51 +000082
Alexey Bataev758e55e2013-09-06 18:03:48 +000083private:
Samuel Antao5de996e2016-01-22 20:21:36 +000084 typedef SmallVector<Expr *, 4> MapInfo;
85
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 struct DSAInfo {
87 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000088 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000089 DeclRefExpr *PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000091 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
92 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000093 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
94 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000095 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000096 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
97 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000098
99 struct SharingMapTy {
100 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000101 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000102 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000103 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000105 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000106 OpenMPDirectiveKind Directive;
107 DeclarationNameInfo DirectiveName;
108 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000109 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000110 /// \brief first argument (Expr *) contains optional argument of the
111 /// 'ordered' clause, the second one is true if the regions has 'ordered'
112 /// clause, false otherwise.
113 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000114 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000115 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000116 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000117 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000118 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000119 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000120 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000121 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000122 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000123 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000124 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000125 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000126 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000127 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000128 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000129 };
130
Axel Naumann323862e2016-02-03 10:45:22 +0000131 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000132
133 /// \brief Stack of used declaration and their data-sharing attributes.
134 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000135 /// \brief true, if check for DSA must be from parent directive, false, if
136 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000137 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000138 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000139 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000140 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141
142 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
143
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000144 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000145
146 /// \brief Checks if the variable is a local for OpenMP region.
147 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000148
Alexey Bataev758e55e2013-09-06 18:03:48 +0000149public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000150 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000151 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
152 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000153
Alexey Bataevaac108a2015-06-23 04:51:00 +0000154 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
155 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000156
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000157 bool isForceVarCapturing() const { return ForceCapturing; }
158 void setForceVarCapturing(bool V) { ForceCapturing = V; }
159
Alexey Bataev758e55e2013-09-06 18:03:48 +0000160 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000161 Scope *CurScope, SourceLocation Loc) {
162 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
163 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000164 }
165
166 void pop() {
167 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
168 Stack.pop_back();
169 }
170
Alexey Bataev28c75412015-12-15 08:19:24 +0000171 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
172 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
173 }
174 const std::pair<OMPCriticalDirective *, llvm::APSInt>
175 getCriticalWithHint(const DeclarationNameInfo &Name) const {
176 auto I = Criticals.find(Name.getAsString());
177 if (I != Criticals.end())
178 return I->second;
179 return std::make_pair(nullptr, llvm::APSInt());
180 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000181 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000182 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000183 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000184 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000185
Alexey Bataev9c821032015-04-30 04:23:23 +0000186 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000187 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000188 /// \brief Check if the specified variable is a loop control variable for
189 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000190 /// \return The index of the loop control variable in the list of associated
191 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000192 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000193 /// \brief Check if the specified variable is a loop control variable for
194 /// parent region.
195 /// \return The index of the loop control variable in the list of associated
196 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000197 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000198 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
199 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000200 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000201
Alexey Bataev758e55e2013-09-06 18:03:48 +0000202 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000203 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
204 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000205
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Returns data sharing attributes from top of the stack for the
207 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000208 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000209 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000210 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000211 /// \brief Checks if the specified variables has data-sharing attributes which
212 /// match specified \a CPred predicate in any directive which matches \a DPred
213 /// predicate.
214 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000215 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000216 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000217 /// \brief Checks if the specified variables has data-sharing attributes which
218 /// match specified \a CPred predicate in any innermost directive which
219 /// matches \a DPred predicate.
220 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000221 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
222 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000223 /// \brief Checks if the specified variables has explicit data-sharing
224 /// attributes which match specified \a CPred predicate at the specified
225 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000226 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000227 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
228 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000229
230 /// \brief Returns true if the directive at level \Level matches in the
231 /// specified \a DPred predicate.
232 bool hasExplicitDirective(
233 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
234 unsigned Level);
235
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000236 /// \brief Finds a directive which matches specified \a DPred predicate.
237 template <class NamedDirectivesPredicate>
238 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000239
Alexey Bataev758e55e2013-09-06 18:03:48 +0000240 /// \brief Returns currently analyzed directive.
241 OpenMPDirectiveKind getCurrentDirective() const {
242 return Stack.back().Directive;
243 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000244 /// \brief Returns parent directive.
245 OpenMPDirectiveKind getParentDirective() const {
246 if (Stack.size() > 2)
247 return Stack[Stack.size() - 2].Directive;
248 return OMPD_unknown;
249 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000250 /// \brief Return the directive associated with the provided scope.
251 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000252
253 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000254 void setDefaultDSANone(SourceLocation Loc) {
255 Stack.back().DefaultAttr = DSA_none;
256 Stack.back().DefaultAttrLoc = Loc;
257 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000259 void setDefaultDSAShared(SourceLocation Loc) {
260 Stack.back().DefaultAttr = DSA_shared;
261 Stack.back().DefaultAttrLoc = Loc;
262 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000263
264 DefaultDataSharingAttributes getDefaultDSA() const {
265 return Stack.back().DefaultAttr;
266 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000267 SourceLocation getDefaultDSALocation() const {
268 return Stack.back().DefaultAttrLoc;
269 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270
Alexey Bataevf29276e2014-06-18 04:14:57 +0000271 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000273 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000274 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000275 }
276
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000277 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000278 void setOrderedRegion(bool IsOrdered, Expr *Param) {
279 Stack.back().OrderedRegion.setInt(IsOrdered);
280 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000281 }
282 /// \brief Returns true, if parent region is ordered (has associated
283 /// 'ordered' clause), false - otherwise.
284 bool isParentOrderedRegion() const {
285 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000286 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000287 return false;
288 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000289 /// \brief Returns optional parameter for the ordered region.
290 Expr *getParentOrderedRegionParam() const {
291 if (Stack.size() > 2)
292 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
293 return nullptr;
294 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000295 /// \brief Marks current region as nowait (it has a 'nowait' clause).
296 void setNowaitRegion(bool IsNowait = true) {
297 Stack.back().NowaitRegion = IsNowait;
298 }
299 /// \brief Returns true, if parent region is nowait (has associated
300 /// 'nowait' clause), false - otherwise.
301 bool isParentNowaitRegion() const {
302 if (Stack.size() > 2)
303 return Stack[Stack.size() - 2].NowaitRegion;
304 return false;
305 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000306 /// \brief Marks parent region as cancel region.
307 void setParentCancelRegion(bool Cancel = true) {
308 if (Stack.size() > 2)
309 Stack[Stack.size() - 2].CancelRegion =
310 Stack[Stack.size() - 2].CancelRegion || Cancel;
311 }
312 /// \brief Return true if current region has inner cancel construct.
313 bool isCancelRegion() const {
314 return Stack.back().CancelRegion;
315 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000316
Alexey Bataev9c821032015-04-30 04:23:23 +0000317 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000318 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000319 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000320 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000321
Alexey Bataev13314bf2014-10-09 04:18:56 +0000322 /// \brief Marks current target region as one with closely nested teams
323 /// region.
324 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
325 if (Stack.size() > 2)
326 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
327 }
328 /// \brief Returns true, if current region has closely nested teams region.
329 bool hasInnerTeamsRegion() const {
330 return getInnerTeamsRegionLoc().isValid();
331 }
332 /// \brief Returns location of the nested teams region (if any).
333 SourceLocation getInnerTeamsRegionLoc() const {
334 if (Stack.size() > 1)
335 return Stack.back().InnerTeamsRegionLoc;
336 return SourceLocation();
337 }
338
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000339 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000340 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000341 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000342
Samuel Antao5de996e2016-01-22 20:21:36 +0000343 // Do the check specified in MapInfoCheck and return true if any issue is
344 // found.
345 template <class MapInfoCheck>
346 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
347 MapInfoCheck Check) {
348 auto SI = Stack.rbegin();
349 auto SE = Stack.rend();
350
351 if (SI == SE)
352 return false;
353
354 if (CurrentRegionOnly) {
355 SE = std::next(SI);
356 } else {
357 ++SI;
358 }
359
360 for (; SI != SE; ++SI) {
361 auto MI = SI->MappedDecls.find(VD);
362 if (MI != SI->MappedDecls.end()) {
363 for (Expr *E : MI->second) {
364 if (Check(E))
365 return true;
366 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 }
368 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000369 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000370 }
371
Samuel Antao5de996e2016-01-22 20:21:36 +0000372 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000373 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000374 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000375 }
376 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000377};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000378bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
379 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000380 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000381 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000382}
Alexey Bataeved09d242014-05-28 05:53:51 +0000383} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000384
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000385static ValueDecl *getCanonicalDecl(ValueDecl *D) {
386 auto *VD = dyn_cast<VarDecl>(D);
387 auto *FD = dyn_cast<FieldDecl>(D);
388 if (VD != nullptr) {
389 VD = VD->getCanonicalDecl();
390 D = VD;
391 } else {
392 assert(FD);
393 FD = FD->getCanonicalDecl();
394 D = FD;
395 }
396 return D;
397}
398
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000399DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000400 ValueDecl *D) {
401 D = getCanonicalDecl(D);
402 auto *VD = dyn_cast<VarDecl>(D);
403 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000405 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000406 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
407 // in a region but not in construct]
408 // File-scope or namespace-scope variables referenced in called routines
409 // in the region are shared unless they appear in a threadprivate
410 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000411 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000412 DVar.CKind = OMPC_shared;
413
414 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
415 // in a region but not in construct]
416 // Variables with static storage duration that are declared in called
417 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000418 if (VD && VD->hasGlobalStorage())
419 DVar.CKind = OMPC_shared;
420
421 // Non-static data members are shared by default.
422 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000423 DVar.CKind = OMPC_shared;
424
Alexey Bataev758e55e2013-09-06 18:03:48 +0000425 return DVar;
426 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000427
Alexey Bataev758e55e2013-09-06 18:03:48 +0000428 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000429 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
430 // in a Construct, C/C++, predetermined, p.1]
431 // Variables with automatic storage duration that are declared in a scope
432 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000433 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
434 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000435 DVar.CKind = OMPC_private;
436 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000437 }
438
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 // Explicitly specified attributes and local variables with predetermined
440 // attributes.
441 if (Iter->SharingMap.count(D)) {
442 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000443 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000444 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000445 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000446 return DVar;
447 }
448
449 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
450 // in a Construct, C/C++, implicitly determined, p.1]
451 // In a parallel or task construct, the data-sharing attributes of these
452 // variables are determined by the default clause, if present.
453 switch (Iter->DefaultAttr) {
454 case DSA_shared:
455 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000456 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000457 return DVar;
458 case DSA_none:
459 return DVar;
460 case DSA_unspecified:
461 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
462 // in a Construct, implicitly determined, p.2]
463 // In a parallel construct, if no default clause is present, these
464 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000465 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000466 if (isOpenMPParallelDirective(DVar.DKind) ||
467 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000468 DVar.CKind = OMPC_shared;
469 return DVar;
470 }
471
472 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
473 // in a Construct, implicitly determined, p.4]
474 // In a task construct, if no default clause is present, a variable that in
475 // the enclosing context is determined to be shared by all implicit tasks
476 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 if (DVar.DKind == OMPD_task) {
478 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000479 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000480 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000481 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
482 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000483 // in a Construct, implicitly determined, p.6]
484 // In a task construct, if no default clause is present, a variable
485 // whose data-sharing attribute is not determined by the rules above is
486 // firstprivate.
487 DVarTemp = getDSA(I, D);
488 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000489 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000490 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000491 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000492 return DVar;
493 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000494 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000495 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496 }
497 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000498 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000499 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000500 return DVar;
501 }
502 }
503 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
504 // in a Construct, implicitly determined, p.3]
505 // For constructs other than task, if no default clause is present, these
506 // variables inherit their data-sharing attributes from the enclosing
507 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000508 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000509}
510
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000511Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000512 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000513 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000514 auto It = Stack.back().AlignedMap.find(D);
515 if (It == Stack.back().AlignedMap.end()) {
516 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
517 Stack.back().AlignedMap[D] = NewDE;
518 return nullptr;
519 } else {
520 assert(It->second && "Unexpected nullptr expr in the aligned map");
521 return It->second;
522 }
523 return nullptr;
524}
525
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000526void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000527 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000528 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000529 Stack.back().LCVMap.insert(
530 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000531}
532
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000533DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000534 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000535 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000536 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
537 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000538}
539
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000540DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000541 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000542 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000543 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
544 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000545 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000546}
547
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000548ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000549 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
550 if (Stack[Stack.size() - 2].LCVMap.size() < I)
551 return nullptr;
552 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000553 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000554 return Pair.first;
555 }
556 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000557}
558
Alexey Bataev90c228f2016-02-08 09:29:13 +0000559void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
560 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000561 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000562 if (A == OMPC_threadprivate) {
563 Stack[0].SharingMap[D].Attributes = A;
564 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000565 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000566 } else {
567 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
568 Stack.back().SharingMap[D].Attributes = A;
569 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000570 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
571 if (PrivateCopy)
572 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000573 }
574}
575
Alexey Bataeved09d242014-05-28 05:53:51 +0000576bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000577 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000578 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000579 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000580 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000581 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000582 ++I;
583 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000584 if (I == E)
585 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000586 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000587 Scope *CurScope = getCurScope();
588 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000589 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000590 }
591 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000592 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000593 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594}
595
Alexey Bataev39f915b82015-05-08 10:41:21 +0000596/// \brief Build a variable declaration for OpenMP loop iteration variable.
597static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000598 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000599 DeclContext *DC = SemaRef.CurContext;
600 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
601 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
602 VarDecl *Decl =
603 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000604 if (Attrs) {
605 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
606 I != E; ++I)
607 Decl->addAttr(*I);
608 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000609 Decl->setImplicit();
610 return Decl;
611}
612
613static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
614 SourceLocation Loc,
615 bool RefersToCapture = false) {
616 D->setReferenced();
617 D->markUsed(S.Context);
618 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
619 SourceLocation(), D, RefersToCapture, Loc, Ty,
620 VK_LValue);
621}
622
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000623DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
624 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000625 DSAVarData DVar;
626
627 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
628 // in a Construct, C/C++, predetermined, p.1]
629 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000630 auto *VD = dyn_cast<VarDecl>(D);
631 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
632 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000633 SemaRef.getLangOpts().OpenMPUseTLS &&
634 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000635 (VD && VD->getStorageClass() == SC_Register &&
636 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
637 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000638 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000639 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000640 }
641 if (Stack[0].SharingMap.count(D)) {
642 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
643 DVar.CKind = OMPC_threadprivate;
644 return DVar;
645 }
646
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000647 if (Stack.size() == 1) {
648 // Not in OpenMP execution region and top scope was already checked.
649 return DVar;
650 }
651
Alexey Bataev758e55e2013-09-06 18:03:48 +0000652 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000653 // in a Construct, C/C++, predetermined, p.4]
654 // Static data members are shared.
655 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
656 // in a Construct, C/C++, predetermined, p.7]
657 // Variables with static storage duration that are declared in a scope
658 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000659 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000660 DSAVarData DVarTemp =
661 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
662 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000663 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000665 DVar.CKind = OMPC_shared;
666 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000667 }
668
669 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000670 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
671 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000672 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
673 // in a Construct, C/C++, predetermined, p.6]
674 // Variables with const qualified type having no mutable member are
675 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000676 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000677 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000678 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
679 if (auto *CTD = CTSD->getSpecializedTemplate())
680 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000681 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000682 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
683 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000684 // Variables with const-qualified type having no mutable member may be
685 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000686 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
687 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000688 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
689 return DVar;
690
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691 DVar.CKind = OMPC_shared;
692 return DVar;
693 }
694
Alexey Bataev758e55e2013-09-06 18:03:48 +0000695 // Explicitly specified attributes and local variables with predetermined
696 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000697 auto StartI = std::next(Stack.rbegin());
698 auto EndI = std::prev(Stack.rend());
699 if (FromParent && StartI != EndI) {
700 StartI = std::next(StartI);
701 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000702 auto I = std::prev(StartI);
703 if (I->SharingMap.count(D)) {
704 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000705 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000706 DVar.CKind = I->SharingMap[D].Attributes;
707 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000708 }
709
710 return DVar;
711}
712
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000713DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
714 bool FromParent) {
715 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000716 auto StartI = Stack.rbegin();
717 auto EndI = std::prev(Stack.rend());
718 if (FromParent && StartI != EndI) {
719 StartI = std::next(StartI);
720 }
721 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000722}
723
Alexey Bataevf29276e2014-06-18 04:14:57 +0000724template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000725DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000726 DirectivesPredicate DPred,
727 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000728 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000729 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000730 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000731 if (FromParent && StartI != EndI) {
732 StartI = std::next(StartI);
733 }
734 for (auto I = StartI, EE = EndI; I != EE; ++I) {
735 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000736 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000737 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000738 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000739 return DVar;
740 }
741 return DSAVarData();
742}
743
Alexey Bataevf29276e2014-06-18 04:14:57 +0000744template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000745DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000746DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000747 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000748 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000749 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000750 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000751 if (FromParent && StartI != EndI) {
752 StartI = std::next(StartI);
753 }
754 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000755 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000756 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000757 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000758 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000759 return DVar;
760 return DSAVarData();
761 }
762 return DSAVarData();
763}
764
Alexey Bataevaac108a2015-06-23 04:51:00 +0000765bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000766 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000767 unsigned Level) {
768 if (CPred(ClauseKindMode))
769 return true;
770 if (isClauseParsingMode())
771 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000772 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000773 auto StartI = Stack.rbegin();
774 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000775 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000776 return false;
777 std::advance(StartI, Level);
778 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
779 CPred(StartI->SharingMap[D].Attributes);
780}
781
Samuel Antao4be30e92015-10-02 17:14:03 +0000782bool DSAStackTy::hasExplicitDirective(
783 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
784 unsigned Level) {
785 if (isClauseParsingMode())
786 ++Level;
787 auto StartI = Stack.rbegin();
788 auto EndI = std::prev(Stack.rend());
789 if (std::distance(StartI, EndI) <= (int)Level)
790 return false;
791 std::advance(StartI, Level);
792 return DPred(StartI->Directive);
793}
794
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000795template <class NamedDirectivesPredicate>
796bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
797 auto StartI = std::next(Stack.rbegin());
798 auto EndI = std::prev(Stack.rend());
799 if (FromParent && StartI != EndI) {
800 StartI = std::next(StartI);
801 }
802 for (auto I = StartI, EE = EndI; I != EE; ++I) {
803 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
804 return true;
805 }
806 return false;
807}
808
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000809OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
810 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
811 if (I->CurScope == S)
812 return I->Directive;
813 return OMPD_unknown;
814}
815
Alexey Bataev758e55e2013-09-06 18:03:48 +0000816void Sema::InitDataSharingAttributesStack() {
817 VarDataSharingAttributesStack = new DSAStackTy(*this);
818}
819
820#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
821
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000822bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000823 const CapturedRegionScopeInfo *RSI) {
824 assert(LangOpts.OpenMP && "OpenMP is not allowed");
825
826 auto &Ctx = getASTContext();
827 bool IsByRef = true;
828
829 // Find the directive that is associated with the provided scope.
830 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000831 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000832
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000833 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000834 // This table summarizes how a given variable should be passed to the device
835 // given its type and the clauses where it appears. This table is based on
836 // the description in OpenMP 4.5 [2.10.4, target Construct] and
837 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
838 //
839 // =========================================================================
840 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
841 // | |(tofrom:scalar)| | pvt | | | |
842 // =========================================================================
843 // | scl | | | | - | | bycopy|
844 // | scl | | - | x | - | - | bycopy|
845 // | scl | | x | - | - | - | null |
846 // | scl | x | | | - | | byref |
847 // | scl | x | - | x | - | - | bycopy|
848 // | scl | x | x | - | - | - | null |
849 // | scl | | - | - | - | x | byref |
850 // | scl | x | - | - | - | x | byref |
851 //
852 // | agg | n.a. | | | - | | byref |
853 // | agg | n.a. | - | x | - | - | byref |
854 // | agg | n.a. | x | - | - | - | null |
855 // | agg | n.a. | - | - | - | x | byref |
856 // | agg | n.a. | - | - | - | x[] | byref |
857 //
858 // | ptr | n.a. | | | - | | bycopy|
859 // | ptr | n.a. | - | x | - | - | bycopy|
860 // | ptr | n.a. | x | - | - | - | null |
861 // | ptr | n.a. | - | - | - | x | byref |
862 // | ptr | n.a. | - | - | - | x[] | bycopy|
863 // | ptr | n.a. | - | - | x | | bycopy|
864 // | ptr | n.a. | - | - | x | x | bycopy|
865 // | ptr | n.a. | - | - | x | x[] | bycopy|
866 // =========================================================================
867 // Legend:
868 // scl - scalar
869 // ptr - pointer
870 // agg - aggregate
871 // x - applies
872 // - - invalid in this combination
873 // [] - mapped with an array section
874 // byref - should be mapped by reference
875 // byval - should be mapped by value
876 // null - initialize a local variable to null on the device
877 //
878 // Observations:
879 // - All scalar declarations that show up in a map clause have to be passed
880 // by reference, because they may have been mapped in the enclosing data
881 // environment.
882 // - If the scalar value does not fit the size of uintptr, it has to be
883 // passed by reference, regardless the result in the table above.
884 // - For pointers mapped by value that have either an implicit map or an
885 // array section, the runtime library may pass the NULL value to the
886 // device instead of the value passed to it by the compiler.
887
888 // FIXME: Right now, only implicit maps are implemented. Properly mapping
889 // values requires having the map, private, and firstprivate clauses SEMA
890 // and parsing in place, which we don't yet.
891
892 if (Ty->isReferenceType())
893 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
894 IsByRef = !Ty->isScalarType();
895 }
896
897 // When passing data by value, we need to make sure it fits the uintptr size
898 // and alignment, because the runtime library only deals with uintptr types.
899 // If it does not fit the uintptr size, we need to pass the data by reference
900 // instead.
901 if (!IsByRef &&
902 (Ctx.getTypeSizeInChars(Ty) >
903 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000904 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000905 IsByRef = true;
906
907 return IsByRef;
908}
909
Alexey Bataev90c228f2016-02-08 09:29:13 +0000910VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000911 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000912 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000913
914 // If we are attempting to capture a global variable in a directive with
915 // 'target' we return true so that this global is also mapped to the device.
916 //
917 // FIXME: If the declaration is enclosed in a 'declare target' directive,
918 // then it should not be captured. Therefore, an extra check has to be
919 // inserted here once support for 'declare target' is added.
920 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000921 auto *VD = dyn_cast<VarDecl>(D);
922 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000923 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000924 !DSAStack->isClauseParsingMode())
925 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000926 if (DSAStack->getCurScope() &&
927 DSAStack->hasDirective(
928 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
929 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000930 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000931 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000932 false))
933 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000934 }
935
Alexey Bataev48977c32015-08-04 08:10:48 +0000936 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
937 (!DSAStack->isClauseParsingMode() ||
938 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000939 auto &&Info = DSAStack->isLoopControlVariable(D);
940 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000941 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000942 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000943 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000944 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000945 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000946 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000947 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000948 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000949 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000950 if (DVarPrivate.CKind != OMPC_unknown)
951 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000952 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000953 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000954}
955
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000956bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000957 assert(LangOpts.OpenMP && "OpenMP is not allowed");
958 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000959 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000960}
961
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000962bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000963 assert(LangOpts.OpenMP && "OpenMP is not allowed");
964 // Return true if the current level is no longer enclosed in a target region.
965
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000966 auto *VD = dyn_cast<VarDecl>(D);
967 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000968 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
969 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000970}
971
Alexey Bataeved09d242014-05-28 05:53:51 +0000972void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000973
974void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
975 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000976 Scope *CurScope, SourceLocation Loc) {
977 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978 PushExpressionEvaluationContext(PotentiallyEvaluated);
979}
980
Alexey Bataevaac108a2015-06-23 04:51:00 +0000981void Sema::StartOpenMPClause(OpenMPClauseKind K) {
982 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000983}
984
Alexey Bataevaac108a2015-06-23 04:51:00 +0000985void Sema::EndOpenMPClause() {
986 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000987}
988
Alexey Bataev758e55e2013-09-06 18:03:48 +0000989void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000990 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
991 // A variable of class type (or array thereof) that appears in a lastprivate
992 // clause requires an accessible, unambiguous default constructor for the
993 // class type, unless the list item is also specified in a firstprivate
994 // clause.
995 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000996 for (auto *C : D->clauses()) {
997 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
998 SmallVector<Expr *, 8> PrivateCopies;
999 for (auto *DE : Clause->varlists()) {
1000 if (DE->isValueDependent() || DE->isTypeDependent()) {
1001 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001002 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001003 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001004 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001005 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1006 QualType Type = VD->getType().getNonReferenceType();
1007 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001008 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001009 // Generate helper private variable and initialize it with the
1010 // default value. The address of the original variable is replaced
1011 // by the address of the new private variable in CodeGen. This new
1012 // variable is not added to IdResolver, so the code in the OpenMP
1013 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001014 auto *VDPrivate = buildVarDecl(
1015 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001016 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001017 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1018 if (VDPrivate->isInvalidDecl())
1019 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001020 PrivateCopies.push_back(buildDeclRefExpr(
1021 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001022 } else {
1023 // The variable is also a firstprivate, so initialization sequence
1024 // for private copy is generated already.
1025 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001026 }
1027 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001028 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001029 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001030 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001031 }
1032 }
1033 }
1034
Alexey Bataev758e55e2013-09-06 18:03:48 +00001035 DSAStack->pop();
1036 DiscardCleanupsInEvaluationContext();
1037 PopExpressionEvaluationContext();
1038}
1039
Alexey Bataev5a3af132016-03-29 08:58:54 +00001040static bool
1041FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1042 Expr *NumIterations, Sema &SemaRef, Scope *S);
Alexander Musman3276a272015-03-21 10:12:56 +00001043
Alexey Bataeva769e072013-03-22 06:34:35 +00001044namespace {
1045
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001046class VarDeclFilterCCC : public CorrectionCandidateCallback {
1047private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001048 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001049
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001051 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001052 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001053 NamedDecl *ND = Candidate.getCorrectionDecl();
1054 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1055 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001056 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1057 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001058 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001059 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001060 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001061};
Alexey Bataeved09d242014-05-28 05:53:51 +00001062} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001063
1064ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1065 CXXScopeSpec &ScopeSpec,
1066 const DeclarationNameInfo &Id) {
1067 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1068 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1069
1070 if (Lookup.isAmbiguous())
1071 return ExprError();
1072
1073 VarDecl *VD;
1074 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001075 if (TypoCorrection Corrected = CorrectTypo(
1076 Id, LookupOrdinaryName, CurScope, nullptr,
1077 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001078 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001079 PDiag(Lookup.empty()
1080 ? diag::err_undeclared_var_use_suggest
1081 : diag::err_omp_expected_var_arg_suggest)
1082 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001083 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001084 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001085 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1086 : diag::err_omp_expected_var_arg)
1087 << Id.getName();
1088 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001089 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001090 } else {
1091 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001092 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001093 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1094 return ExprError();
1095 }
1096 }
1097 Lookup.suppressDiagnostics();
1098
1099 // OpenMP [2.9.2, Syntax, C/C++]
1100 // Variables must be file-scope, namespace-scope, or static block-scope.
1101 if (!VD->hasGlobalStorage()) {
1102 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001103 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1104 bool IsDecl =
1105 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001106 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001107 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1108 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001109 return ExprError();
1110 }
1111
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001112 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1113 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001114 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1115 // A threadprivate directive for file-scope variables must appear outside
1116 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001117 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1118 !getCurLexicalContext()->isTranslationUnit()) {
1119 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001120 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1121 bool IsDecl =
1122 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1123 Diag(VD->getLocation(),
1124 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1125 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001126 return ExprError();
1127 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001128 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1129 // A threadprivate directive for static class member variables must appear
1130 // in the class definition, in the same scope in which the member
1131 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001132 if (CanonicalVD->isStaticDataMember() &&
1133 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1134 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001135 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1136 bool IsDecl =
1137 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1138 Diag(VD->getLocation(),
1139 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1140 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001141 return ExprError();
1142 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001143 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1144 // A threadprivate directive for namespace-scope variables must appear
1145 // outside any definition or declaration other than the namespace
1146 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001147 if (CanonicalVD->getDeclContext()->isNamespace() &&
1148 (!getCurLexicalContext()->isFileContext() ||
1149 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1150 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001151 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1152 bool IsDecl =
1153 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1154 Diag(VD->getLocation(),
1155 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1156 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001157 return ExprError();
1158 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001159 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1160 // A threadprivate directive for static block-scope variables must appear
1161 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001162 if (CanonicalVD->isStaticLocal() && CurScope &&
1163 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001164 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001165 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1166 bool IsDecl =
1167 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1168 Diag(VD->getLocation(),
1169 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1170 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 return ExprError();
1172 }
1173
1174 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1175 // A threadprivate directive must lexically precede all references to any
1176 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001177 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001178 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001179 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 return ExprError();
1181 }
1182
1183 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001184 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1185 SourceLocation(), VD,
1186 /*RefersToEnclosingVariableOrCapture=*/false,
1187 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001188}
1189
Alexey Bataeved09d242014-05-28 05:53:51 +00001190Sema::DeclGroupPtrTy
1191Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1192 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001194 CurContext->addDecl(D);
1195 return DeclGroupPtrTy::make(DeclGroupRef(D));
1196 }
David Blaikie0403cb12016-01-15 23:43:25 +00001197 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001198}
1199
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001200namespace {
1201class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1202 Sema &SemaRef;
1203
1204public:
1205 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1206 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1207 if (VD->hasLocalStorage()) {
1208 SemaRef.Diag(E->getLocStart(),
1209 diag::err_omp_local_var_in_threadprivate_init)
1210 << E->getSourceRange();
1211 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1212 << VD << VD->getSourceRange();
1213 return true;
1214 }
1215 }
1216 return false;
1217 }
1218 bool VisitStmt(const Stmt *S) {
1219 for (auto Child : S->children()) {
1220 if (Child && Visit(Child))
1221 return true;
1222 }
1223 return false;
1224 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001225 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001226};
1227} // namespace
1228
Alexey Bataeved09d242014-05-28 05:53:51 +00001229OMPThreadPrivateDecl *
1230Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001231 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001232 for (auto &RefExpr : VarList) {
1233 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001234 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1235 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001236
Alexey Bataev376b4a42016-02-09 09:41:09 +00001237 // Mark variable as used.
1238 VD->setReferenced();
1239 VD->markUsed(Context);
1240
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001241 QualType QType = VD->getType();
1242 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1243 // It will be analyzed later.
1244 Vars.push_back(DE);
1245 continue;
1246 }
1247
Alexey Bataeva769e072013-03-22 06:34:35 +00001248 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1249 // A threadprivate variable must not have an incomplete type.
1250 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001251 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001252 continue;
1253 }
1254
1255 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1256 // A threadprivate variable must not have a reference type.
1257 if (VD->getType()->isReferenceType()) {
1258 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001259 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1260 bool IsDecl =
1261 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1262 Diag(VD->getLocation(),
1263 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1264 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001265 continue;
1266 }
1267
Samuel Antaof8b50122015-07-13 22:54:53 +00001268 // Check if this is a TLS variable. If TLS is not being supported, produce
1269 // the corresponding diagnostic.
1270 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1271 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1272 getLangOpts().OpenMPUseTLS &&
1273 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001274 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1275 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001276 Diag(ILoc, diag::err_omp_var_thread_local)
1277 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001278 bool IsDecl =
1279 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1280 Diag(VD->getLocation(),
1281 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1282 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001283 continue;
1284 }
1285
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001286 // Check if initial value of threadprivate variable reference variable with
1287 // local storage (it is not supported by runtime).
1288 if (auto Init = VD->getAnyInitializer()) {
1289 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001290 if (Checker.Visit(Init))
1291 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001292 }
1293
Alexey Bataeved09d242014-05-28 05:53:51 +00001294 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001295 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001296 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1297 Context, SourceRange(Loc, Loc)));
1298 if (auto *ML = Context.getASTMutationListener())
1299 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001300 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001301 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001302 if (!Vars.empty()) {
1303 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1304 Vars);
1305 D->setAccess(AS_public);
1306 }
1307 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001308}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001309
Alexey Bataev7ff55242014-06-19 09:13:45 +00001310static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001311 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001312 bool IsLoopIterVar = false) {
1313 if (DVar.RefExpr) {
1314 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1315 << getOpenMPClauseName(DVar.CKind);
1316 return;
1317 }
1318 enum {
1319 PDSA_StaticMemberShared,
1320 PDSA_StaticLocalVarShared,
1321 PDSA_LoopIterVarPrivate,
1322 PDSA_LoopIterVarLinear,
1323 PDSA_LoopIterVarLastprivate,
1324 PDSA_ConstVarShared,
1325 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001326 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001327 PDSA_LocalVarPrivate,
1328 PDSA_Implicit
1329 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001330 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001331 auto ReportLoc = D->getLocation();
1332 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001333 if (IsLoopIterVar) {
1334 if (DVar.CKind == OMPC_private)
1335 Reason = PDSA_LoopIterVarPrivate;
1336 else if (DVar.CKind == OMPC_lastprivate)
1337 Reason = PDSA_LoopIterVarLastprivate;
1338 else
1339 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001340 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1341 Reason = PDSA_TaskVarFirstprivate;
1342 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001343 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001344 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001345 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001346 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001347 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001348 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001349 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001350 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001351 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001352 ReportHint = true;
1353 Reason = PDSA_LocalVarPrivate;
1354 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001355 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001356 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001357 << Reason << ReportHint
1358 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1359 } else if (DVar.ImplicitDSALoc.isValid()) {
1360 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1361 << getOpenMPClauseName(DVar.CKind);
1362 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001363}
1364
Alexey Bataev758e55e2013-09-06 18:03:48 +00001365namespace {
1366class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1367 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001368 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001369 bool ErrorFound;
1370 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001371 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001372 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001373
Alexey Bataev758e55e2013-09-06 18:03:48 +00001374public:
1375 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001376 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001377 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001378 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1379 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001380
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001381 auto DVar = Stack->getTopDSA(VD, false);
1382 // Check if the variable has explicit DSA set and stop analysis if it so.
1383 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001384
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001385 auto ELoc = E->getExprLoc();
1386 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001387 // The default(none) clause requires that each variable that is referenced
1388 // in the construct, and does not have a predetermined data-sharing
1389 // attribute, must have its data-sharing attribute explicitly determined
1390 // by being listed in a data-sharing attribute clause.
1391 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001392 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001393 VarsWithInheritedDSA.count(VD) == 0) {
1394 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001395 return;
1396 }
1397
1398 // OpenMP [2.9.3.6, Restrictions, p.2]
1399 // A list item that appears in a reduction clause of the innermost
1400 // enclosing worksharing or parallel construct may not be accessed in an
1401 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001402 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001403 [](OpenMPDirectiveKind K) -> bool {
1404 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001405 isOpenMPWorksharingDirective(K) ||
1406 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001407 },
1408 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001409 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1410 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001411 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1412 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001413 return;
1414 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001415
1416 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001417 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001418 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001419 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001420 }
1421 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001422 void VisitMemberExpr(MemberExpr *E) {
1423 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1424 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1425 auto DVar = Stack->getTopDSA(FD, false);
1426 // Check if the variable has explicit DSA set and stop analysis if it
1427 // so.
1428 if (DVar.RefExpr)
1429 return;
1430
1431 auto ELoc = E->getExprLoc();
1432 auto DKind = Stack->getCurrentDirective();
1433 // OpenMP [2.9.3.6, Restrictions, p.2]
1434 // A list item that appears in a reduction clause of the innermost
1435 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001436 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001437 DVar =
1438 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1439 [](OpenMPDirectiveKind K) -> bool {
1440 return isOpenMPParallelDirective(K) ||
1441 isOpenMPWorksharingDirective(K) ||
1442 isOpenMPTeamsDirective(K);
1443 },
1444 false);
1445 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1446 ErrorFound = true;
1447 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1448 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1449 return;
1450 }
1451
1452 // Define implicit data-sharing attributes for task.
1453 DVar = Stack->getImplicitDSA(FD, false);
1454 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1455 ImplicitFirstprivate.push_back(E);
1456 }
1457 }
1458 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001459 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001460 for (auto *C : S->clauses()) {
1461 // Skip analysis of arguments of implicitly defined firstprivate clause
1462 // for task directives.
1463 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1464 for (auto *CC : C->children()) {
1465 if (CC)
1466 Visit(CC);
1467 }
1468 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469 }
1470 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001471 for (auto *C : S->children()) {
1472 if (C && !isa<OMPExecutableDirective>(C))
1473 Visit(C);
1474 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001475 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476
1477 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001478 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001479 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001480 return VarsWithInheritedDSA;
1481 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001482
Alexey Bataev7ff55242014-06-19 09:13:45 +00001483 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1484 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001485};
Alexey Bataeved09d242014-05-28 05:53:51 +00001486} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001487
Alexey Bataevbae9a792014-06-27 10:37:06 +00001488void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001489 switch (DKind) {
1490 case OMPD_parallel: {
1491 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001492 QualType KmpInt32PtrTy =
1493 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001494 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001495 std::make_pair(".global_tid.", KmpInt32PtrTy),
1496 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1497 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001498 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001499 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1500 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001501 break;
1502 }
1503 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001504 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001505 std::make_pair(StringRef(), QualType()) // __context with shared vars
1506 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001507 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1508 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001509 break;
1510 }
1511 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001512 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001513 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001514 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001515 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1516 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001517 break;
1518 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001519 case OMPD_for_simd: {
1520 Sema::CapturedParamNameType Params[] = {
1521 std::make_pair(StringRef(), QualType()) // __context with shared vars
1522 };
1523 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1524 Params);
1525 break;
1526 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001527 case OMPD_sections: {
1528 Sema::CapturedParamNameType Params[] = {
1529 std::make_pair(StringRef(), QualType()) // __context with shared vars
1530 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001531 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1532 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001533 break;
1534 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001535 case OMPD_section: {
1536 Sema::CapturedParamNameType Params[] = {
1537 std::make_pair(StringRef(), QualType()) // __context with shared vars
1538 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001539 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1540 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001541 break;
1542 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001543 case OMPD_single: {
1544 Sema::CapturedParamNameType Params[] = {
1545 std::make_pair(StringRef(), QualType()) // __context with shared vars
1546 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001547 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1548 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001549 break;
1550 }
Alexander Musman80c22892014-07-17 08:54:58 +00001551 case OMPD_master: {
1552 Sema::CapturedParamNameType Params[] = {
1553 std::make_pair(StringRef(), QualType()) // __context with shared vars
1554 };
1555 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1556 Params);
1557 break;
1558 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001559 case OMPD_critical: {
1560 Sema::CapturedParamNameType Params[] = {
1561 std::make_pair(StringRef(), QualType()) // __context with shared vars
1562 };
1563 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1564 Params);
1565 break;
1566 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001567 case OMPD_parallel_for: {
1568 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001569 QualType KmpInt32PtrTy =
1570 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001571 Sema::CapturedParamNameType Params[] = {
1572 std::make_pair(".global_tid.", KmpInt32PtrTy),
1573 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1574 std::make_pair(StringRef(), QualType()) // __context with shared vars
1575 };
1576 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1577 Params);
1578 break;
1579 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001580 case OMPD_parallel_for_simd: {
1581 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001582 QualType KmpInt32PtrTy =
1583 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001584 Sema::CapturedParamNameType Params[] = {
1585 std::make_pair(".global_tid.", KmpInt32PtrTy),
1586 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1587 std::make_pair(StringRef(), QualType()) // __context with shared vars
1588 };
1589 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1590 Params);
1591 break;
1592 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001593 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001594 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001595 QualType KmpInt32PtrTy =
1596 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001597 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001598 std::make_pair(".global_tid.", KmpInt32PtrTy),
1599 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001600 std::make_pair(StringRef(), QualType()) // __context with shared vars
1601 };
1602 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1603 Params);
1604 break;
1605 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001606 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001607 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001608 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1609 FunctionProtoType::ExtProtoInfo EPI;
1610 EPI.Variadic = true;
1611 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001612 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001613 std::make_pair(".global_tid.", KmpInt32Ty),
1614 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001615 std::make_pair(".privates.",
1616 Context.VoidPtrTy.withConst().withRestrict()),
1617 std::make_pair(
1618 ".copy_fn.",
1619 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001620 std::make_pair(StringRef(), QualType()) // __context with shared vars
1621 };
1622 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1623 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001624 // Mark this captured region as inlined, because we don't use outlined
1625 // function directly.
1626 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1627 AlwaysInlineAttr::CreateImplicit(
1628 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001629 break;
1630 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001631 case OMPD_ordered: {
1632 Sema::CapturedParamNameType Params[] = {
1633 std::make_pair(StringRef(), QualType()) // __context with shared vars
1634 };
1635 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1636 Params);
1637 break;
1638 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001639 case OMPD_atomic: {
1640 Sema::CapturedParamNameType Params[] = {
1641 std::make_pair(StringRef(), QualType()) // __context with shared vars
1642 };
1643 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1644 Params);
1645 break;
1646 }
Michael Wong65f367f2015-07-21 13:44:28 +00001647 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001648 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001649 case OMPD_target_parallel:
1650 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001651 Sema::CapturedParamNameType Params[] = {
1652 std::make_pair(StringRef(), QualType()) // __context with shared vars
1653 };
1654 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1655 Params);
1656 break;
1657 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001658 case OMPD_teams: {
1659 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001660 QualType KmpInt32PtrTy =
1661 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001662 Sema::CapturedParamNameType Params[] = {
1663 std::make_pair(".global_tid.", KmpInt32PtrTy),
1664 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1665 std::make_pair(StringRef(), QualType()) // __context with shared vars
1666 };
1667 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1668 Params);
1669 break;
1670 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001671 case OMPD_taskgroup: {
1672 Sema::CapturedParamNameType Params[] = {
1673 std::make_pair(StringRef(), QualType()) // __context with shared vars
1674 };
1675 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1676 Params);
1677 break;
1678 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001679 case OMPD_taskloop: {
1680 Sema::CapturedParamNameType Params[] = {
1681 std::make_pair(StringRef(), QualType()) // __context with shared vars
1682 };
1683 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1684 Params);
1685 break;
1686 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001687 case OMPD_taskloop_simd: {
1688 Sema::CapturedParamNameType Params[] = {
1689 std::make_pair(StringRef(), QualType()) // __context with shared vars
1690 };
1691 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1692 Params);
1693 break;
1694 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001695 case OMPD_distribute: {
1696 Sema::CapturedParamNameType Params[] = {
1697 std::make_pair(StringRef(), QualType()) // __context with shared vars
1698 };
1699 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1700 Params);
1701 break;
1702 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001703 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001704 case OMPD_taskyield:
1705 case OMPD_barrier:
1706 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001707 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001708 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001709 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001710 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001711 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001712 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001713 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001714 case OMPD_declare_target:
1715 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00001716 llvm_unreachable("OpenMP Directive is not allowed");
1717 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001718 llvm_unreachable("Unknown OpenMP directive");
1719 }
1720}
1721
Alexey Bataev3392d762016-02-16 11:18:12 +00001722static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001723 Expr *CaptureExpr, bool WithInit,
1724 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001725 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001726 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001727 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001728 QualType Ty = Init->getType();
1729 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1730 if (S.getLangOpts().CPlusPlus)
1731 Ty = C.getLValueReferenceType(Ty);
1732 else {
1733 Ty = C.getPointerType(Ty);
1734 ExprResult Res =
1735 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1736 if (!Res.isUsable())
1737 return nullptr;
1738 Init = Res.get();
1739 }
Alexey Bataev61205072016-03-02 04:57:40 +00001740 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001741 }
1742 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001743 if (!WithInit)
1744 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001745 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001746 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1747 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001748 return CED;
1749}
1750
Alexey Bataev61205072016-03-02 04:57:40 +00001751static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1752 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001753 OMPCapturedExprDecl *CD;
1754 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1755 CD = cast<OMPCapturedExprDecl>(VD);
1756 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001757 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1758 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001759 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001760 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001761}
1762
Alexey Bataev5a3af132016-03-29 08:58:54 +00001763static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1764 if (!Ref) {
1765 auto *CD =
1766 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1767 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1768 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1769 CaptureExpr->getExprLoc());
1770 }
1771 ExprResult Res = Ref;
1772 if (!S.getLangOpts().CPlusPlus &&
1773 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1774 Ref->getType()->isPointerType())
1775 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1776 if (!Res.isUsable())
1777 return ExprError();
1778 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001779}
1780
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001781StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1782 ArrayRef<OMPClause *> Clauses) {
1783 if (!S.isUsable()) {
1784 ActOnCapturedRegionError();
1785 return StmtError();
1786 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001787
1788 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001789 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001790 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001791 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001792 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001793 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001794 Clause->getClauseKind() == OMPC_copyprivate ||
1795 (getLangOpts().OpenMPUseTLS &&
1796 getASTContext().getTargetInfo().isTLSSupported() &&
1797 Clause->getClauseKind() == OMPC_copyin)) {
1798 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001799 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001800 for (auto *VarRef : Clause->children()) {
1801 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001802 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001803 }
1804 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001805 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001806 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001807 // Mark all variables in private list clauses as used in inner region.
1808 // Required for proper codegen of combined directives.
1809 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001810 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001811 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1812 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001813 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1814 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001815 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001816 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1817 if (auto *E = C->getPostUpdateExpr())
1818 MarkDeclarationsReferencedInExpr(E);
1819 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001820 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001821 if (Clause->getClauseKind() == OMPC_schedule)
1822 SC = cast<OMPScheduleClause>(Clause);
1823 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001824 OC = cast<OMPOrderedClause>(Clause);
1825 else if (Clause->getClauseKind() == OMPC_linear)
1826 LCs.push_back(cast<OMPLinearClause>(Clause));
1827 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001828 bool ErrorFound = false;
1829 // OpenMP, 2.7.1 Loop Construct, Restrictions
1830 // The nonmonotonic modifier cannot be specified if an ordered clause is
1831 // specified.
1832 if (SC &&
1833 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1834 SC->getSecondScheduleModifier() ==
1835 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1836 OC) {
1837 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1838 ? SC->getFirstScheduleModifierLoc()
1839 : SC->getSecondScheduleModifierLoc(),
1840 diag::err_omp_schedule_nonmonotonic_ordered)
1841 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1842 ErrorFound = true;
1843 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001844 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1845 for (auto *C : LCs) {
1846 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1847 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1848 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001849 ErrorFound = true;
1850 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001851 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1852 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1853 OC->getNumForLoops()) {
1854 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1855 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1856 ErrorFound = true;
1857 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001858 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001859 ActOnCapturedRegionError();
1860 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001861 }
1862 return ActOnCapturedRegionEnd(S.get());
1863}
1864
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001865static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1866 OpenMPDirectiveKind CurrentRegion,
1867 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001868 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001869 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001870 // Allowed nesting of constructs
1871 // +------------------+-----------------+------------------------------------+
1872 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1873 // +------------------+-----------------+------------------------------------+
1874 // | parallel | parallel | * |
1875 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001876 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001877 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001878 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001879 // | parallel | simd | * |
1880 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001881 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001882 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001883 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001884 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001885 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001886 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001887 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001888 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001889 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001890 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001891 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001892 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001893 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001894 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001895 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001896 // | parallel | target parallel | * |
1897 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001898 // | parallel | target enter | * |
1899 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001900 // | parallel | target exit | * |
1901 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001902 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001903 // | parallel | cancellation | |
1904 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001905 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001906 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001907 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001908 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001909 // +------------------+-----------------+------------------------------------+
1910 // | for | parallel | * |
1911 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001912 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001913 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001914 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001915 // | for | simd | * |
1916 // | for | sections | + |
1917 // | for | section | + |
1918 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001919 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001920 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001921 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001922 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001923 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001924 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001925 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001926 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001927 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001928 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001929 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001930 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001931 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001932 // | for | target parallel | * |
1933 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001934 // | for | target enter | * |
1935 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001936 // | for | target exit | * |
1937 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001938 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001939 // | for | cancellation | |
1940 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001941 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001942 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001943 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001944 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001945 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001946 // | master | parallel | * |
1947 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001948 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001949 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001950 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001951 // | master | simd | * |
1952 // | master | sections | + |
1953 // | master | section | + |
1954 // | master | single | + |
1955 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001956 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001957 // | master |parallel sections| * |
1958 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001959 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001960 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001961 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001962 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001963 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001964 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001965 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001966 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001967 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001968 // | master | target parallel | * |
1969 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001970 // | master | target enter | * |
1971 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001972 // | master | target exit | * |
1973 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001974 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001975 // | master | cancellation | |
1976 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001977 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001978 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001979 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001980 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001981 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001982 // | critical | parallel | * |
1983 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001984 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001985 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001986 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001987 // | critical | simd | * |
1988 // | critical | sections | + |
1989 // | critical | section | + |
1990 // | critical | single | + |
1991 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001992 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001993 // | critical |parallel sections| * |
1994 // | critical | task | * |
1995 // | critical | taskyield | * |
1996 // | critical | barrier | + |
1997 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001998 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001999 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002000 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002001 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002002 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002003 // | critical | target parallel | * |
2004 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002005 // | critical | target enter | * |
2006 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002007 // | critical | target exit | * |
2008 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002009 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002010 // | critical | cancellation | |
2011 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002012 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002013 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002014 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002015 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002016 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002017 // | simd | parallel | |
2018 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002019 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002020 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002021 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002022 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002023 // | simd | sections | |
2024 // | simd | section | |
2025 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002026 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002027 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002028 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002029 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002030 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002031 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002032 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002033 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002034 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002035 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002036 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002037 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002038 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002039 // | simd | target parallel | |
2040 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002041 // | simd | target enter | |
2042 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002043 // | simd | target exit | |
2044 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002045 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002046 // | simd | cancellation | |
2047 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002048 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002049 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002050 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002051 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002052 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002053 // | for simd | parallel | |
2054 // | for simd | for | |
2055 // | for simd | for simd | |
2056 // | for simd | master | |
2057 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002058 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002059 // | for simd | sections | |
2060 // | for simd | section | |
2061 // | for simd | single | |
2062 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002063 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002064 // | for simd |parallel sections| |
2065 // | for simd | task | |
2066 // | for simd | taskyield | |
2067 // | for simd | barrier | |
2068 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002069 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002070 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002071 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002072 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002073 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002074 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002075 // | for simd | target parallel | |
2076 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002077 // | for simd | target enter | |
2078 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002079 // | for simd | target exit | |
2080 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002081 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002082 // | for simd | cancellation | |
2083 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002084 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002085 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002086 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002087 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002088 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002089 // | parallel for simd| parallel | |
2090 // | parallel for simd| for | |
2091 // | parallel for simd| for simd | |
2092 // | parallel for simd| master | |
2093 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002094 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002095 // | parallel for simd| sections | |
2096 // | parallel for simd| section | |
2097 // | parallel for simd| single | |
2098 // | parallel for simd| parallel for | |
2099 // | parallel for simd|parallel for simd| |
2100 // | parallel for simd|parallel sections| |
2101 // | parallel for simd| task | |
2102 // | parallel for simd| taskyield | |
2103 // | parallel for simd| barrier | |
2104 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002105 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002106 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002107 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002108 // | parallel for simd| atomic | |
2109 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002110 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002111 // | parallel for simd| target parallel | |
2112 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002113 // | parallel for simd| target enter | |
2114 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002115 // | parallel for simd| target exit | |
2116 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002117 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002118 // | parallel for simd| cancellation | |
2119 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002120 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002121 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002122 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002123 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002124 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002125 // | sections | parallel | * |
2126 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002127 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002128 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002129 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002130 // | sections | simd | * |
2131 // | sections | sections | + |
2132 // | sections | section | * |
2133 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002134 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002135 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002136 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002137 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002138 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002139 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002140 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002141 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002142 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002143 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002144 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002145 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002146 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002147 // | sections | target parallel | * |
2148 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002149 // | sections | target enter | * |
2150 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002151 // | sections | target exit | * |
2152 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002153 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002154 // | sections | cancellation | |
2155 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002156 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002157 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002158 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002159 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002160 // +------------------+-----------------+------------------------------------+
2161 // | section | parallel | * |
2162 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002163 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002164 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002165 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002166 // | section | simd | * |
2167 // | section | sections | + |
2168 // | section | section | + |
2169 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002170 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002171 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002172 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002173 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002174 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002175 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002176 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002177 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002178 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002179 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002180 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002181 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002182 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002183 // | section | target parallel | * |
2184 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002185 // | section | target enter | * |
2186 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002187 // | section | target exit | * |
2188 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002189 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002190 // | section | cancellation | |
2191 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002192 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002193 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002194 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002195 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002196 // +------------------+-----------------+------------------------------------+
2197 // | single | parallel | * |
2198 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002199 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002200 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002201 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002202 // | single | simd | * |
2203 // | single | sections | + |
2204 // | single | section | + |
2205 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002206 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002207 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002208 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002209 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002210 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002211 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002212 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002213 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002214 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002215 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002216 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002217 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002218 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002219 // | single | target parallel | * |
2220 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002221 // | single | target enter | * |
2222 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002223 // | single | target exit | * |
2224 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002225 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002226 // | single | cancellation | |
2227 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002228 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002229 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002230 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002231 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002232 // +------------------+-----------------+------------------------------------+
2233 // | parallel for | parallel | * |
2234 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002235 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002236 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002237 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002238 // | parallel for | simd | * |
2239 // | parallel for | sections | + |
2240 // | parallel for | section | + |
2241 // | parallel for | single | + |
2242 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002243 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002244 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002245 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002246 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002247 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002248 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002249 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002250 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002251 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002252 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002253 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002254 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002255 // | parallel for | target parallel | * |
2256 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002257 // | parallel for | target enter | * |
2258 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002259 // | parallel for | target exit | * |
2260 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002261 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002262 // | parallel for | cancellation | |
2263 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002264 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002265 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002266 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002267 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002268 // +------------------+-----------------+------------------------------------+
2269 // | parallel sections| parallel | * |
2270 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002271 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002272 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002273 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002274 // | parallel sections| simd | * |
2275 // | parallel sections| sections | + |
2276 // | parallel sections| section | * |
2277 // | parallel sections| single | + |
2278 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002279 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002280 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002281 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002282 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002283 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002284 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002285 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002286 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002287 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002288 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002289 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002290 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002291 // | parallel sections| target parallel | * |
2292 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002293 // | parallel sections| target enter | * |
2294 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002295 // | parallel sections| target exit | * |
2296 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002297 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002298 // | parallel sections| cancellation | |
2299 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002300 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002301 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002302 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002303 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002304 // +------------------+-----------------+------------------------------------+
2305 // | task | parallel | * |
2306 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002307 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002308 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002309 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002310 // | task | simd | * |
2311 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002312 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002313 // | task | single | + |
2314 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002315 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002316 // | task |parallel sections| * |
2317 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002318 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002319 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002320 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002321 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002322 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002323 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002324 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002325 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002326 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002327 // | task | target parallel | * |
2328 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002329 // | task | target enter | * |
2330 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002331 // | task | target exit | * |
2332 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002333 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002334 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002335 // | | point | ! |
2336 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002337 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002338 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002339 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002340 // +------------------+-----------------+------------------------------------+
2341 // | ordered | parallel | * |
2342 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002343 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002344 // | ordered | master | * |
2345 // | ordered | critical | * |
2346 // | ordered | simd | * |
2347 // | ordered | sections | + |
2348 // | ordered | section | + |
2349 // | ordered | single | + |
2350 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002351 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002352 // | ordered |parallel sections| * |
2353 // | ordered | task | * |
2354 // | ordered | taskyield | * |
2355 // | ordered | barrier | + |
2356 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002357 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002358 // | ordered | flush | * |
2359 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002360 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002361 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002362 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002363 // | ordered | target parallel | * |
2364 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002365 // | ordered | target enter | * |
2366 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002367 // | ordered | target exit | * |
2368 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002369 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002370 // | ordered | cancellation | |
2371 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002372 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002373 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002374 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002375 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002376 // +------------------+-----------------+------------------------------------+
2377 // | atomic | parallel | |
2378 // | atomic | for | |
2379 // | atomic | for simd | |
2380 // | atomic | master | |
2381 // | atomic | critical | |
2382 // | atomic | simd | |
2383 // | atomic | sections | |
2384 // | atomic | section | |
2385 // | atomic | single | |
2386 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002387 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002388 // | atomic |parallel sections| |
2389 // | atomic | task | |
2390 // | atomic | taskyield | |
2391 // | atomic | barrier | |
2392 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002393 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002394 // | atomic | flush | |
2395 // | atomic | ordered | |
2396 // | atomic | atomic | |
2397 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002398 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002399 // | atomic | target parallel | |
2400 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002401 // | atomic | target enter | |
2402 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002403 // | atomic | target exit | |
2404 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002405 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002406 // | atomic | cancellation | |
2407 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002408 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002409 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002410 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002411 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002412 // +------------------+-----------------+------------------------------------+
2413 // | target | parallel | * |
2414 // | target | for | * |
2415 // | target | for simd | * |
2416 // | target | master | * |
2417 // | target | critical | * |
2418 // | target | simd | * |
2419 // | target | sections | * |
2420 // | target | section | * |
2421 // | target | single | * |
2422 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002423 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002424 // | target |parallel sections| * |
2425 // | target | task | * |
2426 // | target | taskyield | * |
2427 // | target | barrier | * |
2428 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002429 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002430 // | target | flush | * |
2431 // | target | ordered | * |
2432 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002433 // | target | target | |
2434 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002435 // | target | target parallel | |
2436 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002437 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002438 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002439 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002440 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002441 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002442 // | target | cancellation | |
2443 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002444 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002445 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002446 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002447 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002448 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002449 // | target parallel | parallel | * |
2450 // | target parallel | for | * |
2451 // | target parallel | for simd | * |
2452 // | target parallel | master | * |
2453 // | target parallel | critical | * |
2454 // | target parallel | simd | * |
2455 // | target parallel | sections | * |
2456 // | target parallel | section | * |
2457 // | target parallel | single | * |
2458 // | target parallel | parallel for | * |
2459 // | target parallel |parallel for simd| * |
2460 // | target parallel |parallel sections| * |
2461 // | target parallel | task | * |
2462 // | target parallel | taskyield | * |
2463 // | target parallel | barrier | * |
2464 // | target parallel | taskwait | * |
2465 // | target parallel | taskgroup | * |
2466 // | target parallel | flush | * |
2467 // | target parallel | ordered | * |
2468 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002469 // | target parallel | target | |
2470 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002471 // | target parallel | target parallel | |
2472 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002473 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002474 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002475 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002476 // | | data | |
2477 // | target parallel | teams | |
2478 // | target parallel | cancellation | |
2479 // | | point | ! |
2480 // | target parallel | cancel | ! |
2481 // | target parallel | taskloop | * |
2482 // | target parallel | taskloop simd | * |
2483 // | target parallel | distribute | |
2484 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002485 // | target parallel | parallel | * |
2486 // | for | | |
2487 // | target parallel | for | * |
2488 // | for | | |
2489 // | target parallel | for simd | * |
2490 // | for | | |
2491 // | target parallel | master | * |
2492 // | for | | |
2493 // | target parallel | critical | * |
2494 // | for | | |
2495 // | target parallel | simd | * |
2496 // | for | | |
2497 // | target parallel | sections | * |
2498 // | for | | |
2499 // | target parallel | section | * |
2500 // | for | | |
2501 // | target parallel | single | * |
2502 // | for | | |
2503 // | target parallel | parallel for | * |
2504 // | for | | |
2505 // | target parallel |parallel for simd| * |
2506 // | for | | |
2507 // | target parallel |parallel sections| * |
2508 // | for | | |
2509 // | target parallel | task | * |
2510 // | for | | |
2511 // | target parallel | taskyield | * |
2512 // | for | | |
2513 // | target parallel | barrier | * |
2514 // | for | | |
2515 // | target parallel | taskwait | * |
2516 // | for | | |
2517 // | target parallel | taskgroup | * |
2518 // | for | | |
2519 // | target parallel | flush | * |
2520 // | for | | |
2521 // | target parallel | ordered | * |
2522 // | for | | |
2523 // | target parallel | atomic | * |
2524 // | for | | |
2525 // | target parallel | target | |
2526 // | for | | |
2527 // | target parallel | target parallel | |
2528 // | for | | |
2529 // | target parallel | target parallel | |
2530 // | for | for | |
2531 // | target parallel | target enter | |
2532 // | for | data | |
2533 // | target parallel | target exit | |
2534 // | for | data | |
2535 // | target parallel | teams | |
2536 // | for | | |
2537 // | target parallel | cancellation | |
2538 // | for | point | ! |
2539 // | target parallel | cancel | ! |
2540 // | for | | |
2541 // | target parallel | taskloop | * |
2542 // | for | | |
2543 // | target parallel | taskloop simd | * |
2544 // | for | | |
2545 // | target parallel | distribute | |
2546 // | for | | |
2547 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002548 // | teams | parallel | * |
2549 // | teams | for | + |
2550 // | teams | for simd | + |
2551 // | teams | master | + |
2552 // | teams | critical | + |
2553 // | teams | simd | + |
2554 // | teams | sections | + |
2555 // | teams | section | + |
2556 // | teams | single | + |
2557 // | teams | parallel for | * |
2558 // | teams |parallel for simd| * |
2559 // | teams |parallel sections| * |
2560 // | teams | task | + |
2561 // | teams | taskyield | + |
2562 // | teams | barrier | + |
2563 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002564 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002565 // | teams | flush | + |
2566 // | teams | ordered | + |
2567 // | teams | atomic | + |
2568 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002569 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002570 // | teams | target parallel | + |
2571 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002572 // | teams | target enter | + |
2573 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002574 // | teams | target exit | + |
2575 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002576 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002577 // | teams | cancellation | |
2578 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002579 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002580 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002581 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002582 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002583 // +------------------+-----------------+------------------------------------+
2584 // | taskloop | parallel | * |
2585 // | taskloop | for | + |
2586 // | taskloop | for simd | + |
2587 // | taskloop | master | + |
2588 // | taskloop | critical | * |
2589 // | taskloop | simd | * |
2590 // | taskloop | sections | + |
2591 // | taskloop | section | + |
2592 // | taskloop | single | + |
2593 // | taskloop | parallel for | * |
2594 // | taskloop |parallel for simd| * |
2595 // | taskloop |parallel sections| * |
2596 // | taskloop | task | * |
2597 // | taskloop | taskyield | * |
2598 // | taskloop | barrier | + |
2599 // | taskloop | taskwait | * |
2600 // | taskloop | taskgroup | * |
2601 // | taskloop | flush | * |
2602 // | taskloop | ordered | + |
2603 // | taskloop | atomic | * |
2604 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002605 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002606 // | taskloop | target parallel | * |
2607 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002608 // | taskloop | target enter | * |
2609 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002610 // | taskloop | target exit | * |
2611 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002612 // | taskloop | teams | + |
2613 // | taskloop | cancellation | |
2614 // | | point | |
2615 // | taskloop | cancel | |
2616 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002617 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002618 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002619 // | taskloop simd | parallel | |
2620 // | taskloop simd | for | |
2621 // | taskloop simd | for simd | |
2622 // | taskloop simd | master | |
2623 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002624 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002625 // | taskloop simd | sections | |
2626 // | taskloop simd | section | |
2627 // | taskloop simd | single | |
2628 // | taskloop simd | parallel for | |
2629 // | taskloop simd |parallel for simd| |
2630 // | taskloop simd |parallel sections| |
2631 // | taskloop simd | task | |
2632 // | taskloop simd | taskyield | |
2633 // | taskloop simd | barrier | |
2634 // | taskloop simd | taskwait | |
2635 // | taskloop simd | taskgroup | |
2636 // | taskloop simd | flush | |
2637 // | taskloop simd | ordered | + (with simd clause) |
2638 // | taskloop simd | atomic | |
2639 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002640 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002641 // | taskloop simd | target parallel | |
2642 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002643 // | taskloop simd | target enter | |
2644 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002645 // | taskloop simd | target exit | |
2646 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002647 // | taskloop simd | teams | |
2648 // | taskloop simd | cancellation | |
2649 // | | point | |
2650 // | taskloop simd | cancel | |
2651 // | taskloop simd | taskloop | |
2652 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002653 // | taskloop simd | distribute | |
2654 // +------------------+-----------------+------------------------------------+
2655 // | distribute | parallel | * |
2656 // | distribute | for | * |
2657 // | distribute | for simd | * |
2658 // | distribute | master | * |
2659 // | distribute | critical | * |
2660 // | distribute | simd | * |
2661 // | distribute | sections | * |
2662 // | distribute | section | * |
2663 // | distribute | single | * |
2664 // | distribute | parallel for | * |
2665 // | distribute |parallel for simd| * |
2666 // | distribute |parallel sections| * |
2667 // | distribute | task | * |
2668 // | distribute | taskyield | * |
2669 // | distribute | barrier | * |
2670 // | distribute | taskwait | * |
2671 // | distribute | taskgroup | * |
2672 // | distribute | flush | * |
2673 // | distribute | ordered | + |
2674 // | distribute | atomic | * |
2675 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002676 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002677 // | distribute | target parallel | |
2678 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002679 // | distribute | target enter | |
2680 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002681 // | distribute | target exit | |
2682 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002683 // | distribute | teams | |
2684 // | distribute | cancellation | + |
2685 // | | point | |
2686 // | distribute | cancel | + |
2687 // | distribute | taskloop | * |
2688 // | distribute | taskloop simd | * |
2689 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002690 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002691 if (Stack->getCurScope()) {
2692 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002693 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002694 bool NestingProhibited = false;
2695 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002696 enum {
2697 NoRecommend,
2698 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002699 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002700 ShouldBeInTargetRegion,
2701 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002702 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002703 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2704 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002705 // OpenMP [2.16, Nesting of Regions]
2706 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002707 // OpenMP [2.8.1,simd Construct, Restrictions]
2708 // An ordered construct with the simd clause is the only OpenMP construct
2709 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002710 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2711 return true;
2712 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002713 if (ParentRegion == OMPD_atomic) {
2714 // OpenMP [2.16, Nesting of Regions]
2715 // OpenMP constructs may not be nested inside an atomic region.
2716 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2717 return true;
2718 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002719 if (CurrentRegion == OMPD_section) {
2720 // OpenMP [2.7.2, sections Construct, Restrictions]
2721 // Orphaned section directives are prohibited. That is, the section
2722 // directives must appear within the sections construct and must not be
2723 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002724 if (ParentRegion != OMPD_sections &&
2725 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002726 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2727 << (ParentRegion != OMPD_unknown)
2728 << getOpenMPDirectiveName(ParentRegion);
2729 return true;
2730 }
2731 return false;
2732 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002733 // Allow some constructs to be orphaned (they could be used in functions,
2734 // called from OpenMP regions with the required preconditions).
2735 if (ParentRegion == OMPD_unknown)
2736 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002737 if (CurrentRegion == OMPD_cancellation_point ||
2738 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002739 // OpenMP [2.16, Nesting of Regions]
2740 // A cancellation point construct for which construct-type-clause is
2741 // taskgroup must be nested inside a task construct. A cancellation
2742 // point construct for which construct-type-clause is not taskgroup must
2743 // be closely nested inside an OpenMP construct that matches the type
2744 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002745 // A cancel construct for which construct-type-clause is taskgroup must be
2746 // nested inside a task construct. A cancel construct for which
2747 // construct-type-clause is not taskgroup must be closely nested inside an
2748 // OpenMP construct that matches the type specified in
2749 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002750 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002751 !((CancelRegion == OMPD_parallel &&
2752 (ParentRegion == OMPD_parallel ||
2753 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002754 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002755 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2756 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002757 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2758 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002759 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2760 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002761 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002762 // OpenMP [2.16, Nesting of Regions]
2763 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002764 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002765 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002766 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002767 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002768 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2769 // OpenMP [2.16, Nesting of Regions]
2770 // A critical region may not be nested (closely or otherwise) inside a
2771 // critical region with the same name. Note that this restriction is not
2772 // sufficient to prevent deadlock.
2773 SourceLocation PreviousCriticalLoc;
2774 bool DeadLock =
2775 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2776 OpenMPDirectiveKind K,
2777 const DeclarationNameInfo &DNI,
2778 SourceLocation Loc)
2779 ->bool {
2780 if (K == OMPD_critical &&
2781 DNI.getName() == CurrentName.getName()) {
2782 PreviousCriticalLoc = Loc;
2783 return true;
2784 } else
2785 return false;
2786 },
2787 false /* skip top directive */);
2788 if (DeadLock) {
2789 SemaRef.Diag(StartLoc,
2790 diag::err_omp_prohibited_region_critical_same_name)
2791 << CurrentName.getName();
2792 if (PreviousCriticalLoc.isValid())
2793 SemaRef.Diag(PreviousCriticalLoc,
2794 diag::note_omp_previous_critical_region);
2795 return true;
2796 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002797 } else if (CurrentRegion == OMPD_barrier) {
2798 // OpenMP [2.16, Nesting of Regions]
2799 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002800 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002801 NestingProhibited =
2802 isOpenMPWorksharingDirective(ParentRegion) ||
2803 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002804 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002805 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002806 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002807 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002808 // OpenMP [2.16, Nesting of Regions]
2809 // A worksharing region may not be closely nested inside a worksharing,
2810 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002811 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002812 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002813 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002814 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002815 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002816 Recommend = ShouldBeInParallelRegion;
2817 } else if (CurrentRegion == OMPD_ordered) {
2818 // OpenMP [2.16, Nesting of Regions]
2819 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002820 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002821 // An ordered region must be closely nested inside a loop region (or
2822 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002823 // OpenMP [2.8.1,simd Construct, Restrictions]
2824 // An ordered construct with the simd clause is the only OpenMP construct
2825 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002826 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002827 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002828 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002829 !(isOpenMPSimdDirective(ParentRegion) ||
2830 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002831 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002832 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2833 // OpenMP [2.16, Nesting of Regions]
2834 // If specified, a teams construct must be contained within a target
2835 // construct.
2836 NestingProhibited = ParentRegion != OMPD_target;
2837 Recommend = ShouldBeInTargetRegion;
2838 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2839 }
2840 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2841 // OpenMP [2.16, Nesting of Regions]
2842 // distribute, parallel, parallel sections, parallel workshare, and the
2843 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2844 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002845 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2846 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002847 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002848 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002849 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2850 // OpenMP 4.5 [2.17 Nesting of Regions]
2851 // The region associated with the distribute construct must be strictly
2852 // nested inside a teams region
2853 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2854 Recommend = ShouldBeInTeamsRegion;
2855 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002856 if (!NestingProhibited &&
2857 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2858 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2859 // OpenMP 4.5 [2.17 Nesting of Regions]
2860 // If a target, target update, target data, target enter data, or
2861 // target exit data construct is encountered during execution of a
2862 // target region, the behavior is unspecified.
2863 NestingProhibited = Stack->hasDirective(
2864 [&OffendingRegion](OpenMPDirectiveKind K,
2865 const DeclarationNameInfo &DNI,
2866 SourceLocation Loc) -> bool {
2867 if (isOpenMPTargetExecutionDirective(K)) {
2868 OffendingRegion = K;
2869 return true;
2870 } else
2871 return false;
2872 },
2873 false /* don't skip top directive */);
2874 CloseNesting = false;
2875 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002876 if (NestingProhibited) {
2877 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002878 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2879 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002880 return true;
2881 }
2882 }
2883 return false;
2884}
2885
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002886static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2887 ArrayRef<OMPClause *> Clauses,
2888 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2889 bool ErrorFound = false;
2890 unsigned NamedModifiersNumber = 0;
2891 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2892 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002893 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002894 for (const auto *C : Clauses) {
2895 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2896 // At most one if clause without a directive-name-modifier can appear on
2897 // the directive.
2898 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2899 if (FoundNameModifiers[CurNM]) {
2900 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2901 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2902 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2903 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002904 } else if (CurNM != OMPD_unknown) {
2905 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002906 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002907 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002908 FoundNameModifiers[CurNM] = IC;
2909 if (CurNM == OMPD_unknown)
2910 continue;
2911 // Check if the specified name modifier is allowed for the current
2912 // directive.
2913 // At most one if clause with the particular directive-name-modifier can
2914 // appear on the directive.
2915 bool MatchFound = false;
2916 for (auto NM : AllowedNameModifiers) {
2917 if (CurNM == NM) {
2918 MatchFound = true;
2919 break;
2920 }
2921 }
2922 if (!MatchFound) {
2923 S.Diag(IC->getNameModifierLoc(),
2924 diag::err_omp_wrong_if_directive_name_modifier)
2925 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2926 ErrorFound = true;
2927 }
2928 }
2929 }
2930 // If any if clause on the directive includes a directive-name-modifier then
2931 // all if clauses on the directive must include a directive-name-modifier.
2932 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2933 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2934 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2935 diag::err_omp_no_more_if_clause);
2936 } else {
2937 std::string Values;
2938 std::string Sep(", ");
2939 unsigned AllowedCnt = 0;
2940 unsigned TotalAllowedNum =
2941 AllowedNameModifiers.size() - NamedModifiersNumber;
2942 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2943 ++Cnt) {
2944 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2945 if (!FoundNameModifiers[NM]) {
2946 Values += "'";
2947 Values += getOpenMPDirectiveName(NM);
2948 Values += "'";
2949 if (AllowedCnt + 2 == TotalAllowedNum)
2950 Values += " or ";
2951 else if (AllowedCnt + 1 != TotalAllowedNum)
2952 Values += Sep;
2953 ++AllowedCnt;
2954 }
2955 }
2956 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2957 diag::err_omp_unnamed_if_clause)
2958 << (TotalAllowedNum > 1) << Values;
2959 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002960 for (auto Loc : NameModifierLoc) {
2961 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2962 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002963 ErrorFound = true;
2964 }
2965 return ErrorFound;
2966}
2967
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002968StmtResult Sema::ActOnOpenMPExecutableDirective(
2969 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2970 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2971 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002972 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002973 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2974 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002975 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002976
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002977 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002978 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002979 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002980 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002981 if (AStmt) {
2982 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2983
2984 // Check default data sharing attributes for referenced variables.
2985 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2986 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2987 if (DSAChecker.isErrorFound())
2988 return StmtError();
2989 // Generate list of implicitly defined firstprivate variables.
2990 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002991
2992 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2993 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2994 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2995 SourceLocation(), SourceLocation())) {
2996 ClausesWithImplicit.push_back(Implicit);
2997 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2998 DSAChecker.getImplicitFirstprivate().size();
2999 } else
3000 ErrorFound = true;
3001 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003002 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003003
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003004 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003005 switch (Kind) {
3006 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003007 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3008 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003009 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003010 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003011 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003012 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3013 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003014 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003015 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003016 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3017 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003018 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003019 case OMPD_for_simd:
3020 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3021 EndLoc, VarsWithInheritedDSA);
3022 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003023 case OMPD_sections:
3024 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3025 EndLoc);
3026 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003027 case OMPD_section:
3028 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003029 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003030 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3031 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003032 case OMPD_single:
3033 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3034 EndLoc);
3035 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003036 case OMPD_master:
3037 assert(ClausesWithImplicit.empty() &&
3038 "No clauses are allowed for 'omp master' directive");
3039 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3040 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003041 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003042 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3043 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003044 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003045 case OMPD_parallel_for:
3046 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3047 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003048 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003049 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003050 case OMPD_parallel_for_simd:
3051 Res = ActOnOpenMPParallelForSimdDirective(
3052 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003053 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003054 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003055 case OMPD_parallel_sections:
3056 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3057 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003058 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003059 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003060 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003061 Res =
3062 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003063 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003064 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003065 case OMPD_taskyield:
3066 assert(ClausesWithImplicit.empty() &&
3067 "No clauses are allowed for 'omp taskyield' directive");
3068 assert(AStmt == nullptr &&
3069 "No associated statement allowed for 'omp taskyield' directive");
3070 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3071 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003072 case OMPD_barrier:
3073 assert(ClausesWithImplicit.empty() &&
3074 "No clauses are allowed for 'omp barrier' directive");
3075 assert(AStmt == nullptr &&
3076 "No associated statement allowed for 'omp barrier' directive");
3077 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3078 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003079 case OMPD_taskwait:
3080 assert(ClausesWithImplicit.empty() &&
3081 "No clauses are allowed for 'omp taskwait' directive");
3082 assert(AStmt == nullptr &&
3083 "No associated statement allowed for 'omp taskwait' directive");
3084 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3085 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003086 case OMPD_taskgroup:
3087 assert(ClausesWithImplicit.empty() &&
3088 "No clauses are allowed for 'omp taskgroup' directive");
3089 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3090 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003091 case OMPD_flush:
3092 assert(AStmt == nullptr &&
3093 "No associated statement allowed for 'omp flush' directive");
3094 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3095 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003096 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003097 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3098 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003099 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003100 case OMPD_atomic:
3101 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3102 EndLoc);
3103 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003104 case OMPD_teams:
3105 Res =
3106 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3107 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003108 case OMPD_target:
3109 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3110 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003111 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003112 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003113 case OMPD_target_parallel:
3114 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3115 StartLoc, EndLoc);
3116 AllowedNameModifiers.push_back(OMPD_target);
3117 AllowedNameModifiers.push_back(OMPD_parallel);
3118 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003119 case OMPD_target_parallel_for:
3120 Res = ActOnOpenMPTargetParallelForDirective(
3121 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3122 AllowedNameModifiers.push_back(OMPD_target);
3123 AllowedNameModifiers.push_back(OMPD_parallel);
3124 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003125 case OMPD_cancellation_point:
3126 assert(ClausesWithImplicit.empty() &&
3127 "No clauses are allowed for 'omp cancellation point' directive");
3128 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3129 "cancellation point' directive");
3130 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3131 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003132 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003133 assert(AStmt == nullptr &&
3134 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003135 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3136 CancelRegion);
3137 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003138 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003139 case OMPD_target_data:
3140 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3141 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003142 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003143 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003144 case OMPD_target_enter_data:
3145 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3146 EndLoc);
3147 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3148 break;
Samuel Antao72590762016-01-19 20:04:50 +00003149 case OMPD_target_exit_data:
3150 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3151 EndLoc);
3152 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3153 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003154 case OMPD_taskloop:
3155 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3156 EndLoc, VarsWithInheritedDSA);
3157 AllowedNameModifiers.push_back(OMPD_taskloop);
3158 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003159 case OMPD_taskloop_simd:
3160 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3161 EndLoc, VarsWithInheritedDSA);
3162 AllowedNameModifiers.push_back(OMPD_taskloop);
3163 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003164 case OMPD_distribute:
3165 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3166 EndLoc, VarsWithInheritedDSA);
3167 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003168 case OMPD_declare_target:
3169 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003170 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003171 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003172 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003173 llvm_unreachable("OpenMP Directive is not allowed");
3174 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003175 llvm_unreachable("Unknown OpenMP directive");
3176 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003177
Alexey Bataev4acb8592014-07-07 13:01:15 +00003178 for (auto P : VarsWithInheritedDSA) {
3179 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3180 << P.first << P.second->getSourceRange();
3181 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003182 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3183
3184 if (!AllowedNameModifiers.empty())
3185 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3186 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003187
Alexey Bataeved09d242014-05-28 05:53:51 +00003188 if (ErrorFound)
3189 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003190 return Res;
3191}
3192
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003193Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3194 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003195 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
3196 ArrayRef<Expr *> Alignments, SourceRange SR) {
3197 assert(Aligneds.size() == Alignments.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003198 if (!DG || DG.get().isNull())
3199 return DeclGroupPtrTy();
3200
3201 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003202 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003203 return DG;
3204 }
3205 auto *ADecl = DG.get().getSingleDecl();
3206 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3207 ADecl = FTD->getTemplatedDecl();
3208
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003209 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3210 if (!FD) {
3211 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003212 return DeclGroupPtrTy();
3213 }
3214
Alexey Bataev2af33e32016-04-07 12:45:37 +00003215 // OpenMP [2.8.2, declare simd construct, Description]
3216 // The parameter of the simdlen clause must be a constant positive integer
3217 // expression.
3218 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003219 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003220 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003221 // OpenMP [2.8.2, declare simd construct, Description]
3222 // The special this pointer can be used as if was one of the arguments to the
3223 // function in any of the linear, aligned, or uniform clauses.
3224 // The uniform clause declares one or more arguments to have an invariant
3225 // value for all concurrent invocations of the function in the execution of a
3226 // single SIMD loop.
3227 for (auto *E : Uniforms) {
3228 E = E->IgnoreParenImpCasts();
3229 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3230 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3231 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3232 FD->getParamDecl(PVD->getFunctionScopeIndex())
3233 ->getCanonicalDecl() == PVD->getCanonicalDecl())
3234 continue;
3235 if (isa<CXXThisExpr>(E))
3236 continue;
3237 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3238 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003239 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003240 // OpenMP [2.8.2, declare simd construct, Description]
3241 // The aligned clause declares that the object to which each list item points
3242 // is aligned to the number of bytes expressed in the optional parameter of
3243 // the aligned clause.
3244 // The special this pointer can be used as if was one of the arguments to the
3245 // function in any of the linear, aligned, or uniform clauses.
3246 // The type of list items appearing in the aligned clause must be array,
3247 // pointer, reference to array, or reference to pointer.
3248 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3249 Expr *AlignedThis = nullptr;
3250 for (auto *E : Aligneds) {
3251 E = E->IgnoreParenImpCasts();
3252 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3253 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3254 auto *CanonPVD = PVD->getCanonicalDecl();
3255 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3256 FD->getParamDecl(PVD->getFunctionScopeIndex())
3257 ->getCanonicalDecl() == CanonPVD) {
3258 // OpenMP [2.8.1, simd construct, Restrictions]
3259 // A list-item cannot appear in more than one aligned clause.
3260 if (AlignedArgs.count(CanonPVD) > 0) {
3261 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3262 << 1 << E->getSourceRange();
3263 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3264 diag::note_omp_explicit_dsa)
3265 << getOpenMPClauseName(OMPC_aligned);
3266 continue;
3267 }
3268 AlignedArgs[CanonPVD] = E;
3269 QualType QTy = PVD->getType()
3270 .getNonReferenceType()
3271 .getUnqualifiedType()
3272 .getCanonicalType();
3273 const Type *Ty = QTy.getTypePtrOrNull();
3274 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3275 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3276 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3277 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3278 }
3279 continue;
3280 }
3281 }
3282 if (isa<CXXThisExpr>(E)) {
3283 if (AlignedThis) {
3284 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3285 << 2 << E->getSourceRange();
3286 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3287 << getOpenMPClauseName(OMPC_aligned);
3288 }
3289 AlignedThis = E;
3290 continue;
3291 }
3292 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3293 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3294 }
3295 // The optional parameter of the aligned clause, alignment, must be a constant
3296 // positive integer expression. If no optional parameter is specified,
3297 // implementation-defined default alignments for SIMD instructions on the
3298 // target platforms are assumed.
3299 SmallVector<Expr *, 4> NewAligns;
3300 for (auto *E : Alignments) {
3301 ExprResult Align;
3302 if (E)
3303 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3304 NewAligns.push_back(Align.get());
3305 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003306 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3307 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003308 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
3309 const_cast<Expr **>(NewAligns.data()), NewAligns.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003310 ADecl->addAttr(NewAttr);
3311 return ConvertDeclToDeclGroup(ADecl);
3312}
3313
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003314StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3315 Stmt *AStmt,
3316 SourceLocation StartLoc,
3317 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003318 if (!AStmt)
3319 return StmtError();
3320
Alexey Bataev9959db52014-05-06 10:08:46 +00003321 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3322 // 1.2.2 OpenMP Language Terminology
3323 // Structured block - An executable statement with a single entry at the
3324 // top and a single exit at the bottom.
3325 // The point of exit cannot be a branch out of the structured block.
3326 // longjmp() and throw() must not violate the entry/exit criteria.
3327 CS->getCapturedDecl()->setNothrow();
3328
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003329 getCurFunction()->setHasBranchProtectedScope();
3330
Alexey Bataev25e5b442015-09-15 12:52:43 +00003331 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3332 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003333}
3334
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003335namespace {
3336/// \brief Helper class for checking canonical form of the OpenMP loops and
3337/// extracting iteration space of each loop in the loop nest, that will be used
3338/// for IR generation.
3339class OpenMPIterationSpaceChecker {
3340 /// \brief Reference to Sema.
3341 Sema &SemaRef;
3342 /// \brief A location for diagnostics (when there is no some better location).
3343 SourceLocation DefaultLoc;
3344 /// \brief A location for diagnostics (when increment is not compatible).
3345 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003346 /// \brief A source location for referring to loop init later.
3347 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003348 /// \brief A source location for referring to condition later.
3349 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003350 /// \brief A source location for referring to increment later.
3351 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003352 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003353 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003354 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003355 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003356 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003357 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003358 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003359 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003360 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003361 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003362 /// \brief This flag is true when condition is one of:
3363 /// Var < UB
3364 /// Var <= UB
3365 /// UB > Var
3366 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003367 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003368 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003369 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003370 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003371 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003372
3373public:
3374 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003375 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003376 /// \brief Check init-expr for canonical loop form and save loop counter
3377 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003378 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003379 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3380 /// for less/greater and for strict/non-strict comparison.
3381 bool CheckCond(Expr *S);
3382 /// \brief Check incr-expr for canonical loop form and return true if it
3383 /// does not conform, otherwise save loop step (#Step).
3384 bool CheckInc(Expr *S);
3385 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003386 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003387 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003388 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003389 /// \brief Source range of the loop init.
3390 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3391 /// \brief Source range of the loop condition.
3392 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3393 /// \brief Source range of the loop increment.
3394 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3395 /// \brief True if the step should be subtracted.
3396 bool ShouldSubtractStep() const { return SubtractStep; }
3397 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003398 Expr *
3399 BuildNumIterations(Scope *S, const bool LimitedType,
3400 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003401 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003402 Expr *BuildPreCond(Scope *S, Expr *Cond,
3403 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003404 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003405 DeclRefExpr *
3406 BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003407 /// \brief Build reference expression to the private counter be used for
3408 /// codegen.
3409 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003410 /// \brief Build initization of the counter be used for codegen.
3411 Expr *BuildCounterInit() const;
3412 /// \brief Build step of the counter be used for codegen.
3413 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003414 /// \brief Return true if any expression is dependent.
3415 bool Dependent() const;
3416
3417private:
3418 /// \brief Check the right-hand side of an assignment in the increment
3419 /// expression.
3420 bool CheckIncRHS(Expr *RHS);
3421 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003422 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003423 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003424 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003425 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003426 /// \brief Helper to set loop increment.
3427 bool SetStep(Expr *NewStep, bool Subtract);
3428};
3429
3430bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003431 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003432 assert(!LB && !UB && !Step);
3433 return false;
3434 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003435 return LCDecl->getType()->isDependentType() ||
3436 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3437 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003438}
3439
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003440static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003441 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3442 E = ExprTemp->getSubExpr();
3443
3444 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3445 E = MTE->GetTemporaryExpr();
3446
3447 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3448 E = Binder->getSubExpr();
3449
3450 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3451 E = ICE->getSubExprAsWritten();
3452 return E->IgnoreParens();
3453}
3454
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003455bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3456 Expr *NewLCRefExpr,
3457 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003458 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003459 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003460 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003461 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003462 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003463 LCDecl = getCanonicalDecl(NewLCDecl);
3464 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003465 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3466 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003467 if ((Ctor->isCopyOrMoveConstructor() ||
3468 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3469 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003470 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003471 LB = NewLB;
3472 return false;
3473}
3474
3475bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003476 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003477 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003478 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3479 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003480 if (!NewUB)
3481 return true;
3482 UB = NewUB;
3483 TestIsLessOp = LessOp;
3484 TestIsStrictOp = StrictOp;
3485 ConditionSrcRange = SR;
3486 ConditionLoc = SL;
3487 return false;
3488}
3489
3490bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3491 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003492 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003493 if (!NewStep)
3494 return true;
3495 if (!NewStep->isValueDependent()) {
3496 // Check that the step is integer expression.
3497 SourceLocation StepLoc = NewStep->getLocStart();
3498 ExprResult Val =
3499 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3500 if (Val.isInvalid())
3501 return true;
3502 NewStep = Val.get();
3503
3504 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3505 // If test-expr is of form var relational-op b and relational-op is < or
3506 // <= then incr-expr must cause var to increase on each iteration of the
3507 // loop. If test-expr is of form var relational-op b and relational-op is
3508 // > or >= then incr-expr must cause var to decrease on each iteration of
3509 // the loop.
3510 // If test-expr is of form b relational-op var and relational-op is < or
3511 // <= then incr-expr must cause var to decrease on each iteration of the
3512 // loop. If test-expr is of form b relational-op var and relational-op is
3513 // > or >= then incr-expr must cause var to increase on each iteration of
3514 // the loop.
3515 llvm::APSInt Result;
3516 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3517 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3518 bool IsConstNeg =
3519 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003520 bool IsConstPos =
3521 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003522 bool IsConstZero = IsConstant && !Result.getBoolValue();
3523 if (UB && (IsConstZero ||
3524 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003525 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003526 SemaRef.Diag(NewStep->getExprLoc(),
3527 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003528 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003529 SemaRef.Diag(ConditionLoc,
3530 diag::note_omp_loop_cond_requres_compatible_incr)
3531 << TestIsLessOp << ConditionSrcRange;
3532 return true;
3533 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003534 if (TestIsLessOp == Subtract) {
3535 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3536 NewStep).get();
3537 Subtract = !Subtract;
3538 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003539 }
3540
3541 Step = NewStep;
3542 SubtractStep = Subtract;
3543 return false;
3544}
3545
Alexey Bataev9c821032015-04-30 04:23:23 +00003546bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003547 // Check init-expr for canonical loop form and save loop counter
3548 // variable - #Var and its initialization value - #LB.
3549 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3550 // var = lb
3551 // integer-type var = lb
3552 // random-access-iterator-type var = lb
3553 // pointer-type var = lb
3554 //
3555 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003556 if (EmitDiags) {
3557 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3558 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003559 return true;
3560 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003561 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003562 if (Expr *E = dyn_cast<Expr>(S))
3563 S = E->IgnoreParens();
3564 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003565 if (BO->getOpcode() == BO_Assign) {
3566 auto *LHS = BO->getLHS()->IgnoreParens();
3567 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3568 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3569 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3570 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3571 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3572 }
3573 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3574 if (ME->isArrow() &&
3575 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3576 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3577 }
3578 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003579 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3580 if (DS->isSingleDecl()) {
3581 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003582 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003583 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003584 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003585 SemaRef.Diag(S->getLocStart(),
3586 diag::ext_omp_loop_not_canonical_init)
3587 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003588 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003589 }
3590 }
3591 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003592 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3593 if (CE->getOperator() == OO_Equal) {
3594 auto *LHS = CE->getArg(0);
3595 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3596 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3597 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3598 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3599 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3600 }
3601 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3602 if (ME->isArrow() &&
3603 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3604 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3605 }
3606 }
3607 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003608
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003609 if (Dependent() || SemaRef.CurContext->isDependentContext())
3610 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003611 if (EmitDiags) {
3612 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3613 << S->getSourceRange();
3614 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003615 return true;
3616}
3617
Alexey Bataev23b69422014-06-18 07:08:49 +00003618/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003619/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003620static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003621 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003622 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003623 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003624 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3625 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003626 if ((Ctor->isCopyOrMoveConstructor() ||
3627 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3628 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003629 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003630 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3631 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3632 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3633 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3634 return getCanonicalDecl(ME->getMemberDecl());
3635 return getCanonicalDecl(VD);
3636 }
3637 }
3638 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3639 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3640 return getCanonicalDecl(ME->getMemberDecl());
3641 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003642}
3643
3644bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3645 // Check test-expr for canonical form, save upper-bound UB, flags for
3646 // less/greater and for strict/non-strict comparison.
3647 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3648 // var relational-op b
3649 // b relational-op var
3650 //
3651 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003652 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003653 return true;
3654 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003655 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003656 SourceLocation CondLoc = S->getLocStart();
3657 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3658 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003659 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003660 return SetUB(BO->getRHS(),
3661 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3662 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3663 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003664 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003665 return SetUB(BO->getLHS(),
3666 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3667 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3668 BO->getSourceRange(), BO->getOperatorLoc());
3669 }
3670 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3671 if (CE->getNumArgs() == 2) {
3672 auto Op = CE->getOperator();
3673 switch (Op) {
3674 case OO_Greater:
3675 case OO_GreaterEqual:
3676 case OO_Less:
3677 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003678 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003679 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3680 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3681 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003682 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003683 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3684 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3685 CE->getOperatorLoc());
3686 break;
3687 default:
3688 break;
3689 }
3690 }
3691 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003692 if (Dependent() || SemaRef.CurContext->isDependentContext())
3693 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003694 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003695 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003696 return true;
3697}
3698
3699bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3700 // RHS of canonical loop form increment can be:
3701 // var + incr
3702 // incr + var
3703 // var - incr
3704 //
3705 RHS = RHS->IgnoreParenImpCasts();
3706 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3707 if (BO->isAdditiveOp()) {
3708 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003709 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003710 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003711 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003712 return SetStep(BO->getLHS(), false);
3713 }
3714 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3715 bool IsAdd = CE->getOperator() == OO_Plus;
3716 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003717 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003718 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003719 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003720 return SetStep(CE->getArg(0), false);
3721 }
3722 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003723 if (Dependent() || SemaRef.CurContext->isDependentContext())
3724 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003725 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003726 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003727 return true;
3728}
3729
3730bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3731 // Check incr-expr for canonical loop form and return true if it
3732 // does not conform.
3733 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3734 // ++var
3735 // var++
3736 // --var
3737 // var--
3738 // var += incr
3739 // var -= incr
3740 // var = var + incr
3741 // var = incr + var
3742 // var = var - incr
3743 //
3744 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003745 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003746 return true;
3747 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003748 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003749 S = S->IgnoreParens();
3750 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003751 if (UO->isIncrementDecrementOp() &&
3752 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003753 return SetStep(
3754 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3755 (UO->isDecrementOp() ? -1 : 1)).get(),
3756 false);
3757 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3758 switch (BO->getOpcode()) {
3759 case BO_AddAssign:
3760 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003761 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003762 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3763 break;
3764 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003765 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003766 return CheckIncRHS(BO->getRHS());
3767 break;
3768 default:
3769 break;
3770 }
3771 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3772 switch (CE->getOperator()) {
3773 case OO_PlusPlus:
3774 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003775 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003776 return SetStep(
3777 SemaRef.ActOnIntegerConstant(
3778 CE->getLocStart(),
3779 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3780 false);
3781 break;
3782 case OO_PlusEqual:
3783 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003784 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003785 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3786 break;
3787 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003788 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003789 return CheckIncRHS(CE->getArg(1));
3790 break;
3791 default:
3792 break;
3793 }
3794 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003795 if (Dependent() || SemaRef.CurContext->isDependentContext())
3796 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003797 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003798 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003799 return true;
3800}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003801
Alexey Bataev5a3af132016-03-29 08:58:54 +00003802static ExprResult
3803tryBuildCapture(Sema &SemaRef, Expr *Capture,
3804 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3805 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3806 return SemaRef.PerformImplicitConversion(
3807 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3808 /*AllowExplicit=*/true);
3809 auto I = Captures.find(Capture);
3810 if (I != Captures.end())
3811 return buildCapture(SemaRef, Capture, I->second);
3812 DeclRefExpr *Ref = nullptr;
3813 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3814 Captures[Capture] = Ref;
3815 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003816}
3817
Alexander Musmana5f070a2014-10-01 06:03:56 +00003818/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003819Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3820 Scope *S, const bool LimitedType,
3821 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003822 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003823 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003824 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003825 SemaRef.getLangOpts().CPlusPlus) {
3826 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003827 auto *UBExpr = TestIsLessOp ? UB : LB;
3828 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00003829 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3830 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003831 if (!Upper || !Lower)
3832 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003833
3834 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3835
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003836 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003837 // BuildBinOp already emitted error, this one is to point user to upper
3838 // and lower bound, and to tell what is passed to 'operator-'.
3839 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3840 << Upper->getSourceRange() << Lower->getSourceRange();
3841 return nullptr;
3842 }
3843 }
3844
3845 if (!Diff.isUsable())
3846 return nullptr;
3847
3848 // Upper - Lower [- 1]
3849 if (TestIsStrictOp)
3850 Diff = SemaRef.BuildBinOp(
3851 S, DefaultLoc, BO_Sub, Diff.get(),
3852 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3853 if (!Diff.isUsable())
3854 return nullptr;
3855
3856 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00003857 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3858 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003859 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003860 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003861 if (!Diff.isUsable())
3862 return nullptr;
3863
3864 // Parentheses (for dumping/debugging purposes only).
3865 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3866 if (!Diff.isUsable())
3867 return nullptr;
3868
3869 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003870 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003871 if (!Diff.isUsable())
3872 return nullptr;
3873
Alexander Musman174b3ca2014-10-06 11:16:29 +00003874 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003875 QualType Type = Diff.get()->getType();
3876 auto &C = SemaRef.Context;
3877 bool UseVarType = VarType->hasIntegerRepresentation() &&
3878 C.getTypeSize(Type) > C.getTypeSize(VarType);
3879 if (!Type->isIntegerType() || UseVarType) {
3880 unsigned NewSize =
3881 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3882 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3883 : Type->hasSignedIntegerRepresentation();
3884 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003885 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3886 Diff = SemaRef.PerformImplicitConversion(
3887 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3888 if (!Diff.isUsable())
3889 return nullptr;
3890 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003891 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003892 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003893 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3894 if (NewSize != C.getTypeSize(Type)) {
3895 if (NewSize < C.getTypeSize(Type)) {
3896 assert(NewSize == 64 && "incorrect loop var size");
3897 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3898 << InitSrcRange << ConditionSrcRange;
3899 }
3900 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003901 NewSize, Type->hasSignedIntegerRepresentation() ||
3902 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00003903 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3904 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3905 Sema::AA_Converting, true);
3906 if (!Diff.isUsable())
3907 return nullptr;
3908 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003909 }
3910 }
3911
Alexander Musmana5f070a2014-10-01 06:03:56 +00003912 return Diff.get();
3913}
3914
Alexey Bataev5a3af132016-03-29 08:58:54 +00003915Expr *OpenMPIterationSpaceChecker::BuildPreCond(
3916 Scope *S, Expr *Cond,
3917 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003918 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3919 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3920 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003921
Alexey Bataev5a3af132016-03-29 08:58:54 +00003922 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
3923 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
3924 if (!NewLB.isUsable() || !NewUB.isUsable())
3925 return nullptr;
3926
Alexey Bataev62dbb972015-04-22 11:59:37 +00003927 auto CondExpr = SemaRef.BuildBinOp(
3928 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3929 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003930 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003931 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003932 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
3933 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00003934 CondExpr = SemaRef.PerformImplicitConversion(
3935 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3936 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003937 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003938 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3939 // Otherwise use original loop conditon and evaluate it in runtime.
3940 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3941}
3942
Alexander Musmana5f070a2014-10-01 06:03:56 +00003943/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003944DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
3945 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
3946 auto *VD = dyn_cast<VarDecl>(LCDecl);
3947 if (!VD) {
3948 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
3949 auto *Ref = buildDeclRefExpr(
3950 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
3951 Captures.insert(std::make_pair(LCRef, Ref));
3952 return Ref;
3953 }
3954 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00003955 DefaultLoc);
3956}
3957
3958Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003959 if (LCDecl && !LCDecl->isInvalidDecl()) {
3960 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003961 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003962 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
3963 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003964 if (PrivateVar->isInvalidDecl())
3965 return nullptr;
3966 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3967 }
3968 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003969}
3970
3971/// \brief Build initization of the counter be used for codegen.
3972Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3973
3974/// \brief Build step of the counter be used for codegen.
3975Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3976
3977/// \brief Iteration space of a single for loop.
3978struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003979 /// \brief Condition of the loop.
3980 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003981 /// \brief This expression calculates the number of iterations in the loop.
3982 /// It is always possible to calculate it before starting the loop.
3983 Expr *NumIterations;
3984 /// \brief The loop counter variable.
3985 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003986 /// \brief Private loop counter variable.
3987 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003988 /// \brief This is initializer for the initial value of #CounterVar.
3989 Expr *CounterInit;
3990 /// \brief This is step for the #CounterVar used to generate its update:
3991 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3992 Expr *CounterStep;
3993 /// \brief Should step be subtracted?
3994 bool Subtract;
3995 /// \brief Source range of the loop init.
3996 SourceRange InitSrcRange;
3997 /// \brief Source range of the loop condition.
3998 SourceRange CondSrcRange;
3999 /// \brief Source range of the loop increment.
4000 SourceRange IncSrcRange;
4001};
4002
Alexey Bataev23b69422014-06-18 07:08:49 +00004003} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004004
Alexey Bataev9c821032015-04-30 04:23:23 +00004005void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4006 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4007 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004008 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4009 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004010 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4011 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004012 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4013 if (auto *D = ISC.GetLoopDecl()) {
4014 auto *VD = dyn_cast<VarDecl>(D);
4015 if (!VD) {
4016 if (auto *Private = IsOpenMPCapturedDecl(D))
4017 VD = Private;
4018 else {
4019 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4020 /*WithInit=*/false);
4021 VD = cast<VarDecl>(Ref->getDecl());
4022 }
4023 }
4024 DSAStack->addLoopControlVariable(D, VD);
4025 }
4026 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004027 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004028 }
4029}
4030
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004031/// \brief Called on a for stmt to check and extract its iteration space
4032/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004033static bool CheckOpenMPIterationSpace(
4034 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4035 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004036 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004037 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004038 LoopIterationSpace &ResultIterSpace,
4039 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004040 // OpenMP [2.6, Canonical Loop Form]
4041 // for (init-expr; test-expr; incr-expr) structured-block
4042 auto For = dyn_cast_or_null<ForStmt>(S);
4043 if (!For) {
4044 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004045 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4046 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4047 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4048 if (NestedLoopCount > 1) {
4049 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4050 SemaRef.Diag(DSA.getConstructLoc(),
4051 diag::note_omp_collapse_ordered_expr)
4052 << 2 << CollapseLoopCountExpr->getSourceRange()
4053 << OrderedLoopCountExpr->getSourceRange();
4054 else if (CollapseLoopCountExpr)
4055 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4056 diag::note_omp_collapse_ordered_expr)
4057 << 0 << CollapseLoopCountExpr->getSourceRange();
4058 else
4059 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4060 diag::note_omp_collapse_ordered_expr)
4061 << 1 << OrderedLoopCountExpr->getSourceRange();
4062 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004063 return true;
4064 }
4065 assert(For->getBody());
4066
4067 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4068
4069 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004070 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004071 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004072 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004073
4074 bool HasErrors = false;
4075
4076 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004077 if (auto *LCDecl = ISC.GetLoopDecl()) {
4078 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004079
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004080 // OpenMP [2.6, Canonical Loop Form]
4081 // Var is one of the following:
4082 // A variable of signed or unsigned integer type.
4083 // For C++, a variable of a random access iterator type.
4084 // For C, a variable of a pointer type.
4085 auto VarType = LCDecl->getType().getNonReferenceType();
4086 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4087 !VarType->isPointerType() &&
4088 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4089 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4090 << SemaRef.getLangOpts().CPlusPlus;
4091 HasErrors = true;
4092 }
4093
4094 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4095 // a Construct
4096 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4097 // parallel for construct is (are) private.
4098 // The loop iteration variable in the associated for-loop of a simd
4099 // construct with just one associated for-loop is linear with a
4100 // constant-linear-step that is the increment of the associated for-loop.
4101 // Exclude loop var from the list of variables with implicitly defined data
4102 // sharing attributes.
4103 VarsWithImplicitDSA.erase(LCDecl);
4104
4105 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4106 // in a Construct, C/C++].
4107 // The loop iteration variable in the associated for-loop of a simd
4108 // construct with just one associated for-loop may be listed in a linear
4109 // clause with a constant-linear-step that is the increment of the
4110 // associated for-loop.
4111 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4112 // parallel for construct may be listed in a private or lastprivate clause.
4113 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4114 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4115 // declared in the loop and it is predetermined as a private.
4116 auto PredeterminedCKind =
4117 isOpenMPSimdDirective(DKind)
4118 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4119 : OMPC_private;
4120 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4121 DVar.CKind != PredeterminedCKind) ||
4122 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4123 isOpenMPDistributeDirective(DKind)) &&
4124 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4125 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4126 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4127 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4128 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4129 << getOpenMPClauseName(PredeterminedCKind);
4130 if (DVar.RefExpr == nullptr)
4131 DVar.CKind = PredeterminedCKind;
4132 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4133 HasErrors = true;
4134 } else if (LoopDeclRefExpr != nullptr) {
4135 // Make the loop iteration variable private (for worksharing constructs),
4136 // linear (for simd directives with the only one associated loop) or
4137 // lastprivate (for simd directives with several collapsed or ordered
4138 // loops).
4139 if (DVar.CKind == OMPC_unknown)
4140 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, MatchesAlways(),
4141 /*FromParent=*/false);
4142 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4143 }
4144
4145 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4146
4147 // Check test-expr.
4148 HasErrors |= ISC.CheckCond(For->getCond());
4149
4150 // Check incr-expr.
4151 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004152 }
4153
Alexander Musmana5f070a2014-10-01 06:03:56 +00004154 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004155 return HasErrors;
4156
Alexander Musmana5f070a2014-10-01 06:03:56 +00004157 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004158 ResultIterSpace.PreCond =
4159 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004160 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004161 DSA.getCurScope(),
4162 (isOpenMPWorksharingDirective(DKind) ||
4163 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4164 Captures);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004165 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures);
Alexey Bataeva8899172015-08-06 12:30:57 +00004166 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004167 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4168 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4169 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4170 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4171 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4172 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4173
Alexey Bataev62dbb972015-04-22 11:59:37 +00004174 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4175 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004176 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004177 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004178 ResultIterSpace.CounterInit == nullptr ||
4179 ResultIterSpace.CounterStep == nullptr);
4180
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004181 return HasErrors;
4182}
4183
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004184/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004185static ExprResult
4186BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4187 ExprResult Start,
4188 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004189 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004190 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4191 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004192 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004193 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004194 VarRef.get()->getType())) {
4195 NewStart = SemaRef.PerformImplicitConversion(
4196 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4197 /*AllowExplicit=*/true);
4198 if (!NewStart.isUsable())
4199 return ExprError();
4200 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004201
4202 auto Init =
4203 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4204 return Init;
4205}
4206
Alexander Musmana5f070a2014-10-01 06:03:56 +00004207/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004208static ExprResult
4209BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4210 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4211 ExprResult Step, bool Subtract,
4212 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004213 // Add parentheses (for debugging purposes only).
4214 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4215 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4216 !Step.isUsable())
4217 return ExprError();
4218
Alexey Bataev5a3af132016-03-29 08:58:54 +00004219 ExprResult NewStep = Step;
4220 if (Captures)
4221 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004222 if (NewStep.isInvalid())
4223 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004224 ExprResult Update =
4225 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004226 if (!Update.isUsable())
4227 return ExprError();
4228
Alexey Bataevc0214e02016-02-16 12:13:49 +00004229 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4230 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004231 ExprResult NewStart = Start;
4232 if (Captures)
4233 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004234 if (NewStart.isInvalid())
4235 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004236
Alexey Bataevc0214e02016-02-16 12:13:49 +00004237 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4238 ExprResult SavedUpdate = Update;
4239 ExprResult UpdateVal;
4240 if (VarRef.get()->getType()->isOverloadableType() ||
4241 NewStart.get()->getType()->isOverloadableType() ||
4242 Update.get()->getType()->isOverloadableType()) {
4243 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4244 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4245 Update =
4246 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4247 if (Update.isUsable()) {
4248 UpdateVal =
4249 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4250 VarRef.get(), SavedUpdate.get());
4251 if (UpdateVal.isUsable()) {
4252 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4253 UpdateVal.get());
4254 }
4255 }
4256 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4257 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004258
Alexey Bataevc0214e02016-02-16 12:13:49 +00004259 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4260 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4261 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4262 NewStart.get(), SavedUpdate.get());
4263 if (!Update.isUsable())
4264 return ExprError();
4265
Alexey Bataev11481f52016-02-17 10:29:05 +00004266 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4267 VarRef.get()->getType())) {
4268 Update = SemaRef.PerformImplicitConversion(
4269 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4270 if (!Update.isUsable())
4271 return ExprError();
4272 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004273
4274 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4275 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004276 return Update;
4277}
4278
4279/// \brief Convert integer expression \a E to make it have at least \a Bits
4280/// bits.
4281static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4282 Sema &SemaRef) {
4283 if (E == nullptr)
4284 return ExprError();
4285 auto &C = SemaRef.Context;
4286 QualType OldType = E->getType();
4287 unsigned HasBits = C.getTypeSize(OldType);
4288 if (HasBits >= Bits)
4289 return ExprResult(E);
4290 // OK to convert to signed, because new type has more bits than old.
4291 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4292 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4293 true);
4294}
4295
4296/// \brief Check if the given expression \a E is a constant integer that fits
4297/// into \a Bits bits.
4298static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4299 if (E == nullptr)
4300 return false;
4301 llvm::APSInt Result;
4302 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4303 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4304 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004305}
4306
Alexey Bataev5a3af132016-03-29 08:58:54 +00004307/// Build preinits statement for the given declarations.
4308static Stmt *buildPreInits(ASTContext &Context,
4309 SmallVectorImpl<Decl *> &PreInits) {
4310 if (!PreInits.empty()) {
4311 return new (Context) DeclStmt(
4312 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4313 SourceLocation(), SourceLocation());
4314 }
4315 return nullptr;
4316}
4317
4318/// Build preinits statement for the given declarations.
4319static Stmt *buildPreInits(ASTContext &Context,
4320 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4321 if (!Captures.empty()) {
4322 SmallVector<Decl *, 16> PreInits;
4323 for (auto &Pair : Captures)
4324 PreInits.push_back(Pair.second->getDecl());
4325 return buildPreInits(Context, PreInits);
4326 }
4327 return nullptr;
4328}
4329
4330/// Build postupdate expression for the given list of postupdates expressions.
4331static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4332 Expr *PostUpdate = nullptr;
4333 if (!PostUpdates.empty()) {
4334 for (auto *E : PostUpdates) {
4335 Expr *ConvE = S.BuildCStyleCastExpr(
4336 E->getExprLoc(),
4337 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4338 E->getExprLoc(), E)
4339 .get();
4340 PostUpdate = PostUpdate
4341 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4342 PostUpdate, ConvE)
4343 .get()
4344 : ConvE;
4345 }
4346 }
4347 return PostUpdate;
4348}
4349
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004350/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004351/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4352/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004353static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004354CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4355 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4356 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004357 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004358 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004359 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004360 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004361 // Found 'collapse' clause - calculate collapse number.
4362 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004363 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004364 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004365 }
4366 if (OrderedLoopCountExpr) {
4367 // Found 'ordered' clause - calculate collapse number.
4368 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004369 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4370 if (Result.getLimitedValue() < NestedLoopCount) {
4371 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4372 diag::err_omp_wrong_ordered_loop_count)
4373 << OrderedLoopCountExpr->getSourceRange();
4374 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4375 diag::note_collapse_loop_count)
4376 << CollapseLoopCountExpr->getSourceRange();
4377 }
4378 NestedLoopCount = Result.getLimitedValue();
4379 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004380 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004381 // This is helper routine for loop directives (e.g., 'for', 'simd',
4382 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004383 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004384 SmallVector<LoopIterationSpace, 4> IterSpaces;
4385 IterSpaces.resize(NestedLoopCount);
4386 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004387 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004388 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004389 NestedLoopCount, CollapseLoopCountExpr,
4390 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004391 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004392 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004393 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004394 // OpenMP [2.8.1, simd construct, Restrictions]
4395 // All loops associated with the construct must be perfectly nested; that
4396 // is, there must be no intervening code nor any OpenMP directive between
4397 // any two loops.
4398 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004399 }
4400
Alexander Musmana5f070a2014-10-01 06:03:56 +00004401 Built.clear(/* size */ NestedLoopCount);
4402
4403 if (SemaRef.CurContext->isDependentContext())
4404 return NestedLoopCount;
4405
4406 // An example of what is generated for the following code:
4407 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004408 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004409 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004410 // for (k = 0; k < NK; ++k)
4411 // for (j = J0; j < NJ; j+=2) {
4412 // <loop body>
4413 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004414 //
4415 // We generate the code below.
4416 // Note: the loop body may be outlined in CodeGen.
4417 // Note: some counters may be C++ classes, operator- is used to find number of
4418 // iterations and operator+= to calculate counter value.
4419 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4420 // or i64 is currently supported).
4421 //
4422 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4423 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4424 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4425 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4426 // // similar updates for vars in clauses (e.g. 'linear')
4427 // <loop body (using local i and j)>
4428 // }
4429 // i = NI; // assign final values of counters
4430 // j = NJ;
4431 //
4432
4433 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4434 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004435 // Precondition tests if there is at least one iteration (all conditions are
4436 // true).
4437 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004438 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004439 ExprResult LastIteration32 = WidenIterationCount(
4440 32 /* Bits */, SemaRef.PerformImplicitConversion(
4441 N0->IgnoreImpCasts(), N0->getType(),
4442 Sema::AA_Converting, /*AllowExplicit=*/true)
4443 .get(),
4444 SemaRef);
4445 ExprResult LastIteration64 = WidenIterationCount(
4446 64 /* Bits */, SemaRef.PerformImplicitConversion(
4447 N0->IgnoreImpCasts(), N0->getType(),
4448 Sema::AA_Converting, /*AllowExplicit=*/true)
4449 .get(),
4450 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004451
4452 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4453 return NestedLoopCount;
4454
4455 auto &C = SemaRef.Context;
4456 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4457
4458 Scope *CurScope = DSA.getCurScope();
4459 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004460 if (PreCond.isUsable()) {
4461 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4462 PreCond.get(), IterSpaces[Cnt].PreCond);
4463 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004464 auto N = IterSpaces[Cnt].NumIterations;
4465 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4466 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004467 LastIteration32 = SemaRef.BuildBinOp(
4468 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4469 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4470 Sema::AA_Converting,
4471 /*AllowExplicit=*/true)
4472 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004473 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004474 LastIteration64 = SemaRef.BuildBinOp(
4475 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4476 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4477 Sema::AA_Converting,
4478 /*AllowExplicit=*/true)
4479 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004480 }
4481
4482 // Choose either the 32-bit or 64-bit version.
4483 ExprResult LastIteration = LastIteration64;
4484 if (LastIteration32.isUsable() &&
4485 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4486 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4487 FitsInto(
4488 32 /* Bits */,
4489 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4490 LastIteration64.get(), SemaRef)))
4491 LastIteration = LastIteration32;
4492
4493 if (!LastIteration.isUsable())
4494 return 0;
4495
4496 // Save the number of iterations.
4497 ExprResult NumIterations = LastIteration;
4498 {
4499 LastIteration = SemaRef.BuildBinOp(
4500 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4501 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4502 if (!LastIteration.isUsable())
4503 return 0;
4504 }
4505
4506 // Calculate the last iteration number beforehand instead of doing this on
4507 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4508 llvm::APSInt Result;
4509 bool IsConstant =
4510 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4511 ExprResult CalcLastIteration;
4512 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004513 ExprResult SaveRef =
4514 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004515 LastIteration = SaveRef;
4516
4517 // Prepare SaveRef + 1.
4518 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004519 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004520 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4521 if (!NumIterations.isUsable())
4522 return 0;
4523 }
4524
4525 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4526
Alexander Musmanc6388682014-12-15 07:07:06 +00004527 QualType VType = LastIteration.get()->getType();
4528 // Build variables passed into runtime, nesessary for worksharing directives.
4529 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004530 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4531 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004532 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004533 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4534 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004535 SemaRef.AddInitializerToDecl(
4536 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4537 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4538
4539 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004540 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4541 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004542 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4543 /*DirectInit*/ false,
4544 /*TypeMayContainAuto*/ false);
4545
4546 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4547 // This will be used to implement clause 'lastprivate'.
4548 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004549 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4550 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004551 SemaRef.AddInitializerToDecl(
4552 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4553 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4554
4555 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004556 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4557 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004558 SemaRef.AddInitializerToDecl(
4559 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4560 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4561
4562 // Build expression: UB = min(UB, LastIteration)
4563 // It is nesessary for CodeGen of directives with static scheduling.
4564 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4565 UB.get(), LastIteration.get());
4566 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4567 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4568 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4569 CondOp.get());
4570 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4571 }
4572
4573 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004574 ExprResult IV;
4575 ExprResult Init;
4576 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004577 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4578 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004579 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004580 isOpenMPTaskLoopDirective(DKind) ||
4581 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004582 ? LB.get()
4583 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4584 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4585 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004586 }
4587
Alexander Musmanc6388682014-12-15 07:07:06 +00004588 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004589 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004590 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004591 (isOpenMPWorksharingDirective(DKind) ||
4592 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004593 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4594 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4595 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004596
4597 // Loop increment (IV = IV + 1)
4598 SourceLocation IncLoc;
4599 ExprResult Inc =
4600 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4601 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4602 if (!Inc.isUsable())
4603 return 0;
4604 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004605 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4606 if (!Inc.isUsable())
4607 return 0;
4608
4609 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4610 // Used for directives with static scheduling.
4611 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004612 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4613 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004614 // LB + ST
4615 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4616 if (!NextLB.isUsable())
4617 return 0;
4618 // LB = LB + ST
4619 NextLB =
4620 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4621 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4622 if (!NextLB.isUsable())
4623 return 0;
4624 // UB + ST
4625 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4626 if (!NextUB.isUsable())
4627 return 0;
4628 // UB = UB + ST
4629 NextUB =
4630 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4631 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4632 if (!NextUB.isUsable())
4633 return 0;
4634 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004635
4636 // Build updates and final values of the loop counters.
4637 bool HasErrors = false;
4638 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004639 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004640 Built.Updates.resize(NestedLoopCount);
4641 Built.Finals.resize(NestedLoopCount);
4642 {
4643 ExprResult Div;
4644 // Go from inner nested loop to outer.
4645 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4646 LoopIterationSpace &IS = IterSpaces[Cnt];
4647 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4648 // Build: Iter = (IV / Div) % IS.NumIters
4649 // where Div is product of previous iterations' IS.NumIters.
4650 ExprResult Iter;
4651 if (Div.isUsable()) {
4652 Iter =
4653 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4654 } else {
4655 Iter = IV;
4656 assert((Cnt == (int)NestedLoopCount - 1) &&
4657 "unusable div expected on first iteration only");
4658 }
4659
4660 if (Cnt != 0 && Iter.isUsable())
4661 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4662 IS.NumIterations);
4663 if (!Iter.isUsable()) {
4664 HasErrors = true;
4665 break;
4666 }
4667
Alexey Bataev39f915b82015-05-08 10:41:21 +00004668 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4669 auto *CounterVar = buildDeclRefExpr(
4670 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4671 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4672 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004673 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004674 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004675 if (!Init.isUsable()) {
4676 HasErrors = true;
4677 break;
4678 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004679 ExprResult Update = BuildCounterUpdate(
4680 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4681 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004682 if (!Update.isUsable()) {
4683 HasErrors = true;
4684 break;
4685 }
4686
4687 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4688 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004689 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004690 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004691 if (!Final.isUsable()) {
4692 HasErrors = true;
4693 break;
4694 }
4695
4696 // Build Div for the next iteration: Div <- Div * IS.NumIters
4697 if (Cnt != 0) {
4698 if (Div.isUnset())
4699 Div = IS.NumIterations;
4700 else
4701 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4702 IS.NumIterations);
4703
4704 // Add parentheses (for debugging purposes only).
4705 if (Div.isUsable())
4706 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4707 if (!Div.isUsable()) {
4708 HasErrors = true;
4709 break;
4710 }
4711 }
4712 if (!Update.isUsable() || !Final.isUsable()) {
4713 HasErrors = true;
4714 break;
4715 }
4716 // Save results
4717 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004718 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004719 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004720 Built.Updates[Cnt] = Update.get();
4721 Built.Finals[Cnt] = Final.get();
4722 }
4723 }
4724
4725 if (HasErrors)
4726 return 0;
4727
4728 // Save results
4729 Built.IterationVarRef = IV.get();
4730 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004731 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004732 Built.CalcLastIteration =
4733 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004734 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004735 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004736 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004737 Built.Init = Init.get();
4738 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004739 Built.LB = LB.get();
4740 Built.UB = UB.get();
4741 Built.IL = IL.get();
4742 Built.ST = ST.get();
4743 Built.EUB = EUB.get();
4744 Built.NLB = NextLB.get();
4745 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004746
Alexey Bataevabfc0692014-06-25 06:52:00 +00004747 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004748}
4749
Alexey Bataev10e775f2015-07-30 11:36:16 +00004750static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004751 auto CollapseClauses =
4752 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4753 if (CollapseClauses.begin() != CollapseClauses.end())
4754 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004755 return nullptr;
4756}
4757
Alexey Bataev10e775f2015-07-30 11:36:16 +00004758static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004759 auto OrderedClauses =
4760 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4761 if (OrderedClauses.begin() != OrderedClauses.end())
4762 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004763 return nullptr;
4764}
4765
Alexey Bataev66b15b52015-08-21 11:14:16 +00004766static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4767 const Expr *Safelen) {
4768 llvm::APSInt SimdlenRes, SafelenRes;
4769 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4770 Simdlen->isInstantiationDependent() ||
4771 Simdlen->containsUnexpandedParameterPack())
4772 return false;
4773 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4774 Safelen->isInstantiationDependent() ||
4775 Safelen->containsUnexpandedParameterPack())
4776 return false;
4777 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4778 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4779 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4780 // If both simdlen and safelen clauses are specified, the value of the simdlen
4781 // parameter must be less than or equal to the value of the safelen parameter.
4782 if (SimdlenRes > SafelenRes) {
4783 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4784 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4785 return true;
4786 }
4787 return false;
4788}
4789
Alexey Bataev4acb8592014-07-07 13:01:15 +00004790StmtResult Sema::ActOnOpenMPSimdDirective(
4791 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4792 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004793 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004794 if (!AStmt)
4795 return StmtError();
4796
4797 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004798 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004799 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4800 // define the nested loops number.
4801 unsigned NestedLoopCount = CheckOpenMPLoop(
4802 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4803 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004804 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004805 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004806
Alexander Musmana5f070a2014-10-01 06:03:56 +00004807 assert((CurContext->isDependentContext() || B.builtAll()) &&
4808 "omp simd loop exprs were not built");
4809
Alexander Musman3276a272015-03-21 10:12:56 +00004810 if (!CurContext->isDependentContext()) {
4811 // Finalize the clauses that need pre-built expressions for CodeGen.
4812 for (auto C : Clauses) {
4813 if (auto LC = dyn_cast<OMPLinearClause>(C))
4814 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4815 B.NumIterations, *this, CurScope))
4816 return StmtError();
4817 }
4818 }
4819
Alexey Bataev66b15b52015-08-21 11:14:16 +00004820 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4821 // If both simdlen and safelen clauses are specified, the value of the simdlen
4822 // parameter must be less than or equal to the value of the safelen parameter.
4823 OMPSafelenClause *Safelen = nullptr;
4824 OMPSimdlenClause *Simdlen = nullptr;
4825 for (auto *Clause : Clauses) {
4826 if (Clause->getClauseKind() == OMPC_safelen)
4827 Safelen = cast<OMPSafelenClause>(Clause);
4828 else if (Clause->getClauseKind() == OMPC_simdlen)
4829 Simdlen = cast<OMPSimdlenClause>(Clause);
4830 if (Safelen && Simdlen)
4831 break;
4832 }
4833 if (Simdlen && Safelen &&
4834 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4835 Safelen->getSafelen()))
4836 return StmtError();
4837
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004838 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004839 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4840 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004841}
4842
Alexey Bataev4acb8592014-07-07 13:01:15 +00004843StmtResult Sema::ActOnOpenMPForDirective(
4844 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4845 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004846 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004847 if (!AStmt)
4848 return StmtError();
4849
4850 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004851 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004852 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4853 // define the nested loops number.
4854 unsigned NestedLoopCount = CheckOpenMPLoop(
4855 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4856 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004857 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004858 return StmtError();
4859
Alexander Musmana5f070a2014-10-01 06:03:56 +00004860 assert((CurContext->isDependentContext() || B.builtAll()) &&
4861 "omp for loop exprs were not built");
4862
Alexey Bataev54acd402015-08-04 11:18:19 +00004863 if (!CurContext->isDependentContext()) {
4864 // Finalize the clauses that need pre-built expressions for CodeGen.
4865 for (auto C : Clauses) {
4866 if (auto LC = dyn_cast<OMPLinearClause>(C))
4867 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4868 B.NumIterations, *this, CurScope))
4869 return StmtError();
4870 }
4871 }
4872
Alexey Bataevf29276e2014-06-18 04:14:57 +00004873 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004874 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004875 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004876}
4877
Alexander Musmanf82886e2014-09-18 05:12:34 +00004878StmtResult Sema::ActOnOpenMPForSimdDirective(
4879 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4880 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004881 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004882 if (!AStmt)
4883 return StmtError();
4884
4885 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004886 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004887 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4888 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004889 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004890 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4891 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4892 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004893 if (NestedLoopCount == 0)
4894 return StmtError();
4895
Alexander Musmanc6388682014-12-15 07:07:06 +00004896 assert((CurContext->isDependentContext() || B.builtAll()) &&
4897 "omp for simd loop exprs were not built");
4898
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004899 if (!CurContext->isDependentContext()) {
4900 // Finalize the clauses that need pre-built expressions for CodeGen.
4901 for (auto C : Clauses) {
4902 if (auto LC = dyn_cast<OMPLinearClause>(C))
4903 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4904 B.NumIterations, *this, CurScope))
4905 return StmtError();
4906 }
4907 }
4908
Alexey Bataev66b15b52015-08-21 11:14:16 +00004909 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4910 // If both simdlen and safelen clauses are specified, the value of the simdlen
4911 // parameter must be less than or equal to the value of the safelen parameter.
4912 OMPSafelenClause *Safelen = nullptr;
4913 OMPSimdlenClause *Simdlen = nullptr;
4914 for (auto *Clause : Clauses) {
4915 if (Clause->getClauseKind() == OMPC_safelen)
4916 Safelen = cast<OMPSafelenClause>(Clause);
4917 else if (Clause->getClauseKind() == OMPC_simdlen)
4918 Simdlen = cast<OMPSimdlenClause>(Clause);
4919 if (Safelen && Simdlen)
4920 break;
4921 }
4922 if (Simdlen && Safelen &&
4923 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4924 Safelen->getSafelen()))
4925 return StmtError();
4926
Alexander Musmanf82886e2014-09-18 05:12:34 +00004927 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004928 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4929 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004930}
4931
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004932StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4933 Stmt *AStmt,
4934 SourceLocation StartLoc,
4935 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004936 if (!AStmt)
4937 return StmtError();
4938
4939 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004940 auto BaseStmt = AStmt;
4941 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4942 BaseStmt = CS->getCapturedStmt();
4943 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4944 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004945 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004946 return StmtError();
4947 // All associated statements must be '#pragma omp section' except for
4948 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004949 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004950 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4951 if (SectionStmt)
4952 Diag(SectionStmt->getLocStart(),
4953 diag::err_omp_sections_substmt_not_section);
4954 return StmtError();
4955 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004956 cast<OMPSectionDirective>(SectionStmt)
4957 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004958 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004959 } else {
4960 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4961 return StmtError();
4962 }
4963
4964 getCurFunction()->setHasBranchProtectedScope();
4965
Alexey Bataev25e5b442015-09-15 12:52:43 +00004966 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4967 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004968}
4969
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004970StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4971 SourceLocation StartLoc,
4972 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004973 if (!AStmt)
4974 return StmtError();
4975
4976 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004977
4978 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004979 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004980
Alexey Bataev25e5b442015-09-15 12:52:43 +00004981 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4982 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004983}
4984
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004985StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4986 Stmt *AStmt,
4987 SourceLocation StartLoc,
4988 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004989 if (!AStmt)
4990 return StmtError();
4991
4992 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004993
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004994 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004995
Alexey Bataev3255bf32015-01-19 05:20:46 +00004996 // OpenMP [2.7.3, single Construct, Restrictions]
4997 // The copyprivate clause must not be used with the nowait clause.
4998 OMPClause *Nowait = nullptr;
4999 OMPClause *Copyprivate = nullptr;
5000 for (auto *Clause : Clauses) {
5001 if (Clause->getClauseKind() == OMPC_nowait)
5002 Nowait = Clause;
5003 else if (Clause->getClauseKind() == OMPC_copyprivate)
5004 Copyprivate = Clause;
5005 if (Copyprivate && Nowait) {
5006 Diag(Copyprivate->getLocStart(),
5007 diag::err_omp_single_copyprivate_with_nowait);
5008 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5009 return StmtError();
5010 }
5011 }
5012
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005013 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5014}
5015
Alexander Musman80c22892014-07-17 08:54:58 +00005016StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5017 SourceLocation StartLoc,
5018 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005019 if (!AStmt)
5020 return StmtError();
5021
5022 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005023
5024 getCurFunction()->setHasBranchProtectedScope();
5025
5026 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5027}
5028
Alexey Bataev28c75412015-12-15 08:19:24 +00005029StmtResult Sema::ActOnOpenMPCriticalDirective(
5030 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5031 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005032 if (!AStmt)
5033 return StmtError();
5034
5035 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005036
Alexey Bataev28c75412015-12-15 08:19:24 +00005037 bool ErrorFound = false;
5038 llvm::APSInt Hint;
5039 SourceLocation HintLoc;
5040 bool DependentHint = false;
5041 for (auto *C : Clauses) {
5042 if (C->getClauseKind() == OMPC_hint) {
5043 if (!DirName.getName()) {
5044 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5045 ErrorFound = true;
5046 }
5047 Expr *E = cast<OMPHintClause>(C)->getHint();
5048 if (E->isTypeDependent() || E->isValueDependent() ||
5049 E->isInstantiationDependent())
5050 DependentHint = true;
5051 else {
5052 Hint = E->EvaluateKnownConstInt(Context);
5053 HintLoc = C->getLocStart();
5054 }
5055 }
5056 }
5057 if (ErrorFound)
5058 return StmtError();
5059 auto Pair = DSAStack->getCriticalWithHint(DirName);
5060 if (Pair.first && DirName.getName() && !DependentHint) {
5061 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5062 Diag(StartLoc, diag::err_omp_critical_with_hint);
5063 if (HintLoc.isValid()) {
5064 Diag(HintLoc, diag::note_omp_critical_hint_here)
5065 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5066 } else
5067 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5068 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5069 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5070 << 1
5071 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5072 /*Radix=*/10, /*Signed=*/false);
5073 } else
5074 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5075 }
5076 }
5077
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005078 getCurFunction()->setHasBranchProtectedScope();
5079
Alexey Bataev28c75412015-12-15 08:19:24 +00005080 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5081 Clauses, AStmt);
5082 if (!Pair.first && DirName.getName() && !DependentHint)
5083 DSAStack->addCriticalWithHint(Dir, Hint);
5084 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005085}
5086
Alexey Bataev4acb8592014-07-07 13:01:15 +00005087StmtResult Sema::ActOnOpenMPParallelForDirective(
5088 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5089 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005090 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005091 if (!AStmt)
5092 return StmtError();
5093
Alexey Bataev4acb8592014-07-07 13:01:15 +00005094 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5095 // 1.2.2 OpenMP Language Terminology
5096 // Structured block - An executable statement with a single entry at the
5097 // top and a single exit at the bottom.
5098 // The point of exit cannot be a branch out of the structured block.
5099 // longjmp() and throw() must not violate the entry/exit criteria.
5100 CS->getCapturedDecl()->setNothrow();
5101
Alexander Musmanc6388682014-12-15 07:07:06 +00005102 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005103 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5104 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005105 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005106 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5107 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5108 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005109 if (NestedLoopCount == 0)
5110 return StmtError();
5111
Alexander Musmana5f070a2014-10-01 06:03:56 +00005112 assert((CurContext->isDependentContext() || B.builtAll()) &&
5113 "omp parallel for loop exprs were not built");
5114
Alexey Bataev54acd402015-08-04 11:18:19 +00005115 if (!CurContext->isDependentContext()) {
5116 // Finalize the clauses that need pre-built expressions for CodeGen.
5117 for (auto C : Clauses) {
5118 if (auto LC = dyn_cast<OMPLinearClause>(C))
5119 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5120 B.NumIterations, *this, CurScope))
5121 return StmtError();
5122 }
5123 }
5124
Alexey Bataev4acb8592014-07-07 13:01:15 +00005125 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005126 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005127 NestedLoopCount, Clauses, AStmt, B,
5128 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005129}
5130
Alexander Musmane4e893b2014-09-23 09:33:00 +00005131StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5132 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5133 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005134 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005135 if (!AStmt)
5136 return StmtError();
5137
Alexander Musmane4e893b2014-09-23 09:33:00 +00005138 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5139 // 1.2.2 OpenMP Language Terminology
5140 // Structured block - An executable statement with a single entry at the
5141 // top and a single exit at the bottom.
5142 // The point of exit cannot be a branch out of the structured block.
5143 // longjmp() and throw() must not violate the entry/exit criteria.
5144 CS->getCapturedDecl()->setNothrow();
5145
Alexander Musmanc6388682014-12-15 07:07:06 +00005146 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005147 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5148 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005149 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005150 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5151 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5152 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005153 if (NestedLoopCount == 0)
5154 return StmtError();
5155
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005156 if (!CurContext->isDependentContext()) {
5157 // Finalize the clauses that need pre-built expressions for CodeGen.
5158 for (auto C : Clauses) {
5159 if (auto LC = dyn_cast<OMPLinearClause>(C))
5160 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5161 B.NumIterations, *this, CurScope))
5162 return StmtError();
5163 }
5164 }
5165
Alexey Bataev66b15b52015-08-21 11:14:16 +00005166 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5167 // If both simdlen and safelen clauses are specified, the value of the simdlen
5168 // parameter must be less than or equal to the value of the safelen parameter.
5169 OMPSafelenClause *Safelen = nullptr;
5170 OMPSimdlenClause *Simdlen = nullptr;
5171 for (auto *Clause : Clauses) {
5172 if (Clause->getClauseKind() == OMPC_safelen)
5173 Safelen = cast<OMPSafelenClause>(Clause);
5174 else if (Clause->getClauseKind() == OMPC_simdlen)
5175 Simdlen = cast<OMPSimdlenClause>(Clause);
5176 if (Safelen && Simdlen)
5177 break;
5178 }
5179 if (Simdlen && Safelen &&
5180 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5181 Safelen->getSafelen()))
5182 return StmtError();
5183
Alexander Musmane4e893b2014-09-23 09:33:00 +00005184 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005185 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005186 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005187}
5188
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005189StmtResult
5190Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5191 Stmt *AStmt, SourceLocation StartLoc,
5192 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005193 if (!AStmt)
5194 return StmtError();
5195
5196 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005197 auto BaseStmt = AStmt;
5198 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5199 BaseStmt = CS->getCapturedStmt();
5200 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5201 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005202 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005203 return StmtError();
5204 // All associated statements must be '#pragma omp section' except for
5205 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005206 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005207 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5208 if (SectionStmt)
5209 Diag(SectionStmt->getLocStart(),
5210 diag::err_omp_parallel_sections_substmt_not_section);
5211 return StmtError();
5212 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005213 cast<OMPSectionDirective>(SectionStmt)
5214 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005215 }
5216 } else {
5217 Diag(AStmt->getLocStart(),
5218 diag::err_omp_parallel_sections_not_compound_stmt);
5219 return StmtError();
5220 }
5221
5222 getCurFunction()->setHasBranchProtectedScope();
5223
Alexey Bataev25e5b442015-09-15 12:52:43 +00005224 return OMPParallelSectionsDirective::Create(
5225 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005226}
5227
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005228StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5229 Stmt *AStmt, SourceLocation StartLoc,
5230 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005231 if (!AStmt)
5232 return StmtError();
5233
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005234 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5235 // 1.2.2 OpenMP Language Terminology
5236 // Structured block - An executable statement with a single entry at the
5237 // top and a single exit at the bottom.
5238 // The point of exit cannot be a branch out of the structured block.
5239 // longjmp() and throw() must not violate the entry/exit criteria.
5240 CS->getCapturedDecl()->setNothrow();
5241
5242 getCurFunction()->setHasBranchProtectedScope();
5243
Alexey Bataev25e5b442015-09-15 12:52:43 +00005244 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5245 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005246}
5247
Alexey Bataev68446b72014-07-18 07:47:19 +00005248StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5249 SourceLocation EndLoc) {
5250 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5251}
5252
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005253StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5254 SourceLocation EndLoc) {
5255 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5256}
5257
Alexey Bataev2df347a2014-07-18 10:17:07 +00005258StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5259 SourceLocation EndLoc) {
5260 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5261}
5262
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005263StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5264 SourceLocation StartLoc,
5265 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005266 if (!AStmt)
5267 return StmtError();
5268
5269 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005270
5271 getCurFunction()->setHasBranchProtectedScope();
5272
5273 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5274}
5275
Alexey Bataev6125da92014-07-21 11:26:11 +00005276StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5277 SourceLocation StartLoc,
5278 SourceLocation EndLoc) {
5279 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5280 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5281}
5282
Alexey Bataev346265e2015-09-25 10:37:12 +00005283StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5284 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005285 SourceLocation StartLoc,
5286 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005287 OMPClause *DependFound = nullptr;
5288 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005289 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005290 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005291 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005292 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005293 for (auto *C : Clauses) {
5294 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5295 DependFound = C;
5296 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5297 if (DependSourceClause) {
5298 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5299 << getOpenMPDirectiveName(OMPD_ordered)
5300 << getOpenMPClauseName(OMPC_depend) << 2;
5301 ErrorFound = true;
5302 } else
5303 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005304 if (DependSinkClause) {
5305 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5306 << 0;
5307 ErrorFound = true;
5308 }
5309 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5310 if (DependSourceClause) {
5311 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5312 << 1;
5313 ErrorFound = true;
5314 }
5315 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005316 }
5317 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005318 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005319 else if (C->getClauseKind() == OMPC_simd)
5320 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005321 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005322 if (!ErrorFound && !SC &&
5323 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005324 // OpenMP [2.8.1,simd Construct, Restrictions]
5325 // An ordered construct with the simd clause is the only OpenMP construct
5326 // that can appear in the simd region.
5327 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005328 ErrorFound = true;
5329 } else if (DependFound && (TC || SC)) {
5330 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5331 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5332 ErrorFound = true;
5333 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5334 Diag(DependFound->getLocStart(),
5335 diag::err_omp_ordered_directive_without_param);
5336 ErrorFound = true;
5337 } else if (TC || Clauses.empty()) {
5338 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5339 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5340 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5341 << (TC != nullptr);
5342 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5343 ErrorFound = true;
5344 }
5345 }
5346 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005347 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005348
5349 if (AStmt) {
5350 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5351
5352 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005353 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005354
5355 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005356}
5357
Alexey Bataev1d160b12015-03-13 12:27:31 +00005358namespace {
5359/// \brief Helper class for checking expression in 'omp atomic [update]'
5360/// construct.
5361class OpenMPAtomicUpdateChecker {
5362 /// \brief Error results for atomic update expressions.
5363 enum ExprAnalysisErrorCode {
5364 /// \brief A statement is not an expression statement.
5365 NotAnExpression,
5366 /// \brief Expression is not builtin binary or unary operation.
5367 NotABinaryOrUnaryExpression,
5368 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5369 NotAnUnaryIncDecExpression,
5370 /// \brief An expression is not of scalar type.
5371 NotAScalarType,
5372 /// \brief A binary operation is not an assignment operation.
5373 NotAnAssignmentOp,
5374 /// \brief RHS part of the binary operation is not a binary expression.
5375 NotABinaryExpression,
5376 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5377 /// expression.
5378 NotABinaryOperator,
5379 /// \brief RHS binary operation does not have reference to the updated LHS
5380 /// part.
5381 NotAnUpdateExpression,
5382 /// \brief No errors is found.
5383 NoError
5384 };
5385 /// \brief Reference to Sema.
5386 Sema &SemaRef;
5387 /// \brief A location for note diagnostics (when error is found).
5388 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005389 /// \brief 'x' lvalue part of the source atomic expression.
5390 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005391 /// \brief 'expr' rvalue part of the source atomic expression.
5392 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005393 /// \brief Helper expression of the form
5394 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5395 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5396 Expr *UpdateExpr;
5397 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5398 /// important for non-associative operations.
5399 bool IsXLHSInRHSPart;
5400 BinaryOperatorKind Op;
5401 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005402 /// \brief true if the source expression is a postfix unary operation, false
5403 /// if it is a prefix unary operation.
5404 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005405
5406public:
5407 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005408 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005409 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005410 /// \brief Check specified statement that it is suitable for 'atomic update'
5411 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005412 /// expression. If DiagId and NoteId == 0, then only check is performed
5413 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005414 /// \param DiagId Diagnostic which should be emitted if error is found.
5415 /// \param NoteId Diagnostic note for the main error message.
5416 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005417 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005418 /// \brief Return the 'x' lvalue part of the source atomic expression.
5419 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005420 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5421 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005422 /// \brief Return the update expression used in calculation of the updated
5423 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5424 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5425 Expr *getUpdateExpr() const { return UpdateExpr; }
5426 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5427 /// false otherwise.
5428 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5429
Alexey Bataevb78ca832015-04-01 03:33:17 +00005430 /// \brief true if the source expression is a postfix unary operation, false
5431 /// if it is a prefix unary operation.
5432 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5433
Alexey Bataev1d160b12015-03-13 12:27:31 +00005434private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005435 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5436 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005437};
5438} // namespace
5439
5440bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5441 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5442 ExprAnalysisErrorCode ErrorFound = NoError;
5443 SourceLocation ErrorLoc, NoteLoc;
5444 SourceRange ErrorRange, NoteRange;
5445 // Allowed constructs are:
5446 // x = x binop expr;
5447 // x = expr binop x;
5448 if (AtomicBinOp->getOpcode() == BO_Assign) {
5449 X = AtomicBinOp->getLHS();
5450 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5451 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5452 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5453 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5454 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005455 Op = AtomicInnerBinOp->getOpcode();
5456 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005457 auto *LHS = AtomicInnerBinOp->getLHS();
5458 auto *RHS = AtomicInnerBinOp->getRHS();
5459 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5460 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5461 /*Canonical=*/true);
5462 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5463 /*Canonical=*/true);
5464 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5465 /*Canonical=*/true);
5466 if (XId == LHSId) {
5467 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005468 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005469 } else if (XId == RHSId) {
5470 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005471 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005472 } else {
5473 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5474 ErrorRange = AtomicInnerBinOp->getSourceRange();
5475 NoteLoc = X->getExprLoc();
5476 NoteRange = X->getSourceRange();
5477 ErrorFound = NotAnUpdateExpression;
5478 }
5479 } else {
5480 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5481 ErrorRange = AtomicInnerBinOp->getSourceRange();
5482 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5483 NoteRange = SourceRange(NoteLoc, NoteLoc);
5484 ErrorFound = NotABinaryOperator;
5485 }
5486 } else {
5487 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5488 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5489 ErrorFound = NotABinaryExpression;
5490 }
5491 } else {
5492 ErrorLoc = AtomicBinOp->getExprLoc();
5493 ErrorRange = AtomicBinOp->getSourceRange();
5494 NoteLoc = AtomicBinOp->getOperatorLoc();
5495 NoteRange = SourceRange(NoteLoc, NoteLoc);
5496 ErrorFound = NotAnAssignmentOp;
5497 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005498 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005499 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5500 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5501 return true;
5502 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005503 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005504 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005505}
5506
5507bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5508 unsigned NoteId) {
5509 ExprAnalysisErrorCode ErrorFound = NoError;
5510 SourceLocation ErrorLoc, NoteLoc;
5511 SourceRange ErrorRange, NoteRange;
5512 // Allowed constructs are:
5513 // x++;
5514 // x--;
5515 // ++x;
5516 // --x;
5517 // x binop= expr;
5518 // x = x binop expr;
5519 // x = expr binop x;
5520 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5521 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5522 if (AtomicBody->getType()->isScalarType() ||
5523 AtomicBody->isInstantiationDependent()) {
5524 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5525 AtomicBody->IgnoreParenImpCasts())) {
5526 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005527 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005528 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005529 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005530 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005531 X = AtomicCompAssignOp->getLHS();
5532 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005533 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5534 AtomicBody->IgnoreParenImpCasts())) {
5535 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005536 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5537 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005538 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005539 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5540 // Check for Unary Operation
5541 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005542 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005543 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5544 OpLoc = AtomicUnaryOp->getOperatorLoc();
5545 X = AtomicUnaryOp->getSubExpr();
5546 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5547 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005548 } else {
5549 ErrorFound = NotAnUnaryIncDecExpression;
5550 ErrorLoc = AtomicUnaryOp->getExprLoc();
5551 ErrorRange = AtomicUnaryOp->getSourceRange();
5552 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5553 NoteRange = SourceRange(NoteLoc, NoteLoc);
5554 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005555 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005556 ErrorFound = NotABinaryOrUnaryExpression;
5557 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5558 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5559 }
5560 } else {
5561 ErrorFound = NotAScalarType;
5562 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5563 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5564 }
5565 } else {
5566 ErrorFound = NotAnExpression;
5567 NoteLoc = ErrorLoc = S->getLocStart();
5568 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5569 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005570 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005571 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5572 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5573 return true;
5574 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005575 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005576 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005577 // Build an update expression of form 'OpaqueValueExpr(x) binop
5578 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5579 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5580 auto *OVEX = new (SemaRef.getASTContext())
5581 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5582 auto *OVEExpr = new (SemaRef.getASTContext())
5583 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5584 auto Update =
5585 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5586 IsXLHSInRHSPart ? OVEExpr : OVEX);
5587 if (Update.isInvalid())
5588 return true;
5589 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5590 Sema::AA_Casting);
5591 if (Update.isInvalid())
5592 return true;
5593 UpdateExpr = Update.get();
5594 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005595 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005596}
5597
Alexey Bataev0162e452014-07-22 10:10:35 +00005598StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5599 Stmt *AStmt,
5600 SourceLocation StartLoc,
5601 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005602 if (!AStmt)
5603 return StmtError();
5604
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005605 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005606 // 1.2.2 OpenMP Language Terminology
5607 // Structured block - An executable statement with a single entry at the
5608 // top and a single exit at the bottom.
5609 // The point of exit cannot be a branch out of the structured block.
5610 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005611 OpenMPClauseKind AtomicKind = OMPC_unknown;
5612 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005613 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005614 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005615 C->getClauseKind() == OMPC_update ||
5616 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005617 if (AtomicKind != OMPC_unknown) {
5618 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5619 << SourceRange(C->getLocStart(), C->getLocEnd());
5620 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5621 << getOpenMPClauseName(AtomicKind);
5622 } else {
5623 AtomicKind = C->getClauseKind();
5624 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005625 }
5626 }
5627 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005628
Alexey Bataev459dec02014-07-24 06:46:57 +00005629 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005630 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5631 Body = EWC->getSubExpr();
5632
Alexey Bataev62cec442014-11-18 10:14:22 +00005633 Expr *X = nullptr;
5634 Expr *V = nullptr;
5635 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005636 Expr *UE = nullptr;
5637 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005638 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005639 // OpenMP [2.12.6, atomic Construct]
5640 // In the next expressions:
5641 // * x and v (as applicable) are both l-value expressions with scalar type.
5642 // * During the execution of an atomic region, multiple syntactic
5643 // occurrences of x must designate the same storage location.
5644 // * Neither of v and expr (as applicable) may access the storage location
5645 // designated by x.
5646 // * Neither of x and expr (as applicable) may access the storage location
5647 // designated by v.
5648 // * expr is an expression with scalar type.
5649 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5650 // * binop, binop=, ++, and -- are not overloaded operators.
5651 // * The expression x binop expr must be numerically equivalent to x binop
5652 // (expr). This requirement is satisfied if the operators in expr have
5653 // precedence greater than binop, or by using parentheses around expr or
5654 // subexpressions of expr.
5655 // * The expression expr binop x must be numerically equivalent to (expr)
5656 // binop x. This requirement is satisfied if the operators in expr have
5657 // precedence equal to or greater than binop, or by using parentheses around
5658 // expr or subexpressions of expr.
5659 // * For forms that allow multiple occurrences of x, the number of times
5660 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005661 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005662 enum {
5663 NotAnExpression,
5664 NotAnAssignmentOp,
5665 NotAScalarType,
5666 NotAnLValue,
5667 NoError
5668 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005669 SourceLocation ErrorLoc, NoteLoc;
5670 SourceRange ErrorRange, NoteRange;
5671 // If clause is read:
5672 // v = x;
5673 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5674 auto AtomicBinOp =
5675 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5676 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5677 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5678 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5679 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5680 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5681 if (!X->isLValue() || !V->isLValue()) {
5682 auto NotLValueExpr = X->isLValue() ? V : X;
5683 ErrorFound = NotAnLValue;
5684 ErrorLoc = AtomicBinOp->getExprLoc();
5685 ErrorRange = AtomicBinOp->getSourceRange();
5686 NoteLoc = NotLValueExpr->getExprLoc();
5687 NoteRange = NotLValueExpr->getSourceRange();
5688 }
5689 } else if (!X->isInstantiationDependent() ||
5690 !V->isInstantiationDependent()) {
5691 auto NotScalarExpr =
5692 (X->isInstantiationDependent() || X->getType()->isScalarType())
5693 ? V
5694 : X;
5695 ErrorFound = NotAScalarType;
5696 ErrorLoc = AtomicBinOp->getExprLoc();
5697 ErrorRange = AtomicBinOp->getSourceRange();
5698 NoteLoc = NotScalarExpr->getExprLoc();
5699 NoteRange = NotScalarExpr->getSourceRange();
5700 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005701 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005702 ErrorFound = NotAnAssignmentOp;
5703 ErrorLoc = AtomicBody->getExprLoc();
5704 ErrorRange = AtomicBody->getSourceRange();
5705 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5706 : AtomicBody->getExprLoc();
5707 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5708 : AtomicBody->getSourceRange();
5709 }
5710 } else {
5711 ErrorFound = NotAnExpression;
5712 NoteLoc = ErrorLoc = Body->getLocStart();
5713 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005714 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005715 if (ErrorFound != NoError) {
5716 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5717 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005718 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5719 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005720 return StmtError();
5721 } else if (CurContext->isDependentContext())
5722 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005723 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005724 enum {
5725 NotAnExpression,
5726 NotAnAssignmentOp,
5727 NotAScalarType,
5728 NotAnLValue,
5729 NoError
5730 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005731 SourceLocation ErrorLoc, NoteLoc;
5732 SourceRange ErrorRange, NoteRange;
5733 // If clause is write:
5734 // x = expr;
5735 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5736 auto AtomicBinOp =
5737 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5738 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005739 X = AtomicBinOp->getLHS();
5740 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005741 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5742 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5743 if (!X->isLValue()) {
5744 ErrorFound = NotAnLValue;
5745 ErrorLoc = AtomicBinOp->getExprLoc();
5746 ErrorRange = AtomicBinOp->getSourceRange();
5747 NoteLoc = X->getExprLoc();
5748 NoteRange = X->getSourceRange();
5749 }
5750 } else if (!X->isInstantiationDependent() ||
5751 !E->isInstantiationDependent()) {
5752 auto NotScalarExpr =
5753 (X->isInstantiationDependent() || X->getType()->isScalarType())
5754 ? E
5755 : X;
5756 ErrorFound = NotAScalarType;
5757 ErrorLoc = AtomicBinOp->getExprLoc();
5758 ErrorRange = AtomicBinOp->getSourceRange();
5759 NoteLoc = NotScalarExpr->getExprLoc();
5760 NoteRange = NotScalarExpr->getSourceRange();
5761 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005762 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005763 ErrorFound = NotAnAssignmentOp;
5764 ErrorLoc = AtomicBody->getExprLoc();
5765 ErrorRange = AtomicBody->getSourceRange();
5766 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5767 : AtomicBody->getExprLoc();
5768 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5769 : AtomicBody->getSourceRange();
5770 }
5771 } else {
5772 ErrorFound = NotAnExpression;
5773 NoteLoc = ErrorLoc = Body->getLocStart();
5774 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005775 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005776 if (ErrorFound != NoError) {
5777 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5778 << ErrorRange;
5779 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5780 << NoteRange;
5781 return StmtError();
5782 } else if (CurContext->isDependentContext())
5783 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005784 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005785 // If clause is update:
5786 // x++;
5787 // x--;
5788 // ++x;
5789 // --x;
5790 // x binop= expr;
5791 // x = x binop expr;
5792 // x = expr binop x;
5793 OpenMPAtomicUpdateChecker Checker(*this);
5794 if (Checker.checkStatement(
5795 Body, (AtomicKind == OMPC_update)
5796 ? diag::err_omp_atomic_update_not_expression_statement
5797 : diag::err_omp_atomic_not_expression_statement,
5798 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005799 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005800 if (!CurContext->isDependentContext()) {
5801 E = Checker.getExpr();
5802 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005803 UE = Checker.getUpdateExpr();
5804 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005805 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005806 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005807 enum {
5808 NotAnAssignmentOp,
5809 NotACompoundStatement,
5810 NotTwoSubstatements,
5811 NotASpecificExpression,
5812 NoError
5813 } ErrorFound = NoError;
5814 SourceLocation ErrorLoc, NoteLoc;
5815 SourceRange ErrorRange, NoteRange;
5816 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5817 // If clause is a capture:
5818 // v = x++;
5819 // v = x--;
5820 // v = ++x;
5821 // v = --x;
5822 // v = x binop= expr;
5823 // v = x = x binop expr;
5824 // v = x = expr binop x;
5825 auto *AtomicBinOp =
5826 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5827 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5828 V = AtomicBinOp->getLHS();
5829 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5830 OpenMPAtomicUpdateChecker Checker(*this);
5831 if (Checker.checkStatement(
5832 Body, diag::err_omp_atomic_capture_not_expression_statement,
5833 diag::note_omp_atomic_update))
5834 return StmtError();
5835 E = Checker.getExpr();
5836 X = Checker.getX();
5837 UE = Checker.getUpdateExpr();
5838 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5839 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005840 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005841 ErrorLoc = AtomicBody->getExprLoc();
5842 ErrorRange = AtomicBody->getSourceRange();
5843 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5844 : AtomicBody->getExprLoc();
5845 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5846 : AtomicBody->getSourceRange();
5847 ErrorFound = NotAnAssignmentOp;
5848 }
5849 if (ErrorFound != NoError) {
5850 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5851 << ErrorRange;
5852 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5853 return StmtError();
5854 } else if (CurContext->isDependentContext()) {
5855 UE = V = E = X = nullptr;
5856 }
5857 } else {
5858 // If clause is a capture:
5859 // { v = x; x = expr; }
5860 // { v = x; x++; }
5861 // { v = x; x--; }
5862 // { v = x; ++x; }
5863 // { v = x; --x; }
5864 // { v = x; x binop= expr; }
5865 // { v = x; x = x binop expr; }
5866 // { v = x; x = expr binop x; }
5867 // { x++; v = x; }
5868 // { x--; v = x; }
5869 // { ++x; v = x; }
5870 // { --x; v = x; }
5871 // { x binop= expr; v = x; }
5872 // { x = x binop expr; v = x; }
5873 // { x = expr binop x; v = x; }
5874 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5875 // Check that this is { expr1; expr2; }
5876 if (CS->size() == 2) {
5877 auto *First = CS->body_front();
5878 auto *Second = CS->body_back();
5879 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5880 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5881 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5882 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5883 // Need to find what subexpression is 'v' and what is 'x'.
5884 OpenMPAtomicUpdateChecker Checker(*this);
5885 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5886 BinaryOperator *BinOp = nullptr;
5887 if (IsUpdateExprFound) {
5888 BinOp = dyn_cast<BinaryOperator>(First);
5889 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5890 }
5891 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5892 // { v = x; x++; }
5893 // { v = x; x--; }
5894 // { v = x; ++x; }
5895 // { v = x; --x; }
5896 // { v = x; x binop= expr; }
5897 // { v = x; x = x binop expr; }
5898 // { v = x; x = expr binop x; }
5899 // Check that the first expression has form v = x.
5900 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5901 llvm::FoldingSetNodeID XId, PossibleXId;
5902 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5903 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5904 IsUpdateExprFound = XId == PossibleXId;
5905 if (IsUpdateExprFound) {
5906 V = BinOp->getLHS();
5907 X = Checker.getX();
5908 E = Checker.getExpr();
5909 UE = Checker.getUpdateExpr();
5910 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005911 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005912 }
5913 }
5914 if (!IsUpdateExprFound) {
5915 IsUpdateExprFound = !Checker.checkStatement(First);
5916 BinOp = nullptr;
5917 if (IsUpdateExprFound) {
5918 BinOp = dyn_cast<BinaryOperator>(Second);
5919 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5920 }
5921 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5922 // { x++; v = x; }
5923 // { x--; v = x; }
5924 // { ++x; v = x; }
5925 // { --x; v = x; }
5926 // { x binop= expr; v = x; }
5927 // { x = x binop expr; v = x; }
5928 // { x = expr binop x; v = x; }
5929 // Check that the second expression has form v = x.
5930 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5931 llvm::FoldingSetNodeID XId, PossibleXId;
5932 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5933 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5934 IsUpdateExprFound = XId == PossibleXId;
5935 if (IsUpdateExprFound) {
5936 V = BinOp->getLHS();
5937 X = Checker.getX();
5938 E = Checker.getExpr();
5939 UE = Checker.getUpdateExpr();
5940 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005941 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005942 }
5943 }
5944 }
5945 if (!IsUpdateExprFound) {
5946 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005947 auto *FirstExpr = dyn_cast<Expr>(First);
5948 auto *SecondExpr = dyn_cast<Expr>(Second);
5949 if (!FirstExpr || !SecondExpr ||
5950 !(FirstExpr->isInstantiationDependent() ||
5951 SecondExpr->isInstantiationDependent())) {
5952 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5953 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005954 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005955 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5956 : First->getLocStart();
5957 NoteRange = ErrorRange = FirstBinOp
5958 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005959 : SourceRange(ErrorLoc, ErrorLoc);
5960 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005961 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5962 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5963 ErrorFound = NotAnAssignmentOp;
5964 NoteLoc = ErrorLoc = SecondBinOp
5965 ? SecondBinOp->getOperatorLoc()
5966 : Second->getLocStart();
5967 NoteRange = ErrorRange =
5968 SecondBinOp ? SecondBinOp->getSourceRange()
5969 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005970 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005971 auto *PossibleXRHSInFirst =
5972 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5973 auto *PossibleXLHSInSecond =
5974 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5975 llvm::FoldingSetNodeID X1Id, X2Id;
5976 PossibleXRHSInFirst->Profile(X1Id, Context,
5977 /*Canonical=*/true);
5978 PossibleXLHSInSecond->Profile(X2Id, Context,
5979 /*Canonical=*/true);
5980 IsUpdateExprFound = X1Id == X2Id;
5981 if (IsUpdateExprFound) {
5982 V = FirstBinOp->getLHS();
5983 X = SecondBinOp->getLHS();
5984 E = SecondBinOp->getRHS();
5985 UE = nullptr;
5986 IsXLHSInRHSPart = false;
5987 IsPostfixUpdate = true;
5988 } else {
5989 ErrorFound = NotASpecificExpression;
5990 ErrorLoc = FirstBinOp->getExprLoc();
5991 ErrorRange = FirstBinOp->getSourceRange();
5992 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5993 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5994 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005995 }
5996 }
5997 }
5998 }
5999 } else {
6000 NoteLoc = ErrorLoc = Body->getLocStart();
6001 NoteRange = ErrorRange =
6002 SourceRange(Body->getLocStart(), Body->getLocStart());
6003 ErrorFound = NotTwoSubstatements;
6004 }
6005 } else {
6006 NoteLoc = ErrorLoc = Body->getLocStart();
6007 NoteRange = ErrorRange =
6008 SourceRange(Body->getLocStart(), Body->getLocStart());
6009 ErrorFound = NotACompoundStatement;
6010 }
6011 if (ErrorFound != NoError) {
6012 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6013 << ErrorRange;
6014 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6015 return StmtError();
6016 } else if (CurContext->isDependentContext()) {
6017 UE = V = E = X = nullptr;
6018 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006019 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006020 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006021
6022 getCurFunction()->setHasBranchProtectedScope();
6023
Alexey Bataev62cec442014-11-18 10:14:22 +00006024 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006025 X, V, E, UE, IsXLHSInRHSPart,
6026 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006027}
6028
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006029StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6030 Stmt *AStmt,
6031 SourceLocation StartLoc,
6032 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006033 if (!AStmt)
6034 return StmtError();
6035
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006036 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6037 // 1.2.2 OpenMP Language Terminology
6038 // Structured block - An executable statement with a single entry at the
6039 // top and a single exit at the bottom.
6040 // The point of exit cannot be a branch out of the structured block.
6041 // longjmp() and throw() must not violate the entry/exit criteria.
6042 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006043
Alexey Bataev13314bf2014-10-09 04:18:56 +00006044 // OpenMP [2.16, Nesting of Regions]
6045 // If specified, a teams construct must be contained within a target
6046 // construct. That target construct must contain no statements or directives
6047 // outside of the teams construct.
6048 if (DSAStack->hasInnerTeamsRegion()) {
6049 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6050 bool OMPTeamsFound = true;
6051 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6052 auto I = CS->body_begin();
6053 while (I != CS->body_end()) {
6054 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6055 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6056 OMPTeamsFound = false;
6057 break;
6058 }
6059 ++I;
6060 }
6061 assert(I != CS->body_end() && "Not found statement");
6062 S = *I;
6063 }
6064 if (!OMPTeamsFound) {
6065 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6066 Diag(DSAStack->getInnerTeamsRegionLoc(),
6067 diag::note_omp_nested_teams_construct_here);
6068 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6069 << isa<OMPExecutableDirective>(S);
6070 return StmtError();
6071 }
6072 }
6073
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006074 getCurFunction()->setHasBranchProtectedScope();
6075
6076 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6077}
6078
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006079StmtResult
6080Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6081 Stmt *AStmt, SourceLocation StartLoc,
6082 SourceLocation EndLoc) {
6083 if (!AStmt)
6084 return StmtError();
6085
6086 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6087 // 1.2.2 OpenMP Language Terminology
6088 // Structured block - An executable statement with a single entry at the
6089 // top and a single exit at the bottom.
6090 // The point of exit cannot be a branch out of the structured block.
6091 // longjmp() and throw() must not violate the entry/exit criteria.
6092 CS->getCapturedDecl()->setNothrow();
6093
6094 getCurFunction()->setHasBranchProtectedScope();
6095
6096 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6097 AStmt);
6098}
6099
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006100StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6101 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6102 SourceLocation EndLoc,
6103 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6104 if (!AStmt)
6105 return StmtError();
6106
6107 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6108 // 1.2.2 OpenMP Language Terminology
6109 // Structured block - An executable statement with a single entry at the
6110 // top and a single exit at the bottom.
6111 // The point of exit cannot be a branch out of the structured block.
6112 // longjmp() and throw() must not violate the entry/exit criteria.
6113 CS->getCapturedDecl()->setNothrow();
6114
6115 OMPLoopDirective::HelperExprs B;
6116 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6117 // define the nested loops number.
6118 unsigned NestedLoopCount =
6119 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6120 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6121 VarsWithImplicitDSA, B);
6122 if (NestedLoopCount == 0)
6123 return StmtError();
6124
6125 assert((CurContext->isDependentContext() || B.builtAll()) &&
6126 "omp target parallel for loop exprs were not built");
6127
6128 if (!CurContext->isDependentContext()) {
6129 // Finalize the clauses that need pre-built expressions for CodeGen.
6130 for (auto C : Clauses) {
6131 if (auto LC = dyn_cast<OMPLinearClause>(C))
6132 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6133 B.NumIterations, *this, CurScope))
6134 return StmtError();
6135 }
6136 }
6137
6138 getCurFunction()->setHasBranchProtectedScope();
6139 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6140 NestedLoopCount, Clauses, AStmt,
6141 B, DSAStack->isCancelRegion());
6142}
6143
Samuel Antaodf67fc42016-01-19 19:15:56 +00006144/// \brief Check for existence of a map clause in the list of clauses.
6145static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6146 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6147 I != E; ++I) {
6148 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6149 return true;
6150 }
6151 }
6152
6153 return false;
6154}
6155
Michael Wong65f367f2015-07-21 13:44:28 +00006156StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6157 Stmt *AStmt,
6158 SourceLocation StartLoc,
6159 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006160 if (!AStmt)
6161 return StmtError();
6162
6163 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6164
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006165 // OpenMP [2.10.1, Restrictions, p. 97]
6166 // At least one map clause must appear on the directive.
6167 if (!HasMapClause(Clauses)) {
6168 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6169 getOpenMPDirectiveName(OMPD_target_data);
6170 return StmtError();
6171 }
6172
Michael Wong65f367f2015-07-21 13:44:28 +00006173 getCurFunction()->setHasBranchProtectedScope();
6174
6175 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6176 AStmt);
6177}
6178
Samuel Antaodf67fc42016-01-19 19:15:56 +00006179StmtResult
6180Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6181 SourceLocation StartLoc,
6182 SourceLocation EndLoc) {
6183 // OpenMP [2.10.2, Restrictions, p. 99]
6184 // At least one map clause must appear on the directive.
6185 if (!HasMapClause(Clauses)) {
6186 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6187 << getOpenMPDirectiveName(OMPD_target_enter_data);
6188 return StmtError();
6189 }
6190
6191 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6192 Clauses);
6193}
6194
Samuel Antao72590762016-01-19 20:04:50 +00006195StmtResult
6196Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6197 SourceLocation StartLoc,
6198 SourceLocation EndLoc) {
6199 // OpenMP [2.10.3, Restrictions, p. 102]
6200 // At least one map clause must appear on the directive.
6201 if (!HasMapClause(Clauses)) {
6202 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6203 << getOpenMPDirectiveName(OMPD_target_exit_data);
6204 return StmtError();
6205 }
6206
6207 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6208}
6209
Alexey Bataev13314bf2014-10-09 04:18:56 +00006210StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6211 Stmt *AStmt, SourceLocation StartLoc,
6212 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006213 if (!AStmt)
6214 return StmtError();
6215
Alexey Bataev13314bf2014-10-09 04:18:56 +00006216 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6217 // 1.2.2 OpenMP Language Terminology
6218 // Structured block - An executable statement with a single entry at the
6219 // top and a single exit at the bottom.
6220 // The point of exit cannot be a branch out of the structured block.
6221 // longjmp() and throw() must not violate the entry/exit criteria.
6222 CS->getCapturedDecl()->setNothrow();
6223
6224 getCurFunction()->setHasBranchProtectedScope();
6225
6226 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6227}
6228
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006229StmtResult
6230Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6231 SourceLocation EndLoc,
6232 OpenMPDirectiveKind CancelRegion) {
6233 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6234 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6235 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6236 << getOpenMPDirectiveName(CancelRegion);
6237 return StmtError();
6238 }
6239 if (DSAStack->isParentNowaitRegion()) {
6240 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6241 return StmtError();
6242 }
6243 if (DSAStack->isParentOrderedRegion()) {
6244 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6245 return StmtError();
6246 }
6247 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6248 CancelRegion);
6249}
6250
Alexey Bataev87933c72015-09-18 08:07:34 +00006251StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6252 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006253 SourceLocation EndLoc,
6254 OpenMPDirectiveKind CancelRegion) {
6255 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6256 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6257 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6258 << getOpenMPDirectiveName(CancelRegion);
6259 return StmtError();
6260 }
6261 if (DSAStack->isParentNowaitRegion()) {
6262 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6263 return StmtError();
6264 }
6265 if (DSAStack->isParentOrderedRegion()) {
6266 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6267 return StmtError();
6268 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006269 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006270 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6271 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006272}
6273
Alexey Bataev382967a2015-12-08 12:06:20 +00006274static bool checkGrainsizeNumTasksClauses(Sema &S,
6275 ArrayRef<OMPClause *> Clauses) {
6276 OMPClause *PrevClause = nullptr;
6277 bool ErrorFound = false;
6278 for (auto *C : Clauses) {
6279 if (C->getClauseKind() == OMPC_grainsize ||
6280 C->getClauseKind() == OMPC_num_tasks) {
6281 if (!PrevClause)
6282 PrevClause = C;
6283 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6284 S.Diag(C->getLocStart(),
6285 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6286 << getOpenMPClauseName(C->getClauseKind())
6287 << getOpenMPClauseName(PrevClause->getClauseKind());
6288 S.Diag(PrevClause->getLocStart(),
6289 diag::note_omp_previous_grainsize_num_tasks)
6290 << getOpenMPClauseName(PrevClause->getClauseKind());
6291 ErrorFound = true;
6292 }
6293 }
6294 }
6295 return ErrorFound;
6296}
6297
Alexey Bataev49f6e782015-12-01 04:18:41 +00006298StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6299 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6300 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006301 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006302 if (!AStmt)
6303 return StmtError();
6304
6305 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6306 OMPLoopDirective::HelperExprs B;
6307 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6308 // define the nested loops number.
6309 unsigned NestedLoopCount =
6310 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006311 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006312 VarsWithImplicitDSA, B);
6313 if (NestedLoopCount == 0)
6314 return StmtError();
6315
6316 assert((CurContext->isDependentContext() || B.builtAll()) &&
6317 "omp for loop exprs were not built");
6318
Alexey Bataev382967a2015-12-08 12:06:20 +00006319 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6320 // The grainsize clause and num_tasks clause are mutually exclusive and may
6321 // not appear on the same taskloop directive.
6322 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6323 return StmtError();
6324
Alexey Bataev49f6e782015-12-01 04:18:41 +00006325 getCurFunction()->setHasBranchProtectedScope();
6326 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6327 NestedLoopCount, Clauses, AStmt, B);
6328}
6329
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006330StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6331 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6332 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006333 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006334 if (!AStmt)
6335 return StmtError();
6336
6337 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6338 OMPLoopDirective::HelperExprs B;
6339 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6340 // define the nested loops number.
6341 unsigned NestedLoopCount =
6342 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6343 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6344 VarsWithImplicitDSA, B);
6345 if (NestedLoopCount == 0)
6346 return StmtError();
6347
6348 assert((CurContext->isDependentContext() || B.builtAll()) &&
6349 "omp for loop exprs were not built");
6350
Alexey Bataev5a3af132016-03-29 08:58:54 +00006351 if (!CurContext->isDependentContext()) {
6352 // Finalize the clauses that need pre-built expressions for CodeGen.
6353 for (auto C : Clauses) {
6354 if (auto LC = dyn_cast<OMPLinearClause>(C))
6355 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6356 B.NumIterations, *this, CurScope))
6357 return StmtError();
6358 }
6359 }
6360
Alexey Bataev382967a2015-12-08 12:06:20 +00006361 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6362 // The grainsize clause and num_tasks clause are mutually exclusive and may
6363 // not appear on the same taskloop directive.
6364 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6365 return StmtError();
6366
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006367 getCurFunction()->setHasBranchProtectedScope();
6368 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6369 NestedLoopCount, Clauses, AStmt, B);
6370}
6371
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006372StmtResult Sema::ActOnOpenMPDistributeDirective(
6373 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6374 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006375 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006376 if (!AStmt)
6377 return StmtError();
6378
6379 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6380 OMPLoopDirective::HelperExprs B;
6381 // In presence of clause 'collapse' with number of loops, it will
6382 // define the nested loops number.
6383 unsigned NestedLoopCount =
6384 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6385 nullptr /*ordered not a clause on distribute*/, AStmt,
6386 *this, *DSAStack, VarsWithImplicitDSA, B);
6387 if (NestedLoopCount == 0)
6388 return StmtError();
6389
6390 assert((CurContext->isDependentContext() || B.builtAll()) &&
6391 "omp for loop exprs were not built");
6392
6393 getCurFunction()->setHasBranchProtectedScope();
6394 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6395 NestedLoopCount, Clauses, AStmt, B);
6396}
6397
Alexey Bataeved09d242014-05-28 05:53:51 +00006398OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006399 SourceLocation StartLoc,
6400 SourceLocation LParenLoc,
6401 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006402 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006403 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006404 case OMPC_final:
6405 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6406 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006407 case OMPC_num_threads:
6408 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6409 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006410 case OMPC_safelen:
6411 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6412 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006413 case OMPC_simdlen:
6414 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6415 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006416 case OMPC_collapse:
6417 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6418 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006419 case OMPC_ordered:
6420 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6421 break;
Michael Wonge710d542015-08-07 16:16:36 +00006422 case OMPC_device:
6423 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6424 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006425 case OMPC_num_teams:
6426 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6427 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006428 case OMPC_thread_limit:
6429 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6430 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006431 case OMPC_priority:
6432 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6433 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006434 case OMPC_grainsize:
6435 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6436 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006437 case OMPC_num_tasks:
6438 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6439 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006440 case OMPC_hint:
6441 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6442 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006443 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006444 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006445 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006446 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006447 case OMPC_private:
6448 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006449 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006450 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006451 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006452 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006453 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006454 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006455 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006456 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006457 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006458 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006459 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006460 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006461 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006462 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006463 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006464 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006465 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006466 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006467 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006468 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006469 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006470 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006471 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006472 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006473 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006474 case OMPC_uniform:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006475 llvm_unreachable("Clause is not allowed.");
6476 }
6477 return Res;
6478}
6479
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006480OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6481 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006482 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006483 SourceLocation NameModifierLoc,
6484 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006485 SourceLocation EndLoc) {
6486 Expr *ValExpr = Condition;
6487 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6488 !Condition->isInstantiationDependent() &&
6489 !Condition->containsUnexpandedParameterPack()) {
6490 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006491 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006492 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006493 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006494
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006495 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006496 }
6497
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006498 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6499 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006500}
6501
Alexey Bataev3778b602014-07-17 07:32:53 +00006502OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6503 SourceLocation StartLoc,
6504 SourceLocation LParenLoc,
6505 SourceLocation EndLoc) {
6506 Expr *ValExpr = Condition;
6507 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6508 !Condition->isInstantiationDependent() &&
6509 !Condition->containsUnexpandedParameterPack()) {
6510 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6511 Condition->getExprLoc(), Condition);
6512 if (Val.isInvalid())
6513 return nullptr;
6514
6515 ValExpr = Val.get();
6516 }
6517
6518 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6519}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006520ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6521 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006522 if (!Op)
6523 return ExprError();
6524
6525 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6526 public:
6527 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006528 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006529 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6530 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006531 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6532 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006533 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6534 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006535 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6536 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006537 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6538 QualType T,
6539 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006540 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6541 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006542 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6543 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006544 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006545 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006546 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006547 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6548 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006549 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6550 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006551 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6552 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006553 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006554 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006555 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006556 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6557 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006558 llvm_unreachable("conversion functions are permitted");
6559 }
6560 } ConvertDiagnoser;
6561 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6562}
6563
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006564static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006565 OpenMPClauseKind CKind,
6566 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006567 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6568 !ValExpr->isInstantiationDependent()) {
6569 SourceLocation Loc = ValExpr->getExprLoc();
6570 ExprResult Value =
6571 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6572 if (Value.isInvalid())
6573 return false;
6574
6575 ValExpr = Value.get();
6576 // The expression must evaluate to a non-negative integer value.
6577 llvm::APSInt Result;
6578 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006579 Result.isSigned() &&
6580 !((!StrictlyPositive && Result.isNonNegative()) ||
6581 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006582 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006583 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6584 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006585 return false;
6586 }
6587 }
6588 return true;
6589}
6590
Alexey Bataev568a8332014-03-06 06:15:19 +00006591OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6592 SourceLocation StartLoc,
6593 SourceLocation LParenLoc,
6594 SourceLocation EndLoc) {
6595 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006596
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006597 // OpenMP [2.5, Restrictions]
6598 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006599 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6600 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006601 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006602
Alexey Bataeved09d242014-05-28 05:53:51 +00006603 return new (Context)
6604 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006605}
6606
Alexey Bataev62c87d22014-03-21 04:51:18 +00006607ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006608 OpenMPClauseKind CKind,
6609 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006610 if (!E)
6611 return ExprError();
6612 if (E->isValueDependent() || E->isTypeDependent() ||
6613 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006614 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006615 llvm::APSInt Result;
6616 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6617 if (ICE.isInvalid())
6618 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006619 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6620 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006621 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006622 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6623 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006624 return ExprError();
6625 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006626 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6627 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6628 << E->getSourceRange();
6629 return ExprError();
6630 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006631 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6632 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006633 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006634 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006635 return ICE;
6636}
6637
6638OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6639 SourceLocation LParenLoc,
6640 SourceLocation EndLoc) {
6641 // OpenMP [2.8.1, simd construct, Description]
6642 // The parameter of the safelen clause must be a constant
6643 // positive integer expression.
6644 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6645 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006646 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006647 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006648 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006649}
6650
Alexey Bataev66b15b52015-08-21 11:14:16 +00006651OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6652 SourceLocation LParenLoc,
6653 SourceLocation EndLoc) {
6654 // OpenMP [2.8.1, simd construct, Description]
6655 // The parameter of the simdlen clause must be a constant
6656 // positive integer expression.
6657 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6658 if (Simdlen.isInvalid())
6659 return nullptr;
6660 return new (Context)
6661 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6662}
6663
Alexander Musman64d33f12014-06-04 07:53:32 +00006664OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6665 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006666 SourceLocation LParenLoc,
6667 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006668 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006669 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006670 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006671 // The parameter of the collapse clause must be a constant
6672 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006673 ExprResult NumForLoopsResult =
6674 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6675 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006676 return nullptr;
6677 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006678 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006679}
6680
Alexey Bataev10e775f2015-07-30 11:36:16 +00006681OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6682 SourceLocation EndLoc,
6683 SourceLocation LParenLoc,
6684 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006685 // OpenMP [2.7.1, loop construct, Description]
6686 // OpenMP [2.8.1, simd construct, Description]
6687 // OpenMP [2.9.6, distribute construct, Description]
6688 // The parameter of the ordered clause must be a constant
6689 // positive integer expression if any.
6690 if (NumForLoops && LParenLoc.isValid()) {
6691 ExprResult NumForLoopsResult =
6692 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6693 if (NumForLoopsResult.isInvalid())
6694 return nullptr;
6695 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006696 } else
6697 NumForLoops = nullptr;
6698 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006699 return new (Context)
6700 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6701}
6702
Alexey Bataeved09d242014-05-28 05:53:51 +00006703OMPClause *Sema::ActOnOpenMPSimpleClause(
6704 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6705 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006706 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006707 switch (Kind) {
6708 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006709 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006710 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6711 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006712 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006713 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006714 Res = ActOnOpenMPProcBindClause(
6715 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6716 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006717 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006718 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006719 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006720 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006721 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006722 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006723 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006724 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006725 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006726 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006727 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006728 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006729 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006730 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006731 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006732 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006733 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006734 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006735 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006736 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006737 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006738 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006739 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006740 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006741 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006742 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006743 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006744 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006745 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006746 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006747 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006748 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006749 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006750 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006751 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006752 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006753 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006754 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006755 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006756 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006757 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006758 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006759 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006760 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006761 llvm_unreachable("Clause is not allowed.");
6762 }
6763 return Res;
6764}
6765
Alexey Bataev6402bca2015-12-28 07:25:51 +00006766static std::string
6767getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6768 ArrayRef<unsigned> Exclude = llvm::None) {
6769 std::string Values;
6770 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6771 unsigned Skipped = Exclude.size();
6772 auto S = Exclude.begin(), E = Exclude.end();
6773 for (unsigned i = First; i < Last; ++i) {
6774 if (std::find(S, E, i) != E) {
6775 --Skipped;
6776 continue;
6777 }
6778 Values += "'";
6779 Values += getOpenMPSimpleClauseTypeName(K, i);
6780 Values += "'";
6781 if (i == Bound - Skipped)
6782 Values += " or ";
6783 else if (i != Bound + 1 - Skipped)
6784 Values += ", ";
6785 }
6786 return Values;
6787}
6788
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006789OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6790 SourceLocation KindKwLoc,
6791 SourceLocation StartLoc,
6792 SourceLocation LParenLoc,
6793 SourceLocation EndLoc) {
6794 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006795 static_assert(OMPC_DEFAULT_unknown > 0,
6796 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006797 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006798 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6799 /*Last=*/OMPC_DEFAULT_unknown)
6800 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006801 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006802 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006803 switch (Kind) {
6804 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006805 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006806 break;
6807 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006808 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006809 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006810 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006811 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006812 break;
6813 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006814 return new (Context)
6815 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006816}
6817
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006818OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6819 SourceLocation KindKwLoc,
6820 SourceLocation StartLoc,
6821 SourceLocation LParenLoc,
6822 SourceLocation EndLoc) {
6823 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006824 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006825 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6826 /*Last=*/OMPC_PROC_BIND_unknown)
6827 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006828 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006829 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006830 return new (Context)
6831 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006832}
6833
Alexey Bataev56dafe82014-06-20 07:16:17 +00006834OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006835 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006836 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006837 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006838 SourceLocation EndLoc) {
6839 OMPClause *Res = nullptr;
6840 switch (Kind) {
6841 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006842 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6843 assert(Argument.size() == NumberOfElements &&
6844 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006845 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006846 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6847 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6848 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6849 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6850 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006851 break;
6852 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006853 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6854 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6855 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6856 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006857 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006858 case OMPC_dist_schedule:
6859 Res = ActOnOpenMPDistScheduleClause(
6860 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6861 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6862 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006863 case OMPC_defaultmap:
6864 enum { Modifier, DefaultmapKind };
6865 Res = ActOnOpenMPDefaultmapClause(
6866 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6867 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6868 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6869 ArgumentLoc[DefaultmapKind], EndLoc);
6870 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006871 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006872 case OMPC_num_threads:
6873 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006874 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006875 case OMPC_collapse:
6876 case OMPC_default:
6877 case OMPC_proc_bind:
6878 case OMPC_private:
6879 case OMPC_firstprivate:
6880 case OMPC_lastprivate:
6881 case OMPC_shared:
6882 case OMPC_reduction:
6883 case OMPC_linear:
6884 case OMPC_aligned:
6885 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006886 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006887 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006888 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006889 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006890 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006891 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006892 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006893 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006894 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006895 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006896 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006897 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006898 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006899 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006900 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006901 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006902 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006903 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006904 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006905 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006906 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006907 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006908 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006909 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006910 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006911 case OMPC_uniform:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006912 llvm_unreachable("Clause is not allowed.");
6913 }
6914 return Res;
6915}
6916
Alexey Bataev6402bca2015-12-28 07:25:51 +00006917static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6918 OpenMPScheduleClauseModifier M2,
6919 SourceLocation M1Loc, SourceLocation M2Loc) {
6920 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6921 SmallVector<unsigned, 2> Excluded;
6922 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6923 Excluded.push_back(M2);
6924 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6925 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6926 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6927 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6928 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6929 << getListOfPossibleValues(OMPC_schedule,
6930 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6931 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6932 Excluded)
6933 << getOpenMPClauseName(OMPC_schedule);
6934 return true;
6935 }
6936 return false;
6937}
6938
Alexey Bataev56dafe82014-06-20 07:16:17 +00006939OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006940 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006941 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006942 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6943 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6944 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6945 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6946 return nullptr;
6947 // OpenMP, 2.7.1, Loop Construct, Restrictions
6948 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6949 // but not both.
6950 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6951 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6952 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6953 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6954 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6955 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6956 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6957 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6958 return nullptr;
6959 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006960 if (Kind == OMPC_SCHEDULE_unknown) {
6961 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006962 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6963 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6964 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6965 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6966 Exclude);
6967 } else {
6968 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6969 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006970 }
6971 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6972 << Values << getOpenMPClauseName(OMPC_schedule);
6973 return nullptr;
6974 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006975 // OpenMP, 2.7.1, Loop Construct, Restrictions
6976 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6977 // schedule(guided).
6978 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6979 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6980 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6981 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6982 diag::err_omp_schedule_nonmonotonic_static);
6983 return nullptr;
6984 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006985 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00006986 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006987 if (ChunkSize) {
6988 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6989 !ChunkSize->isInstantiationDependent() &&
6990 !ChunkSize->containsUnexpandedParameterPack()) {
6991 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6992 ExprResult Val =
6993 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6994 if (Val.isInvalid())
6995 return nullptr;
6996
6997 ValExpr = Val.get();
6998
6999 // OpenMP [2.7.1, Restrictions]
7000 // chunk_size must be a loop invariant integer expression with a positive
7001 // value.
7002 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007003 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7004 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7005 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007006 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007007 return nullptr;
7008 }
7009 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007010 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7011 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7012 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007013 }
7014 }
7015 }
7016
Alexey Bataev6402bca2015-12-28 07:25:51 +00007017 return new (Context)
7018 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007019 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007020}
7021
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007022OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7023 SourceLocation StartLoc,
7024 SourceLocation EndLoc) {
7025 OMPClause *Res = nullptr;
7026 switch (Kind) {
7027 case OMPC_ordered:
7028 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7029 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007030 case OMPC_nowait:
7031 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7032 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007033 case OMPC_untied:
7034 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7035 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007036 case OMPC_mergeable:
7037 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7038 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007039 case OMPC_read:
7040 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7041 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007042 case OMPC_write:
7043 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7044 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007045 case OMPC_update:
7046 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7047 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007048 case OMPC_capture:
7049 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7050 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007051 case OMPC_seq_cst:
7052 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7053 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007054 case OMPC_threads:
7055 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7056 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007057 case OMPC_simd:
7058 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7059 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007060 case OMPC_nogroup:
7061 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7062 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007063 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007064 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007065 case OMPC_num_threads:
7066 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007067 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007068 case OMPC_collapse:
7069 case OMPC_schedule:
7070 case OMPC_private:
7071 case OMPC_firstprivate:
7072 case OMPC_lastprivate:
7073 case OMPC_shared:
7074 case OMPC_reduction:
7075 case OMPC_linear:
7076 case OMPC_aligned:
7077 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007078 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007079 case OMPC_default:
7080 case OMPC_proc_bind:
7081 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007082 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007083 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007084 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007085 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007086 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007087 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007088 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007089 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007090 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007091 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007092 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007093 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007094 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007095 case OMPC_uniform:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007096 llvm_unreachable("Clause is not allowed.");
7097 }
7098 return Res;
7099}
7100
Alexey Bataev236070f2014-06-20 11:19:47 +00007101OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7102 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007103 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007104 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7105}
7106
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007107OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7108 SourceLocation EndLoc) {
7109 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7110}
7111
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007112OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7113 SourceLocation EndLoc) {
7114 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7115}
7116
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007117OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7118 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007119 return new (Context) OMPReadClause(StartLoc, EndLoc);
7120}
7121
Alexey Bataevdea47612014-07-23 07:46:59 +00007122OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7123 SourceLocation EndLoc) {
7124 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7125}
7126
Alexey Bataev67a4f222014-07-23 10:25:33 +00007127OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7128 SourceLocation EndLoc) {
7129 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7130}
7131
Alexey Bataev459dec02014-07-24 06:46:57 +00007132OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7133 SourceLocation EndLoc) {
7134 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7135}
7136
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007137OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7138 SourceLocation EndLoc) {
7139 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7140}
7141
Alexey Bataev346265e2015-09-25 10:37:12 +00007142OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7143 SourceLocation EndLoc) {
7144 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7145}
7146
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007147OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7148 SourceLocation EndLoc) {
7149 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7150}
7151
Alexey Bataevb825de12015-12-07 10:51:44 +00007152OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7153 SourceLocation EndLoc) {
7154 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7155}
7156
Alexey Bataevc5e02582014-06-16 07:08:35 +00007157OMPClause *Sema::ActOnOpenMPVarListClause(
7158 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7159 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7160 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007161 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007162 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7163 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7164 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007165 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007166 switch (Kind) {
7167 case OMPC_private:
7168 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7169 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007170 case OMPC_firstprivate:
7171 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7172 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007173 case OMPC_lastprivate:
7174 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7175 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007176 case OMPC_shared:
7177 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7178 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007179 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007180 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7181 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007182 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007183 case OMPC_linear:
7184 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007185 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007186 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007187 case OMPC_aligned:
7188 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7189 ColonLoc, EndLoc);
7190 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007191 case OMPC_copyin:
7192 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7193 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007194 case OMPC_copyprivate:
7195 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7196 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007197 case OMPC_flush:
7198 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7199 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007200 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007201 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7202 StartLoc, LParenLoc, EndLoc);
7203 break;
7204 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007205 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7206 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7207 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007208 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007209 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007210 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007211 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007212 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007213 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007214 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007215 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007216 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007217 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007218 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007219 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007220 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007221 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007222 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007223 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007224 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007225 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007226 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007227 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007228 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007229 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007230 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007231 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007232 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007233 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007234 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007235 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007236 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007237 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007238 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007239 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007240 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007241 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007242 llvm_unreachable("Clause is not allowed.");
7243 }
7244 return Res;
7245}
7246
Alexey Bataev90c228f2016-02-08 09:29:13 +00007247ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007248 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007249 ExprResult Res = BuildDeclRefExpr(
7250 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7251 if (!Res.isUsable())
7252 return ExprError();
7253 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7254 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7255 if (!Res.isUsable())
7256 return ExprError();
7257 }
7258 if (VK != VK_LValue && Res.get()->isGLValue()) {
7259 Res = DefaultLvalueConversion(Res.get());
7260 if (!Res.isUsable())
7261 return ExprError();
7262 }
7263 return Res;
7264}
7265
Alexey Bataev60da77e2016-02-29 05:54:20 +00007266static std::pair<ValueDecl *, bool>
7267getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7268 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007269 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7270 RefExpr->containsUnexpandedParameterPack())
7271 return std::make_pair(nullptr, true);
7272
Alexey Bataevd985eda2016-02-10 11:29:16 +00007273 // OpenMP [3.1, C/C++]
7274 // A list item is a variable name.
7275 // OpenMP [2.9.3.3, Restrictions, p.1]
7276 // A variable that is part of another variable (as an array or
7277 // structure element) cannot appear in a private clause.
7278 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007279 enum {
7280 NoArrayExpr = -1,
7281 ArraySubscript = 0,
7282 OMPArraySection = 1
7283 } IsArrayExpr = NoArrayExpr;
7284 if (AllowArraySection) {
7285 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7286 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7287 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7288 Base = TempASE->getBase()->IgnoreParenImpCasts();
7289 RefExpr = Base;
7290 IsArrayExpr = ArraySubscript;
7291 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7292 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7293 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7294 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7295 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7296 Base = TempASE->getBase()->IgnoreParenImpCasts();
7297 RefExpr = Base;
7298 IsArrayExpr = OMPArraySection;
7299 }
7300 }
7301 ELoc = RefExpr->getExprLoc();
7302 ERange = RefExpr->getSourceRange();
7303 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007304 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7305 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7306 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7307 (S.getCurrentThisType().isNull() || !ME ||
7308 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7309 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007310 if (IsArrayExpr != NoArrayExpr)
7311 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7312 << ERange;
7313 else {
7314 S.Diag(ELoc,
7315 AllowArraySection
7316 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7317 : diag::err_omp_expected_var_name_member_expr)
7318 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7319 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007320 return std::make_pair(nullptr, false);
7321 }
7322 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7323}
7324
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007325OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7326 SourceLocation StartLoc,
7327 SourceLocation LParenLoc,
7328 SourceLocation EndLoc) {
7329 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007330 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007331 for (auto &RefExpr : VarList) {
7332 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007333 SourceLocation ELoc;
7334 SourceRange ERange;
7335 Expr *SimpleRefExpr = RefExpr;
7336 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007337 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007338 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007339 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007340 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007341 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007342 ValueDecl *D = Res.first;
7343 if (!D)
7344 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007345
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007346 QualType Type = D->getType();
7347 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007348
7349 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7350 // A variable that appears in a private clause must not have an incomplete
7351 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007352 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007353 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007354 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007355
Alexey Bataev758e55e2013-09-06 18:03:48 +00007356 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7357 // in a Construct]
7358 // Variables with the predetermined data-sharing attributes may not be
7359 // listed in data-sharing attributes clauses, except for the cases
7360 // listed below. For these exceptions only, listing a predetermined
7361 // variable in a data-sharing attribute clause is allowed and overrides
7362 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007363 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007364 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007365 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7366 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007367 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007368 continue;
7369 }
7370
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007371 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007372 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007373 DSAStack->getCurrentDirective() == OMPD_task) {
7374 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7375 << getOpenMPClauseName(OMPC_private) << Type
7376 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7377 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007378 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007379 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007380 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007381 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007382 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007383 continue;
7384 }
7385
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007386 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7387 // A list item cannot appear in both a map clause and a data-sharing
7388 // attribute clause on the same construct
7389 if (DSAStack->getCurrentDirective() == OMPD_target) {
7390 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7391 [&](Expr *RE) -> bool {return true;})) {
7392 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7393 << getOpenMPClauseName(OMPC_private)
7394 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7395 ReportOriginalDSA(*this, DSAStack, D, DVar);
7396 continue;
7397 }
7398 }
7399
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007400 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7401 // A variable of class type (or array thereof) that appears in a private
7402 // clause requires an accessible, unambiguous default constructor for the
7403 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007404 // Generate helper private variable and initialize it with the default
7405 // value. The address of the original variable is replaced by the address of
7406 // the new private variable in CodeGen. This new variable is not added to
7407 // IdResolver, so the code in the OpenMP region uses original variable for
7408 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007409 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007410 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7411 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007412 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007413 if (VDPrivate->isInvalidDecl())
7414 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007415 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007416 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007417
Alexey Bataev90c228f2016-02-08 09:29:13 +00007418 DeclRefExpr *Ref = nullptr;
7419 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007420 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007421 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7422 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007423 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007424 }
7425
Alexey Bataeved09d242014-05-28 05:53:51 +00007426 if (Vars.empty())
7427 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007428
Alexey Bataev03b340a2014-10-21 03:16:40 +00007429 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7430 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007431}
7432
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007433namespace {
7434class DiagsUninitializedSeveretyRAII {
7435private:
7436 DiagnosticsEngine &Diags;
7437 SourceLocation SavedLoc;
7438 bool IsIgnored;
7439
7440public:
7441 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7442 bool IsIgnored)
7443 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7444 if (!IsIgnored) {
7445 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7446 /*Map*/ diag::Severity::Ignored, Loc);
7447 }
7448 }
7449 ~DiagsUninitializedSeveretyRAII() {
7450 if (!IsIgnored)
7451 Diags.popMappings(SavedLoc);
7452 }
7453};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007454}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007455
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007456OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7457 SourceLocation StartLoc,
7458 SourceLocation LParenLoc,
7459 SourceLocation EndLoc) {
7460 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007461 SmallVector<Expr *, 8> PrivateCopies;
7462 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007463 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007464 bool IsImplicitClause =
7465 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7466 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7467
Alexey Bataeved09d242014-05-28 05:53:51 +00007468 for (auto &RefExpr : VarList) {
7469 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007470 SourceLocation ELoc;
7471 SourceRange ERange;
7472 Expr *SimpleRefExpr = RefExpr;
7473 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007474 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007475 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007476 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007477 PrivateCopies.push_back(nullptr);
7478 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007479 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007480 ValueDecl *D = Res.first;
7481 if (!D)
7482 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007483
Alexey Bataev60da77e2016-02-29 05:54:20 +00007484 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007485 QualType Type = D->getType();
7486 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007487
7488 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7489 // A variable that appears in a private clause must not have an incomplete
7490 // type or a reference type.
7491 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007492 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007493 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007494 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007495
7496 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7497 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007498 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007499 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007500 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007501
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007502 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007503 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007504 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007505 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007506 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007507 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007508 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7509 // A list item that specifies a given variable may not appear in more
7510 // than one clause on the same directive, except that a variable may be
7511 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007512 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007513 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007514 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007515 << getOpenMPClauseName(DVar.CKind)
7516 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007517 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007518 continue;
7519 }
7520
7521 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7522 // in a Construct]
7523 // Variables with the predetermined data-sharing attributes may not be
7524 // listed in data-sharing attributes clauses, except for the cases
7525 // listed below. For these exceptions only, listing a predetermined
7526 // variable in a data-sharing attribute clause is allowed and overrides
7527 // the variable's predetermined data-sharing attributes.
7528 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7529 // in a Construct, C/C++, p.2]
7530 // Variables with const-qualified type having no mutable member may be
7531 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007532 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007533 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7534 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007535 << getOpenMPClauseName(DVar.CKind)
7536 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007537 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007538 continue;
7539 }
7540
Alexey Bataevf29276e2014-06-18 04:14:57 +00007541 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007542 // OpenMP [2.9.3.4, Restrictions, p.2]
7543 // A list item that is private within a parallel region must not appear
7544 // in a firstprivate clause on a worksharing construct if any of the
7545 // worksharing regions arising from the worksharing construct ever bind
7546 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007547 if (isOpenMPWorksharingDirective(CurrDir) &&
7548 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007549 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007550 if (DVar.CKind != OMPC_shared &&
7551 (isOpenMPParallelDirective(DVar.DKind) ||
7552 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007553 Diag(ELoc, diag::err_omp_required_access)
7554 << getOpenMPClauseName(OMPC_firstprivate)
7555 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007556 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007557 continue;
7558 }
7559 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007560 // OpenMP [2.9.3.4, Restrictions, p.3]
7561 // A list item that appears in a reduction clause of a parallel construct
7562 // must not appear in a firstprivate clause on a worksharing or task
7563 // construct if any of the worksharing or task regions arising from the
7564 // worksharing or task construct ever bind to any of the parallel regions
7565 // arising from the parallel construct.
7566 // OpenMP [2.9.3.4, Restrictions, p.4]
7567 // A list item that appears in a reduction clause in worksharing
7568 // construct must not appear in a firstprivate clause in a task construct
7569 // encountered during execution of any of the worksharing regions arising
7570 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007571 if (CurrDir == OMPD_task) {
7572 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007573 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007574 [](OpenMPDirectiveKind K) -> bool {
7575 return isOpenMPParallelDirective(K) ||
7576 isOpenMPWorksharingDirective(K);
7577 },
7578 false);
7579 if (DVar.CKind == OMPC_reduction &&
7580 (isOpenMPParallelDirective(DVar.DKind) ||
7581 isOpenMPWorksharingDirective(DVar.DKind))) {
7582 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7583 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007584 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007585 continue;
7586 }
7587 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007588
7589 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7590 // A list item that is private within a teams region must not appear in a
7591 // firstprivate clause on a distribute construct if any of the distribute
7592 // regions arising from the distribute construct ever bind to any of the
7593 // teams regions arising from the teams construct.
7594 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7595 // A list item that appears in a reduction clause of a teams construct
7596 // must not appear in a firstprivate clause on a distribute construct if
7597 // any of the distribute regions arising from the distribute construct
7598 // ever bind to any of the teams regions arising from the teams construct.
7599 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7600 // A list item may appear in a firstprivate or lastprivate clause but not
7601 // both.
7602 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007603 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007604 [](OpenMPDirectiveKind K) -> bool {
7605 return isOpenMPTeamsDirective(K);
7606 },
7607 false);
7608 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7609 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007610 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007611 continue;
7612 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007613 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007614 [](OpenMPDirectiveKind K) -> bool {
7615 return isOpenMPTeamsDirective(K);
7616 },
7617 false);
7618 if (DVar.CKind == OMPC_reduction &&
7619 isOpenMPTeamsDirective(DVar.DKind)) {
7620 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007621 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007622 continue;
7623 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007624 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007625 if (DVar.CKind == OMPC_lastprivate) {
7626 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007627 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007628 continue;
7629 }
7630 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007631 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7632 // A list item cannot appear in both a map clause and a data-sharing
7633 // attribute clause on the same construct
7634 if (CurrDir == OMPD_target) {
7635 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7636 [&](Expr *RE) -> bool {return true;})) {
7637 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7638 << getOpenMPClauseName(OMPC_firstprivate)
7639 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7640 ReportOriginalDSA(*this, DSAStack, D, DVar);
7641 continue;
7642 }
7643 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007644 }
7645
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007646 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007647 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007648 DSAStack->getCurrentDirective() == OMPD_task) {
7649 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7650 << getOpenMPClauseName(OMPC_firstprivate) << Type
7651 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7652 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007653 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007654 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007655 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007656 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007657 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007658 continue;
7659 }
7660
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007661 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007662 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7663 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007664 // Generate helper private variable and initialize it with the value of the
7665 // original variable. The address of the original variable is replaced by
7666 // the address of the new private variable in the CodeGen. This new variable
7667 // is not added to IdResolver, so the code in the OpenMP region uses
7668 // original variable for proper diagnostics and variable capturing.
7669 Expr *VDInitRefExpr = nullptr;
7670 // For arrays generate initializer for single element and replace it by the
7671 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007672 if (Type->isArrayType()) {
7673 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007674 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007675 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007676 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007677 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007678 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007679 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007680 InitializedEntity Entity =
7681 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007682 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7683
7684 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7685 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7686 if (Result.isInvalid())
7687 VDPrivate->setInvalidDecl();
7688 else
7689 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007690 // Remove temp variable declaration.
7691 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007692 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007693 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7694 ".firstprivate.temp");
7695 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7696 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007697 AddInitializerToDecl(VDPrivate,
7698 DefaultLvalueConversion(VDInitRefExpr).get(),
7699 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007700 }
7701 if (VDPrivate->isInvalidDecl()) {
7702 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007703 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007704 diag::note_omp_task_predetermined_firstprivate_here);
7705 }
7706 continue;
7707 }
7708 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007709 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007710 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7711 RefExpr->getExprLoc());
7712 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007713 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007714 if (TopDVar.CKind == OMPC_lastprivate)
7715 Ref = TopDVar.PrivateCopy;
7716 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007717 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007718 if (!IsOpenMPCapturedDecl(D))
7719 ExprCaptures.push_back(Ref->getDecl());
7720 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007721 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007722 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7723 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007724 PrivateCopies.push_back(VDPrivateRefExpr);
7725 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007726 }
7727
Alexey Bataeved09d242014-05-28 05:53:51 +00007728 if (Vars.empty())
7729 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007730
7731 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007732 Vars, PrivateCopies, Inits,
7733 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007734}
7735
Alexander Musman1bb328c2014-06-04 13:06:39 +00007736OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7737 SourceLocation StartLoc,
7738 SourceLocation LParenLoc,
7739 SourceLocation EndLoc) {
7740 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007741 SmallVector<Expr *, 8> SrcExprs;
7742 SmallVector<Expr *, 8> DstExprs;
7743 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007744 SmallVector<Decl *, 4> ExprCaptures;
7745 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007746 for (auto &RefExpr : VarList) {
7747 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007748 SourceLocation ELoc;
7749 SourceRange ERange;
7750 Expr *SimpleRefExpr = RefExpr;
7751 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007752 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007753 // It will be analyzed later.
7754 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007755 SrcExprs.push_back(nullptr);
7756 DstExprs.push_back(nullptr);
7757 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007758 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007759 ValueDecl *D = Res.first;
7760 if (!D)
7761 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007762
Alexey Bataev74caaf22016-02-20 04:09:36 +00007763 QualType Type = D->getType();
7764 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007765
7766 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7767 // A variable that appears in a lastprivate clause must not have an
7768 // incomplete type or a reference type.
7769 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007770 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007771 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007772 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007773
7774 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7775 // in a Construct]
7776 // Variables with the predetermined data-sharing attributes may not be
7777 // listed in data-sharing attributes clauses, except for the cases
7778 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007779 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007780 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7781 DVar.CKind != OMPC_firstprivate &&
7782 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7783 Diag(ELoc, diag::err_omp_wrong_dsa)
7784 << getOpenMPClauseName(DVar.CKind)
7785 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007786 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007787 continue;
7788 }
7789
Alexey Bataevf29276e2014-06-18 04:14:57 +00007790 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7791 // OpenMP [2.14.3.5, Restrictions, p.2]
7792 // A list item that is private within a parallel region, or that appears in
7793 // the reduction clause of a parallel construct, must not appear in a
7794 // lastprivate clause on a worksharing construct if any of the corresponding
7795 // worksharing regions ever binds to any of the corresponding parallel
7796 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007797 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007798 if (isOpenMPWorksharingDirective(CurrDir) &&
7799 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007800 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007801 if (DVar.CKind != OMPC_shared) {
7802 Diag(ELoc, diag::err_omp_required_access)
7803 << getOpenMPClauseName(OMPC_lastprivate)
7804 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007805 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007806 continue;
7807 }
7808 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007809
7810 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7811 // A list item may appear in a firstprivate or lastprivate clause but not
7812 // both.
7813 if (CurrDir == OMPD_distribute) {
7814 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7815 if (DVar.CKind == OMPC_firstprivate) {
7816 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7817 ReportOriginalDSA(*this, DSAStack, D, DVar);
7818 continue;
7819 }
7820 }
7821
Alexander Musman1bb328c2014-06-04 13:06:39 +00007822 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007823 // A variable of class type (or array thereof) that appears in a
7824 // lastprivate clause requires an accessible, unambiguous default
7825 // constructor for the class type, unless the list item is also specified
7826 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007827 // A variable of class type (or array thereof) that appears in a
7828 // lastprivate clause requires an accessible, unambiguous copy assignment
7829 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007830 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007831 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007832 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007833 D->hasAttrs() ? &D->getAttrs() : nullptr);
7834 auto *PseudoSrcExpr =
7835 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007836 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007837 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007838 D->hasAttrs() ? &D->getAttrs() : nullptr);
7839 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007840 // For arrays generate assignment operation for single element and replace
7841 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007842 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007843 PseudoDstExpr, PseudoSrcExpr);
7844 if (AssignmentOp.isInvalid())
7845 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007846 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007847 /*DiscardedValue=*/true);
7848 if (AssignmentOp.isInvalid())
7849 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007850
Alexey Bataev74caaf22016-02-20 04:09:36 +00007851 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007852 if (!VD) {
7853 if (TopDVar.CKind == OMPC_firstprivate)
7854 Ref = TopDVar.PrivateCopy;
7855 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007856 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007857 if (!IsOpenMPCapturedDecl(D))
7858 ExprCaptures.push_back(Ref->getDecl());
7859 }
7860 if (TopDVar.CKind == OMPC_firstprivate ||
7861 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00007862 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007863 ExprResult RefRes = DefaultLvalueConversion(Ref);
7864 if (!RefRes.isUsable())
7865 continue;
7866 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007867 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
7868 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007869 if (!PostUpdateRes.isUsable())
7870 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00007871 ExprPostUpdates.push_back(
7872 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007873 }
7874 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00007875 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00007876 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
7877 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00007878 SrcExprs.push_back(PseudoSrcExpr);
7879 DstExprs.push_back(PseudoDstExpr);
7880 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007881 }
7882
7883 if (Vars.empty())
7884 return nullptr;
7885
7886 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00007887 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007888 buildPreInits(Context, ExprCaptures),
7889 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00007890}
7891
Alexey Bataev758e55e2013-09-06 18:03:48 +00007892OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7893 SourceLocation StartLoc,
7894 SourceLocation LParenLoc,
7895 SourceLocation EndLoc) {
7896 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007897 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007898 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007899 SourceLocation ELoc;
7900 SourceRange ERange;
7901 Expr *SimpleRefExpr = RefExpr;
7902 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007903 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007904 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007905 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007906 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007907 ValueDecl *D = Res.first;
7908 if (!D)
7909 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007910
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007911 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007912 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7913 // in a Construct]
7914 // Variables with the predetermined data-sharing attributes may not be
7915 // listed in data-sharing attributes clauses, except for the cases
7916 // listed below. For these exceptions only, listing a predetermined
7917 // variable in a data-sharing attribute clause is allowed and overrides
7918 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007919 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007920 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7921 DVar.RefExpr) {
7922 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7923 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007924 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007925 continue;
7926 }
7927
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007928 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00007929 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00007930 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007931 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00007932 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007933 }
7934
Alexey Bataeved09d242014-05-28 05:53:51 +00007935 if (Vars.empty())
7936 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007937
7938 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7939}
7940
Alexey Bataevc5e02582014-06-16 07:08:35 +00007941namespace {
7942class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7943 DSAStackTy *Stack;
7944
7945public:
7946 bool VisitDeclRefExpr(DeclRefExpr *E) {
7947 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007948 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007949 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7950 return false;
7951 if (DVar.CKind != OMPC_unknown)
7952 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007953 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007954 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007955 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007956 return true;
7957 return false;
7958 }
7959 return false;
7960 }
7961 bool VisitStmt(Stmt *S) {
7962 for (auto Child : S->children()) {
7963 if (Child && Visit(Child))
7964 return true;
7965 }
7966 return false;
7967 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007968 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007969};
Alexey Bataev23b69422014-06-18 07:08:49 +00007970} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007971
Alexey Bataev60da77e2016-02-29 05:54:20 +00007972namespace {
7973// Transform MemberExpression for specified FieldDecl of current class to
7974// DeclRefExpr to specified OMPCapturedExprDecl.
7975class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
7976 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
7977 ValueDecl *Field;
7978 DeclRefExpr *CapturedExpr;
7979
7980public:
7981 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
7982 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
7983
7984 ExprResult TransformMemberExpr(MemberExpr *E) {
7985 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
7986 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00007987 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00007988 return CapturedExpr;
7989 }
7990 return BaseTransform::TransformMemberExpr(E);
7991 }
7992 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
7993};
7994} // namespace
7995
Alexey Bataeva839ddd2016-03-17 10:19:46 +00007996template <typename T>
7997static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
7998 const llvm::function_ref<T(ValueDecl *)> &Gen) {
7999 for (auto &Set : Lookups) {
8000 for (auto *D : Set) {
8001 if (auto Res = Gen(cast<ValueDecl>(D)))
8002 return Res;
8003 }
8004 }
8005 return T();
8006}
8007
8008static ExprResult
8009buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8010 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8011 const DeclarationNameInfo &ReductionId, QualType Ty,
8012 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8013 if (ReductionIdScopeSpec.isInvalid())
8014 return ExprError();
8015 SmallVector<UnresolvedSet<8>, 4> Lookups;
8016 if (S) {
8017 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8018 Lookup.suppressDiagnostics();
8019 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8020 auto *D = Lookup.getRepresentativeDecl();
8021 do {
8022 S = S->getParent();
8023 } while (S && !S->isDeclScope(D));
8024 if (S)
8025 S = S->getParent();
8026 Lookups.push_back(UnresolvedSet<8>());
8027 Lookups.back().append(Lookup.begin(), Lookup.end());
8028 Lookup.clear();
8029 }
8030 } else if (auto *ULE =
8031 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8032 Lookups.push_back(UnresolvedSet<8>());
8033 Decl *PrevD = nullptr;
8034 for(auto *D : ULE->decls()) {
8035 if (D == PrevD)
8036 Lookups.push_back(UnresolvedSet<8>());
8037 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8038 Lookups.back().addDecl(DRD);
8039 PrevD = D;
8040 }
8041 }
8042 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8043 Ty->containsUnexpandedParameterPack() ||
8044 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8045 return !D->isInvalidDecl() &&
8046 (D->getType()->isDependentType() ||
8047 D->getType()->isInstantiationDependentType() ||
8048 D->getType()->containsUnexpandedParameterPack());
8049 })) {
8050 UnresolvedSet<8> ResSet;
8051 for (auto &Set : Lookups) {
8052 ResSet.append(Set.begin(), Set.end());
8053 // The last item marks the end of all declarations at the specified scope.
8054 ResSet.addDecl(Set[Set.size() - 1]);
8055 }
8056 return UnresolvedLookupExpr::Create(
8057 SemaRef.Context, /*NamingClass=*/nullptr,
8058 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8059 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8060 }
8061 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8062 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8063 if (!D->isInvalidDecl() &&
8064 SemaRef.Context.hasSameType(D->getType(), Ty))
8065 return D;
8066 return nullptr;
8067 }))
8068 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8069 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8070 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8071 if (!D->isInvalidDecl() &&
8072 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8073 !Ty.isMoreQualifiedThan(D->getType()))
8074 return D;
8075 return nullptr;
8076 })) {
8077 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8078 /*DetectVirtual=*/false);
8079 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8080 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8081 VD->getType().getUnqualifiedType()))) {
8082 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8083 /*DiagID=*/0) !=
8084 Sema::AR_inaccessible) {
8085 SemaRef.BuildBasePathArray(Paths, BasePath);
8086 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8087 }
8088 }
8089 }
8090 }
8091 if (ReductionIdScopeSpec.isSet()) {
8092 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8093 return ExprError();
8094 }
8095 return ExprEmpty();
8096}
8097
Alexey Bataevc5e02582014-06-16 07:08:35 +00008098OMPClause *Sema::ActOnOpenMPReductionClause(
8099 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8100 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008101 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8102 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008103 auto DN = ReductionId.getName();
8104 auto OOK = DN.getCXXOverloadedOperator();
8105 BinaryOperatorKind BOK = BO_Comma;
8106
8107 // OpenMP [2.14.3.6, reduction clause]
8108 // C
8109 // reduction-identifier is either an identifier or one of the following
8110 // operators: +, -, *, &, |, ^, && and ||
8111 // C++
8112 // reduction-identifier is either an id-expression or one of the following
8113 // operators: +, -, *, &, |, ^, && and ||
8114 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8115 switch (OOK) {
8116 case OO_Plus:
8117 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008118 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008119 break;
8120 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008121 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008122 break;
8123 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008124 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008125 break;
8126 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008127 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008128 break;
8129 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008130 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008131 break;
8132 case OO_AmpAmp:
8133 BOK = BO_LAnd;
8134 break;
8135 case OO_PipePipe:
8136 BOK = BO_LOr;
8137 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008138 case OO_New:
8139 case OO_Delete:
8140 case OO_Array_New:
8141 case OO_Array_Delete:
8142 case OO_Slash:
8143 case OO_Percent:
8144 case OO_Tilde:
8145 case OO_Exclaim:
8146 case OO_Equal:
8147 case OO_Less:
8148 case OO_Greater:
8149 case OO_LessEqual:
8150 case OO_GreaterEqual:
8151 case OO_PlusEqual:
8152 case OO_MinusEqual:
8153 case OO_StarEqual:
8154 case OO_SlashEqual:
8155 case OO_PercentEqual:
8156 case OO_CaretEqual:
8157 case OO_AmpEqual:
8158 case OO_PipeEqual:
8159 case OO_LessLess:
8160 case OO_GreaterGreater:
8161 case OO_LessLessEqual:
8162 case OO_GreaterGreaterEqual:
8163 case OO_EqualEqual:
8164 case OO_ExclaimEqual:
8165 case OO_PlusPlus:
8166 case OO_MinusMinus:
8167 case OO_Comma:
8168 case OO_ArrowStar:
8169 case OO_Arrow:
8170 case OO_Call:
8171 case OO_Subscript:
8172 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008173 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008174 case NUM_OVERLOADED_OPERATORS:
8175 llvm_unreachable("Unexpected reduction identifier");
8176 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008177 if (auto II = DN.getAsIdentifierInfo()) {
8178 if (II->isStr("max"))
8179 BOK = BO_GT;
8180 else if (II->isStr("min"))
8181 BOK = BO_LT;
8182 }
8183 break;
8184 }
8185 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008186 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008187 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008188 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008189
8190 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008191 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008192 SmallVector<Expr *, 8> LHSs;
8193 SmallVector<Expr *, 8> RHSs;
8194 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008195 SmallVector<Decl *, 4> ExprCaptures;
8196 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008197 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8198 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008199 for (auto RefExpr : VarList) {
8200 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008201 // OpenMP [2.1, C/C++]
8202 // A list item is a variable or array section, subject to the restrictions
8203 // specified in Section 2.4 on page 42 and in each of the sections
8204 // describing clauses and directives for which a list appears.
8205 // OpenMP [2.14.3.3, Restrictions, p.1]
8206 // A variable that is part of another variable (as an array or
8207 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008208 if (!FirstIter && IR != ER)
8209 ++IR;
8210 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008211 SourceLocation ELoc;
8212 SourceRange ERange;
8213 Expr *SimpleRefExpr = RefExpr;
8214 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8215 /*AllowArraySection=*/true);
8216 if (Res.second) {
8217 // It will be analyzed later.
8218 Vars.push_back(RefExpr);
8219 Privates.push_back(nullptr);
8220 LHSs.push_back(nullptr);
8221 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008222 // Try to find 'declare reduction' corresponding construct before using
8223 // builtin/overloaded operators.
8224 QualType Type = Context.DependentTy;
8225 CXXCastPath BasePath;
8226 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8227 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8228 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8229 if (CurContext->isDependentContext() &&
8230 (DeclareReductionRef.isUnset() ||
8231 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8232 ReductionOps.push_back(DeclareReductionRef.get());
8233 else
8234 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008235 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008236 ValueDecl *D = Res.first;
8237 if (!D)
8238 continue;
8239
Alexey Bataeva1764212015-09-30 09:22:36 +00008240 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008241 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8242 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8243 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008244 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008245 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008246 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8247 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8248 Type = ATy->getElementType();
8249 else
8250 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008251 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008252 } else
8253 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8254 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008255
Alexey Bataevc5e02582014-06-16 07:08:35 +00008256 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8257 // A variable that appears in a private clause must not have an incomplete
8258 // type or a reference type.
8259 if (RequireCompleteType(ELoc, Type,
8260 diag::err_omp_reduction_incomplete_type))
8261 continue;
8262 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008263 // A list item that appears in a reduction clause must not be
8264 // const-qualified.
8265 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008266 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008267 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008268 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008269 bool IsDecl = !VD ||
8270 VD->isThisDeclarationADefinition(Context) ==
8271 VarDecl::DeclarationOnly;
8272 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008273 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008274 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008275 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008276 continue;
8277 }
8278 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8279 // If a list-item is a reference type then it must bind to the same object
8280 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008281 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008282 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008283 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008284 DSARefChecker Check(DSAStack);
8285 if (Check.Visit(VDDef->getInit())) {
8286 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8287 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8288 continue;
8289 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008290 }
8291 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008292
Alexey Bataevc5e02582014-06-16 07:08:35 +00008293 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8294 // in a Construct]
8295 // Variables with the predetermined data-sharing attributes may not be
8296 // listed in data-sharing attributes clauses, except for the cases
8297 // listed below. For these exceptions only, listing a predetermined
8298 // variable in a data-sharing attribute clause is allowed and overrides
8299 // the variable's predetermined data-sharing attributes.
8300 // OpenMP [2.14.3.6, Restrictions, p.3]
8301 // Any number of reduction clauses can be specified on the directive,
8302 // but a list item can appear only once in the reduction clauses for that
8303 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008304 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008305 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008306 if (DVar.CKind == OMPC_reduction) {
8307 Diag(ELoc, diag::err_omp_once_referenced)
8308 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008309 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008310 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008311 } else if (DVar.CKind != OMPC_unknown) {
8312 Diag(ELoc, diag::err_omp_wrong_dsa)
8313 << getOpenMPClauseName(DVar.CKind)
8314 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008315 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008316 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008317 }
8318
8319 // OpenMP [2.14.3.6, Restrictions, p.1]
8320 // A list item that appears in a reduction clause of a worksharing
8321 // construct must be shared in the parallel regions to which any of the
8322 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008323 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8324 if (isOpenMPWorksharingDirective(CurrDir) &&
8325 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008326 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008327 if (DVar.CKind != OMPC_shared) {
8328 Diag(ELoc, diag::err_omp_required_access)
8329 << getOpenMPClauseName(OMPC_reduction)
8330 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008331 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008332 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008333 }
8334 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008335
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008336 // Try to find 'declare reduction' corresponding construct before using
8337 // builtin/overloaded operators.
8338 CXXCastPath BasePath;
8339 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8340 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8341 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8342 if (DeclareReductionRef.isInvalid())
8343 continue;
8344 if (CurContext->isDependentContext() &&
8345 (DeclareReductionRef.isUnset() ||
8346 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8347 Vars.push_back(RefExpr);
8348 Privates.push_back(nullptr);
8349 LHSs.push_back(nullptr);
8350 RHSs.push_back(nullptr);
8351 ReductionOps.push_back(DeclareReductionRef.get());
8352 continue;
8353 }
8354 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8355 // Not allowed reduction identifier is found.
8356 Diag(ReductionId.getLocStart(),
8357 diag::err_omp_unknown_reduction_identifier)
8358 << Type << ReductionIdRange;
8359 continue;
8360 }
8361
8362 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8363 // The type of a list item that appears in a reduction clause must be valid
8364 // for the reduction-identifier. For a max or min reduction in C, the type
8365 // of the list item must be an allowed arithmetic data type: char, int,
8366 // float, double, or _Bool, possibly modified with long, short, signed, or
8367 // unsigned. For a max or min reduction in C++, the type of the list item
8368 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8369 // double, or bool, possibly modified with long, short, signed, or unsigned.
8370 if (DeclareReductionRef.isUnset()) {
8371 if ((BOK == BO_GT || BOK == BO_LT) &&
8372 !(Type->isScalarType() ||
8373 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8374 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8375 << getLangOpts().CPlusPlus;
8376 if (!ASE && !OASE) {
8377 bool IsDecl = !VD ||
8378 VD->isThisDeclarationADefinition(Context) ==
8379 VarDecl::DeclarationOnly;
8380 Diag(D->getLocation(),
8381 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8382 << D;
8383 }
8384 continue;
8385 }
8386 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8387 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8388 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8389 if (!ASE && !OASE) {
8390 bool IsDecl = !VD ||
8391 VD->isThisDeclarationADefinition(Context) ==
8392 VarDecl::DeclarationOnly;
8393 Diag(D->getLocation(),
8394 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8395 << D;
8396 }
8397 continue;
8398 }
8399 }
8400
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008401 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008402 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008403 D->hasAttrs() ? &D->getAttrs() : nullptr);
8404 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8405 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008406 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008407 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008408 (!ASE &&
8409 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008410 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008411 // Create pseudo array type for private copy. The size for this array will
8412 // be generated during codegen.
8413 // For array subscripts or single variables Private Ty is the same as Type
8414 // (type of the variable or single array element).
8415 PrivateTy = Context.getVariableArrayType(
8416 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8417 Context.getSizeType(), VK_RValue),
8418 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008419 } else if (!ASE && !OASE &&
8420 Context.getAsArrayType(D->getType().getNonReferenceType()))
8421 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008422 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008423 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8424 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008425 // Add initializer for private variable.
8426 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008427 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8428 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8429 if (DeclareReductionRef.isUsable()) {
8430 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8431 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8432 if (DRD->getInitializer()) {
8433 Init = DRDRef;
8434 RHSVD->setInit(DRDRef);
8435 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008436 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008437 } else {
8438 switch (BOK) {
8439 case BO_Add:
8440 case BO_Xor:
8441 case BO_Or:
8442 case BO_LOr:
8443 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8444 if (Type->isScalarType() || Type->isAnyComplexType())
8445 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8446 break;
8447 case BO_Mul:
8448 case BO_LAnd:
8449 if (Type->isScalarType() || Type->isAnyComplexType()) {
8450 // '*' and '&&' reduction ops - initializer is '1'.
8451 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008452 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008453 break;
8454 case BO_And: {
8455 // '&' reduction op - initializer is '~0'.
8456 QualType OrigType = Type;
8457 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8458 Type = ComplexTy->getElementType();
8459 if (Type->isRealFloatingType()) {
8460 llvm::APFloat InitValue =
8461 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8462 /*isIEEE=*/true);
8463 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8464 Type, ELoc);
8465 } else if (Type->isScalarType()) {
8466 auto Size = Context.getTypeSize(Type);
8467 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8468 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8469 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8470 }
8471 if (Init && OrigType->isAnyComplexType()) {
8472 // Init = 0xFFFF + 0xFFFFi;
8473 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8474 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8475 }
8476 Type = OrigType;
8477 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008478 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008479 case BO_LT:
8480 case BO_GT: {
8481 // 'min' reduction op - initializer is 'Largest representable number in
8482 // the reduction list item type'.
8483 // 'max' reduction op - initializer is 'Least representable number in
8484 // the reduction list item type'.
8485 if (Type->isIntegerType() || Type->isPointerType()) {
8486 bool IsSigned = Type->hasSignedIntegerRepresentation();
8487 auto Size = Context.getTypeSize(Type);
8488 QualType IntTy =
8489 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8490 llvm::APInt InitValue =
8491 (BOK != BO_LT)
8492 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8493 : llvm::APInt::getMinValue(Size)
8494 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8495 : llvm::APInt::getMaxValue(Size);
8496 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8497 if (Type->isPointerType()) {
8498 // Cast to pointer type.
8499 auto CastExpr = BuildCStyleCastExpr(
8500 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8501 SourceLocation(), Init);
8502 if (CastExpr.isInvalid())
8503 continue;
8504 Init = CastExpr.get();
8505 }
8506 } else if (Type->isRealFloatingType()) {
8507 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8508 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8509 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8510 Type, ELoc);
8511 }
8512 break;
8513 }
8514 case BO_PtrMemD:
8515 case BO_PtrMemI:
8516 case BO_MulAssign:
8517 case BO_Div:
8518 case BO_Rem:
8519 case BO_Sub:
8520 case BO_Shl:
8521 case BO_Shr:
8522 case BO_LE:
8523 case BO_GE:
8524 case BO_EQ:
8525 case BO_NE:
8526 case BO_AndAssign:
8527 case BO_XorAssign:
8528 case BO_OrAssign:
8529 case BO_Assign:
8530 case BO_AddAssign:
8531 case BO_SubAssign:
8532 case BO_DivAssign:
8533 case BO_RemAssign:
8534 case BO_ShlAssign:
8535 case BO_ShrAssign:
8536 case BO_Comma:
8537 llvm_unreachable("Unexpected reduction operation");
8538 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008539 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008540 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008541 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8542 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008543 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008544 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008545 if (RHSVD->isInvalidDecl())
8546 continue;
8547 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008548 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8549 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008550 bool IsDecl =
8551 !VD ||
8552 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8553 Diag(D->getLocation(),
8554 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8555 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008556 continue;
8557 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008558 // Store initializer for single element in private copy. Will be used during
8559 // codegen.
8560 PrivateVD->setInit(RHSVD->getInit());
8561 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008562 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008563 ExprResult ReductionOp;
8564 if (DeclareReductionRef.isUsable()) {
8565 QualType RedTy = DeclareReductionRef.get()->getType();
8566 QualType PtrRedTy = Context.getPointerType(RedTy);
8567 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8568 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8569 if (!BasePath.empty()) {
8570 LHS = DefaultLvalueConversion(LHS.get());
8571 RHS = DefaultLvalueConversion(RHS.get());
8572 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8573 CK_UncheckedDerivedToBase, LHS.get(),
8574 &BasePath, LHS.get()->getValueKind());
8575 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8576 CK_UncheckedDerivedToBase, RHS.get(),
8577 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008578 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008579 FunctionProtoType::ExtProtoInfo EPI;
8580 QualType Params[] = {PtrRedTy, PtrRedTy};
8581 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8582 auto *OVE = new (Context) OpaqueValueExpr(
8583 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8584 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8585 Expr *Args[] = {LHS.get(), RHS.get()};
8586 ReductionOp = new (Context)
8587 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8588 } else {
8589 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8590 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8591 if (ReductionOp.isUsable()) {
8592 if (BOK != BO_LT && BOK != BO_GT) {
8593 ReductionOp =
8594 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8595 BO_Assign, LHSDRE, ReductionOp.get());
8596 } else {
8597 auto *ConditionalOp = new (Context) ConditionalOperator(
8598 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8599 RHSDRE, Type, VK_LValue, OK_Ordinary);
8600 ReductionOp =
8601 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8602 BO_Assign, LHSDRE, ConditionalOp);
8603 }
8604 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8605 }
8606 if (ReductionOp.isInvalid())
8607 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008608 }
8609
Alexey Bataev60da77e2016-02-29 05:54:20 +00008610 DeclRefExpr *Ref = nullptr;
8611 Expr *VarsExpr = RefExpr->IgnoreParens();
8612 if (!VD) {
8613 if (ASE || OASE) {
8614 TransformExprToCaptures RebuildToCapture(*this, D);
8615 VarsExpr =
8616 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8617 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008618 } else {
8619 VarsExpr = Ref =
8620 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008621 }
8622 if (!IsOpenMPCapturedDecl(D)) {
8623 ExprCaptures.push_back(Ref->getDecl());
8624 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8625 ExprResult RefRes = DefaultLvalueConversion(Ref);
8626 if (!RefRes.isUsable())
8627 continue;
8628 ExprResult PostUpdateRes =
8629 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8630 SimpleRefExpr, RefRes.get());
8631 if (!PostUpdateRes.isUsable())
8632 continue;
8633 ExprPostUpdates.push_back(
8634 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008635 }
8636 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008637 }
8638 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8639 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008640 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008641 LHSs.push_back(LHSDRE);
8642 RHSs.push_back(RHSDRE);
8643 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008644 }
8645
8646 if (Vars.empty())
8647 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008648
Alexey Bataevc5e02582014-06-16 07:08:35 +00008649 return OMPReductionClause::Create(
8650 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008651 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008652 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8653 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008654}
8655
Alexey Bataev182227b2015-08-20 10:54:39 +00008656OMPClause *Sema::ActOnOpenMPLinearClause(
8657 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8658 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8659 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008660 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008661 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008662 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008663 SmallVector<Decl *, 4> ExprCaptures;
8664 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataev182227b2015-08-20 10:54:39 +00008665 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8666 LinKind == OMPC_LINEAR_unknown) {
8667 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8668 LinKind = OMPC_LINEAR_val;
8669 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008670 for (auto &RefExpr : VarList) {
8671 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008672 SourceLocation ELoc;
8673 SourceRange ERange;
8674 Expr *SimpleRefExpr = RefExpr;
8675 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8676 /*AllowArraySection=*/false);
8677 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008678 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008679 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008680 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008681 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008682 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008683 ValueDecl *D = Res.first;
8684 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008685 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008686
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008687 QualType Type = D->getType();
8688 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008689
8690 // OpenMP [2.14.3.7, linear clause]
8691 // A list-item cannot appear in more than one linear clause.
8692 // A list-item that appears in a linear clause cannot appear in any
8693 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008694 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008695 if (DVar.RefExpr) {
8696 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8697 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008698 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008699 continue;
8700 }
8701
8702 // A variable must not have an incomplete type or a reference type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008703 if (RequireCompleteType(ELoc, Type,
8704 diag::err_omp_linear_incomplete_type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008705 continue;
Alexey Bataev1185e192015-08-20 12:15:57 +00008706 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008707 !Type->isReferenceType()) {
Alexey Bataev1185e192015-08-20 12:15:57 +00008708 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008709 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
Alexey Bataev1185e192015-08-20 12:15:57 +00008710 continue;
8711 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008712 Type = Type.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008713
8714 // A list item must not be const-qualified.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008715 if (Type.isConstant(Context)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008716 Diag(ELoc, diag::err_omp_const_variable)
8717 << getOpenMPClauseName(OMPC_linear);
8718 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008719 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008720 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008721 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008722 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008723 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008724 continue;
8725 }
8726
8727 // A list item must be of integral or pointer type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008728 Type = Type.getUnqualifiedType().getCanonicalType();
8729 const auto *Ty = Type.getTypePtrOrNull();
Alexander Musman8dba6642014-04-22 13:09:42 +00008730 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8731 !Ty->isPointerType())) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008732 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
Alexander Musman8dba6642014-04-22 13:09:42 +00008733 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008734 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008735 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008736 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008737 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008738 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008739 continue;
8740 }
8741
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008742 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008743 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8744 D->hasAttrs() ? &D->getAttrs() : nullptr);
8745 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008746 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008747 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008748 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008749 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008750 if (!VD) {
8751 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8752 if (!IsOpenMPCapturedDecl(D)) {
8753 ExprCaptures.push_back(Ref->getDecl());
8754 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8755 ExprResult RefRes = DefaultLvalueConversion(Ref);
8756 if (!RefRes.isUsable())
8757 continue;
8758 ExprResult PostUpdateRes =
8759 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8760 SimpleRefExpr, RefRes.get());
8761 if (!PostUpdateRes.isUsable())
8762 continue;
8763 ExprPostUpdates.push_back(
8764 IgnoredValueConversions(PostUpdateRes.get()).get());
8765 }
8766 }
8767 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008768 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008769 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008770 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008771 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008772 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008773 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8774 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8775
8776 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
8777 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008778 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008779 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008780 }
8781
8782 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008783 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008784
8785 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008786 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008787 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8788 !Step->isInstantiationDependent() &&
8789 !Step->containsUnexpandedParameterPack()) {
8790 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008791 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008792 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008793 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008794 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008795
Alexander Musman3276a272015-03-21 10:12:56 +00008796 // Build var to save the step value.
8797 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008798 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008799 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008800 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008801 ExprResult CalcStep =
8802 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008803 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008804
Alexander Musman8dba6642014-04-22 13:09:42 +00008805 // Warn about zero linear step (it would be probably better specified as
8806 // making corresponding variables 'const').
8807 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008808 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8809 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008810 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8811 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008812 if (!IsConstant && CalcStep.isUsable()) {
8813 // Calculate the step beforehand instead of doing this on each iteration.
8814 // (This is not used if the number of iterations may be kfold-ed).
8815 CalcStepExpr = CalcStep.get();
8816 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008817 }
8818
Alexey Bataev182227b2015-08-20 10:54:39 +00008819 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8820 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008821 StepExpr, CalcStepExpr,
8822 buildPreInits(Context, ExprCaptures),
8823 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00008824}
8825
Alexey Bataev5a3af132016-03-29 08:58:54 +00008826static bool
8827FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8828 Expr *NumIterations, Sema &SemaRef, Scope *S) {
Alexander Musman3276a272015-03-21 10:12:56 +00008829 // Walk the vars and build update/final expressions for the CodeGen.
8830 SmallVector<Expr *, 8> Updates;
8831 SmallVector<Expr *, 8> Finals;
8832 Expr *Step = Clause.getStep();
8833 Expr *CalcStep = Clause.getCalcStep();
8834 // OpenMP [2.14.3.7, linear clause]
8835 // If linear-step is not specified it is assumed to be 1.
8836 if (Step == nullptr)
8837 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008838 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00008839 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008840 }
Alexander Musman3276a272015-03-21 10:12:56 +00008841 bool HasErrors = false;
8842 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008843 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008844 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008845 for (auto &RefExpr : Clause.varlists()) {
8846 Expr *InitExpr = *CurInit;
8847
8848 // Build privatized reference to the current linear var.
8849 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008850 Expr *CapturedRef;
8851 if (LinKind == OMPC_LINEAR_uval)
8852 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8853 else
8854 CapturedRef =
8855 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8856 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8857 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008858
8859 // Build update: Var = InitExpr + IV * Step
8860 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008861 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008862 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008863 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8864 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008865
8866 // Build final: Var = InitExpr + NumIterations * Step
8867 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008868 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008869 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008870 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8871 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008872 if (!Update.isUsable() || !Final.isUsable()) {
8873 Updates.push_back(nullptr);
8874 Finals.push_back(nullptr);
8875 HasErrors = true;
8876 } else {
8877 Updates.push_back(Update.get());
8878 Finals.push_back(Final.get());
8879 }
Richard Trieucc3949d2016-02-18 22:34:54 +00008880 ++CurInit;
8881 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008882 }
8883 Clause.setUpdates(Updates);
8884 Clause.setFinals(Finals);
8885 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008886}
8887
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008888OMPClause *Sema::ActOnOpenMPAlignedClause(
8889 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8890 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8891
8892 SmallVector<Expr *, 8> Vars;
8893 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00008894 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8895 SourceLocation ELoc;
8896 SourceRange ERange;
8897 Expr *SimpleRefExpr = RefExpr;
8898 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8899 /*AllowArraySection=*/false);
8900 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008901 // It will be analyzed later.
8902 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008903 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00008904 ValueDecl *D = Res.first;
8905 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008906 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008907
Alexey Bataev1efd1662016-03-29 10:59:56 +00008908 QualType QType = D->getType();
8909 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008910
8911 // OpenMP [2.8.1, simd construct, Restrictions]
8912 // The type of list items appearing in the aligned clause must be
8913 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008914 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008915 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00008916 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008917 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00008918 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008919 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00008920 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008921 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008922 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008923 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00008924 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008925 continue;
8926 }
8927
8928 // OpenMP [2.8.1, simd construct, Restrictions]
8929 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00008930 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00008931 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008932 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8933 << getOpenMPClauseName(OMPC_aligned);
8934 continue;
8935 }
8936
Alexey Bataev1efd1662016-03-29 10:59:56 +00008937 DeclRefExpr *Ref = nullptr;
8938 if (!VD && IsOpenMPCapturedDecl(D))
8939 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8940 Vars.push_back(DefaultFunctionArrayConversion(
8941 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
8942 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008943 }
8944
8945 // OpenMP [2.8.1, simd construct, Description]
8946 // The parameter of the aligned clause, alignment, must be a constant
8947 // positive integer expression.
8948 // If no optional parameter is specified, implementation-defined default
8949 // alignments for SIMD instructions on the target platforms are assumed.
8950 if (Alignment != nullptr) {
8951 ExprResult AlignResult =
8952 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8953 if (AlignResult.isInvalid())
8954 return nullptr;
8955 Alignment = AlignResult.get();
8956 }
8957 if (Vars.empty())
8958 return nullptr;
8959
8960 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8961 EndLoc, Vars, Alignment);
8962}
8963
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008964OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8965 SourceLocation StartLoc,
8966 SourceLocation LParenLoc,
8967 SourceLocation EndLoc) {
8968 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008969 SmallVector<Expr *, 8> SrcExprs;
8970 SmallVector<Expr *, 8> DstExprs;
8971 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008972 for (auto &RefExpr : VarList) {
8973 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8974 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008975 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008976 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008977 SrcExprs.push_back(nullptr);
8978 DstExprs.push_back(nullptr);
8979 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008980 continue;
8981 }
8982
Alexey Bataeved09d242014-05-28 05:53:51 +00008983 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008984 // OpenMP [2.1, C/C++]
8985 // A list item is a variable name.
8986 // OpenMP [2.14.4.1, Restrictions, p.1]
8987 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008988 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008989 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008990 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8991 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008992 continue;
8993 }
8994
8995 Decl *D = DE->getDecl();
8996 VarDecl *VD = cast<VarDecl>(D);
8997
8998 QualType Type = VD->getType();
8999 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9000 // It will be analyzed later.
9001 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009002 SrcExprs.push_back(nullptr);
9003 DstExprs.push_back(nullptr);
9004 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009005 continue;
9006 }
9007
9008 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9009 // A list item that appears in a copyin clause must be threadprivate.
9010 if (!DSAStack->isThreadPrivate(VD)) {
9011 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009012 << getOpenMPClauseName(OMPC_copyin)
9013 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009014 continue;
9015 }
9016
9017 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9018 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009019 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009020 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009021 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009022 auto *SrcVD =
9023 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9024 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009025 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009026 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9027 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009028 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9029 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009030 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009031 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009032 // For arrays generate assignment operation for single element and replace
9033 // it by the original array element in CodeGen.
9034 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9035 PseudoDstExpr, PseudoSrcExpr);
9036 if (AssignmentOp.isInvalid())
9037 continue;
9038 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9039 /*DiscardedValue=*/true);
9040 if (AssignmentOp.isInvalid())
9041 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009042
9043 DSAStack->addDSA(VD, DE, OMPC_copyin);
9044 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009045 SrcExprs.push_back(PseudoSrcExpr);
9046 DstExprs.push_back(PseudoDstExpr);
9047 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009048 }
9049
Alexey Bataeved09d242014-05-28 05:53:51 +00009050 if (Vars.empty())
9051 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009052
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009053 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9054 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009055}
9056
Alexey Bataevbae9a792014-06-27 10:37:06 +00009057OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9058 SourceLocation StartLoc,
9059 SourceLocation LParenLoc,
9060 SourceLocation EndLoc) {
9061 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009062 SmallVector<Expr *, 8> SrcExprs;
9063 SmallVector<Expr *, 8> DstExprs;
9064 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009065 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009066 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9067 SourceLocation ELoc;
9068 SourceRange ERange;
9069 Expr *SimpleRefExpr = RefExpr;
9070 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9071 /*AllowArraySection=*/false);
9072 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009073 // It will be analyzed later.
9074 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009075 SrcExprs.push_back(nullptr);
9076 DstExprs.push_back(nullptr);
9077 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009078 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009079 ValueDecl *D = Res.first;
9080 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009081 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009082
Alexey Bataeve122da12016-03-17 10:50:17 +00009083 QualType Type = D->getType();
9084 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009085
9086 // OpenMP [2.14.4.2, Restrictions, p.2]
9087 // A list item that appears in a copyprivate clause may not appear in a
9088 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009089 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9090 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009091 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9092 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009093 Diag(ELoc, diag::err_omp_wrong_dsa)
9094 << getOpenMPClauseName(DVar.CKind)
9095 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009096 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009097 continue;
9098 }
9099
9100 // OpenMP [2.11.4.2, Restrictions, p.1]
9101 // All list items that appear in a copyprivate clause must be either
9102 // threadprivate or private in the enclosing context.
9103 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009104 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009105 if (DVar.CKind == OMPC_shared) {
9106 Diag(ELoc, diag::err_omp_required_access)
9107 << getOpenMPClauseName(OMPC_copyprivate)
9108 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009109 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009110 continue;
9111 }
9112 }
9113 }
9114
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009115 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009116 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009117 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009118 << getOpenMPClauseName(OMPC_copyprivate) << Type
9119 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009120 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009121 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009122 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009123 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009124 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009125 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009126 continue;
9127 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009128
Alexey Bataevbae9a792014-06-27 10:37:06 +00009129 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9130 // A variable of class type (or array thereof) that appears in a
9131 // copyin clause requires an accessible, unambiguous copy assignment
9132 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009133 Type = Context.getBaseElementType(Type.getNonReferenceType())
9134 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009135 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009136 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9137 D->hasAttrs() ? &D->getAttrs() : nullptr);
9138 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009139 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009140 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9141 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009142 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009143 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9144 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009145 PseudoDstExpr, PseudoSrcExpr);
9146 if (AssignmentOp.isInvalid())
9147 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009148 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009149 /*DiscardedValue=*/true);
9150 if (AssignmentOp.isInvalid())
9151 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009152
9153 // No need to mark vars as copyprivate, they are already threadprivate or
9154 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009155 assert(VD || IsOpenMPCapturedDecl(D));
9156 Vars.push_back(
9157 VD ? RefExpr->IgnoreParens()
9158 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009159 SrcExprs.push_back(PseudoSrcExpr);
9160 DstExprs.push_back(PseudoDstExpr);
9161 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009162 }
9163
9164 if (Vars.empty())
9165 return nullptr;
9166
Alexey Bataeva63048e2015-03-23 06:18:07 +00009167 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9168 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009169}
9170
Alexey Bataev6125da92014-07-21 11:26:11 +00009171OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9172 SourceLocation StartLoc,
9173 SourceLocation LParenLoc,
9174 SourceLocation EndLoc) {
9175 if (VarList.empty())
9176 return nullptr;
9177
9178 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9179}
Alexey Bataevdea47612014-07-23 07:46:59 +00009180
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009181OMPClause *
9182Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9183 SourceLocation DepLoc, SourceLocation ColonLoc,
9184 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9185 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009186 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009187 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009188 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009189 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009190 return nullptr;
9191 }
9192 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009193 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9194 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009195 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009196 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009197 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9198 /*Last=*/OMPC_DEPEND_unknown, Except)
9199 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009200 return nullptr;
9201 }
9202 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009203 llvm::APSInt DepCounter(/*BitWidth=*/32);
9204 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9205 if (DepKind == OMPC_DEPEND_sink) {
9206 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9207 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9208 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009209 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009210 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009211 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9212 DSAStack->getParentOrderedRegionParam()) {
9213 for (auto &RefExpr : VarList) {
9214 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9215 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9216 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9217 // It will be analyzed later.
9218 Vars.push_back(RefExpr);
9219 continue;
9220 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009221
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009222 SourceLocation ELoc = RefExpr->getExprLoc();
9223 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9224 if (DepKind == OMPC_DEPEND_sink) {
9225 if (DepCounter >= TotalDepCount) {
9226 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9227 continue;
9228 }
9229 ++DepCounter;
9230 // OpenMP [2.13.9, Summary]
9231 // depend(dependence-type : vec), where dependence-type is:
9232 // 'sink' and where vec is the iteration vector, which has the form:
9233 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9234 // where n is the value specified by the ordered clause in the loop
9235 // directive, xi denotes the loop iteration variable of the i-th nested
9236 // loop associated with the loop directive, and di is a constant
9237 // non-negative integer.
9238 SimpleExpr = SimpleExpr->IgnoreImplicit();
9239 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9240 if (!DE) {
9241 OverloadedOperatorKind OOK = OO_None;
9242 SourceLocation OOLoc;
9243 Expr *LHS, *RHS;
9244 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9245 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9246 OOLoc = BO->getOperatorLoc();
9247 LHS = BO->getLHS()->IgnoreParenImpCasts();
9248 RHS = BO->getRHS()->IgnoreParenImpCasts();
9249 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9250 OOK = OCE->getOperator();
9251 OOLoc = OCE->getOperatorLoc();
9252 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9253 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9254 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9255 OOK = MCE->getMethodDecl()
9256 ->getNameInfo()
9257 .getName()
9258 .getCXXOverloadedOperator();
9259 OOLoc = MCE->getCallee()->getExprLoc();
9260 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9261 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9262 } else {
9263 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9264 continue;
9265 }
9266 DE = dyn_cast<DeclRefExpr>(LHS);
9267 if (!DE) {
9268 Diag(LHS->getExprLoc(),
9269 diag::err_omp_depend_sink_expected_loop_iteration)
9270 << DSAStack->getParentLoopControlVariable(
9271 DepCounter.getZExtValue());
9272 continue;
9273 }
9274 if (OOK != OO_Plus && OOK != OO_Minus) {
9275 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9276 continue;
9277 }
9278 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9279 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9280 if (Res.isInvalid())
9281 continue;
9282 }
9283 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9284 if (!CurContext->isDependentContext() &&
9285 DSAStack->getParentOrderedRegionParam() &&
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00009286 (!VD ||
9287 DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009288 Diag(DE->getExprLoc(),
9289 diag::err_omp_depend_sink_expected_loop_iteration)
9290 << DSAStack->getParentLoopControlVariable(
9291 DepCounter.getZExtValue());
9292 continue;
9293 }
9294 } else {
9295 // OpenMP [2.11.1.1, Restrictions, p.3]
9296 // A variable that is part of another variable (such as a field of a
9297 // structure) but is not an array element or an array section cannot
9298 // appear in a depend clause.
9299 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9300 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9301 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9302 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9303 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009304 (ASE &&
9305 !ASE->getBase()
9306 ->getType()
9307 .getNonReferenceType()
9308 ->isPointerType() &&
9309 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009310 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9311 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009312 continue;
9313 }
9314 }
9315
9316 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9317 }
9318
9319 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9320 TotalDepCount > VarList.size() &&
9321 DSAStack->getParentOrderedRegionParam()) {
9322 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9323 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9324 }
9325 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9326 Vars.empty())
9327 return nullptr;
9328 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009329
9330 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9331 DepLoc, ColonLoc, Vars);
9332}
Michael Wonge710d542015-08-07 16:16:36 +00009333
9334OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9335 SourceLocation LParenLoc,
9336 SourceLocation EndLoc) {
9337 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009338
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009339 // OpenMP [2.9.1, Restrictions]
9340 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009341 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9342 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009343 return nullptr;
9344
Michael Wonge710d542015-08-07 16:16:36 +00009345 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9346}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009347
9348static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9349 DSAStackTy *Stack, CXXRecordDecl *RD) {
9350 if (!RD || RD->isInvalidDecl())
9351 return true;
9352
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009353 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9354 if (auto *CTD = CTSD->getSpecializedTemplate())
9355 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009356 auto QTy = SemaRef.Context.getRecordType(RD);
9357 if (RD->isDynamicClass()) {
9358 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9359 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9360 return false;
9361 }
9362 auto *DC = RD;
9363 bool IsCorrect = true;
9364 for (auto *I : DC->decls()) {
9365 if (I) {
9366 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9367 if (MD->isStatic()) {
9368 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9369 SemaRef.Diag(MD->getLocation(),
9370 diag::note_omp_static_member_in_target);
9371 IsCorrect = false;
9372 }
9373 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9374 if (VD->isStaticDataMember()) {
9375 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9376 SemaRef.Diag(VD->getLocation(),
9377 diag::note_omp_static_member_in_target);
9378 IsCorrect = false;
9379 }
9380 }
9381 }
9382 }
9383
9384 for (auto &I : RD->bases()) {
9385 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9386 I.getType()->getAsCXXRecordDecl()))
9387 IsCorrect = false;
9388 }
9389 return IsCorrect;
9390}
9391
9392static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9393 DSAStackTy *Stack, QualType QTy) {
9394 NamedDecl *ND;
9395 if (QTy->isIncompleteType(&ND)) {
9396 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9397 return false;
9398 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9399 if (!RD->isInvalidDecl() &&
9400 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9401 return false;
9402 }
9403 return true;
9404}
9405
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009406/// \brief Return true if it can be proven that the provided array expression
9407/// (array section or array subscript) does NOT specify the whole size of the
9408/// array whose base type is \a BaseQTy.
9409static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9410 const Expr *E,
9411 QualType BaseQTy) {
9412 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9413
9414 // If this is an array subscript, it refers to the whole size if the size of
9415 // the dimension is constant and equals 1. Also, an array section assumes the
9416 // format of an array subscript if no colon is used.
9417 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9418 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9419 return ATy->getSize().getSExtValue() != 1;
9420 // Size can't be evaluated statically.
9421 return false;
9422 }
9423
9424 assert(OASE && "Expecting array section if not an array subscript.");
9425 auto *LowerBound = OASE->getLowerBound();
9426 auto *Length = OASE->getLength();
9427
9428 // If there is a lower bound that does not evaluates to zero, we are not
9429 // convering the whole dimension.
9430 if (LowerBound) {
9431 llvm::APSInt ConstLowerBound;
9432 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9433 return false; // Can't get the integer value as a constant.
9434 if (ConstLowerBound.getSExtValue())
9435 return true;
9436 }
9437
9438 // If we don't have a length we covering the whole dimension.
9439 if (!Length)
9440 return false;
9441
9442 // If the base is a pointer, we don't have a way to get the size of the
9443 // pointee.
9444 if (BaseQTy->isPointerType())
9445 return false;
9446
9447 // We can only check if the length is the same as the size of the dimension
9448 // if we have a constant array.
9449 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9450 if (!CATy)
9451 return false;
9452
9453 llvm::APSInt ConstLength;
9454 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9455 return false; // Can't get the integer value as a constant.
9456
9457 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9458}
9459
9460// Return true if it can be proven that the provided array expression (array
9461// section or array subscript) does NOT specify a single element of the array
9462// whose base type is \a BaseQTy.
9463static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9464 const Expr *E,
9465 QualType BaseQTy) {
9466 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9467
9468 // An array subscript always refer to a single element. Also, an array section
9469 // assumes the format of an array subscript if no colon is used.
9470 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9471 return false;
9472
9473 assert(OASE && "Expecting array section if not an array subscript.");
9474 auto *Length = OASE->getLength();
9475
9476 // If we don't have a length we have to check if the array has unitary size
9477 // for this dimension. Also, we should always expect a length if the base type
9478 // is pointer.
9479 if (!Length) {
9480 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9481 return ATy->getSize().getSExtValue() != 1;
9482 // We cannot assume anything.
9483 return false;
9484 }
9485
9486 // Check if the length evaluates to 1.
9487 llvm::APSInt ConstLength;
9488 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9489 return false; // Can't get the integer value as a constant.
9490
9491 return ConstLength.getSExtValue() != 1;
9492}
9493
Samuel Antao5de996e2016-01-22 20:21:36 +00009494// Return the expression of the base of the map clause or null if it cannot
9495// be determined and do all the necessary checks to see if the expression is
9496// valid as a standalone map clause expression.
9497static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
9498 SourceLocation ELoc = E->getExprLoc();
9499 SourceRange ERange = E->getSourceRange();
9500
9501 // The base of elements of list in a map clause have to be either:
9502 // - a reference to variable or field.
9503 // - a member expression.
9504 // - an array expression.
9505 //
9506 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9507 // reference to 'r'.
9508 //
9509 // If we have:
9510 //
9511 // struct SS {
9512 // Bla S;
9513 // foo() {
9514 // #pragma omp target map (S.Arr[:12]);
9515 // }
9516 // }
9517 //
9518 // We want to retrieve the member expression 'this->S';
9519
9520 Expr *RelevantExpr = nullptr;
9521
Samuel Antao5de996e2016-01-22 20:21:36 +00009522 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9523 // If a list item is an array section, it must specify contiguous storage.
9524 //
9525 // For this restriction it is sufficient that we make sure only references
9526 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009527 // exist except in the rightmost expression (unless they cover the whole
9528 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009529 //
9530 // r.ArrS[3:5].Arr[6:7]
9531 //
9532 // r.ArrS[3:5].x
9533 //
9534 // but these would be valid:
9535 // r.ArrS[3].Arr[6:7]
9536 //
9537 // r.ArrS[3].x
9538
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009539 bool AllowUnitySizeArraySection = true;
9540 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009541
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009542 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009543 E = E->IgnoreParenImpCasts();
9544
9545 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9546 if (!isa<VarDecl>(CurE->getDecl()))
9547 break;
9548
9549 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009550
9551 // If we got a reference to a declaration, we should not expect any array
9552 // section before that.
9553 AllowUnitySizeArraySection = false;
9554 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009555 continue;
9556 }
9557
9558 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9559 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9560
9561 if (isa<CXXThisExpr>(BaseE))
9562 // We found a base expression: this->Val.
9563 RelevantExpr = CurE;
9564 else
9565 E = BaseE;
9566
9567 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9568 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9569 << CurE->getSourceRange();
9570 break;
9571 }
9572
9573 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9574
9575 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9576 // A bit-field cannot appear in a map clause.
9577 //
9578 if (FD->isBitField()) {
9579 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9580 << CurE->getSourceRange();
9581 break;
9582 }
9583
9584 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9585 // If the type of a list item is a reference to a type T then the type
9586 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009587 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009588
9589 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9590 // A list item cannot be a variable that is a member of a structure with
9591 // a union type.
9592 //
9593 if (auto *RT = CurType->getAs<RecordType>())
9594 if (RT->isUnionType()) {
9595 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9596 << CurE->getSourceRange();
9597 break;
9598 }
9599
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009600 // If we got a member expression, we should not expect any array section
9601 // before that:
9602 //
9603 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9604 // If a list item is an element of a structure, only the rightmost symbol
9605 // of the variable reference can be an array section.
9606 //
9607 AllowUnitySizeArraySection = false;
9608 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009609 continue;
9610 }
9611
9612 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9613 E = CurE->getBase()->IgnoreParenImpCasts();
9614
9615 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9616 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9617 << 0 << CurE->getSourceRange();
9618 break;
9619 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009620
9621 // If we got an array subscript that express the whole dimension we
9622 // can have any array expressions before. If it only expressing part of
9623 // the dimension, we can only have unitary-size array expressions.
9624 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9625 E->getType()))
9626 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009627 continue;
9628 }
9629
9630 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009631 E = CurE->getBase()->IgnoreParenImpCasts();
9632
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009633 auto CurType =
9634 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9635
Samuel Antao5de996e2016-01-22 20:21:36 +00009636 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9637 // If the type of a list item is a reference to a type T then the type
9638 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009639 if (CurType->isReferenceType())
9640 CurType = CurType->getPointeeType();
9641
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009642 bool IsPointer = CurType->isAnyPointerType();
9643
9644 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009645 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9646 << 0 << CurE->getSourceRange();
9647 break;
9648 }
9649
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009650 bool NotWhole =
9651 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9652 bool NotUnity =
9653 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9654
9655 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9656 // Any array section is currently allowed.
9657 //
9658 // If this array section refers to the whole dimension we can still
9659 // accept other array sections before this one, except if the base is a
9660 // pointer. Otherwise, only unitary sections are accepted.
9661 if (NotWhole || IsPointer)
9662 AllowWholeSizeArraySection = false;
9663 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9664 (AllowWholeSizeArraySection && NotWhole)) {
9665 // A unity or whole array section is not allowed and that is not
9666 // compatible with the properties of the current array section.
9667 SemaRef.Diag(
9668 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9669 << CurE->getSourceRange();
9670 break;
9671 }
Samuel Antao5de996e2016-01-22 20:21:36 +00009672 continue;
9673 }
9674
9675 // If nothing else worked, this is not a valid map clause expression.
9676 SemaRef.Diag(ELoc,
9677 diag::err_omp_expected_named_var_member_or_array_expression)
9678 << ERange;
9679 break;
9680 }
9681
9682 return RelevantExpr;
9683}
9684
9685// Return true if expression E associated with value VD has conflicts with other
9686// map information.
9687static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9688 Expr *E, bool CurrentRegionOnly) {
9689 assert(VD && E);
9690
9691 // Types used to organize the components of a valid map clause.
9692 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9693 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9694
9695 // Helper to extract the components in the map clause expression E and store
9696 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9697 // it has already passed the single clause checks.
9698 auto ExtractMapExpressionComponents = [](Expr *TE,
9699 MapExpressionComponents &MEC) {
9700 while (true) {
9701 TE = TE->IgnoreParenImpCasts();
9702
9703 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9704 MEC.push_back(
9705 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9706 break;
9707 }
9708
9709 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9710 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9711
9712 MEC.push_back(MapExpressionComponent(
9713 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9714 if (isa<CXXThisExpr>(BaseE))
9715 break;
9716
9717 TE = BaseE;
9718 continue;
9719 }
9720
9721 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9722 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9723 TE = CurE->getBase()->IgnoreParenImpCasts();
9724 continue;
9725 }
9726
9727 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9728 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9729 TE = CurE->getBase()->IgnoreParenImpCasts();
9730 continue;
9731 }
9732
9733 llvm_unreachable(
9734 "Expecting only valid map clause expressions at this point!");
9735 }
9736 };
9737
9738 SourceLocation ELoc = E->getExprLoc();
9739 SourceRange ERange = E->getSourceRange();
9740
9741 // In order to easily check the conflicts we need to match each component of
9742 // the expression under test with the components of the expressions that are
9743 // already in the stack.
9744
9745 MapExpressionComponents CurComponents;
9746 ExtractMapExpressionComponents(E, CurComponents);
9747
9748 assert(!CurComponents.empty() && "Map clause expression with no components!");
9749 assert(CurComponents.back().second == VD &&
9750 "Map clause expression with unexpected base!");
9751
9752 // Variables to help detecting enclosing problems in data environment nests.
9753 bool IsEnclosedByDataEnvironmentExpr = false;
9754 Expr *EnclosingExpr = nullptr;
9755
9756 bool FoundError =
9757 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9758 MapExpressionComponents StackComponents;
9759 ExtractMapExpressionComponents(RE, StackComponents);
9760 assert(!StackComponents.empty() &&
9761 "Map clause expression with no components!");
9762 assert(StackComponents.back().second == VD &&
9763 "Map clause expression with unexpected base!");
9764
9765 // Expressions must start from the same base. Here we detect at which
9766 // point both expressions diverge from each other and see if we can
9767 // detect if the memory referred to both expressions is contiguous and
9768 // do not overlap.
9769 auto CI = CurComponents.rbegin();
9770 auto CE = CurComponents.rend();
9771 auto SI = StackComponents.rbegin();
9772 auto SE = StackComponents.rend();
9773 for (; CI != CE && SI != SE; ++CI, ++SI) {
9774
9775 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9776 // At most one list item can be an array item derived from a given
9777 // variable in map clauses of the same construct.
9778 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9779 isa<OMPArraySectionExpr>(CI->first)) &&
9780 (isa<ArraySubscriptExpr>(SI->first) ||
9781 isa<OMPArraySectionExpr>(SI->first))) {
9782 SemaRef.Diag(CI->first->getExprLoc(),
9783 diag::err_omp_multiple_array_items_in_map_clause)
9784 << CI->first->getSourceRange();
9785 ;
9786 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9787 << SI->first->getSourceRange();
9788 return true;
9789 }
9790
9791 // Do both expressions have the same kind?
9792 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9793 break;
9794
9795 // Are we dealing with different variables/fields?
9796 if (CI->second != SI->second)
9797 break;
9798 }
9799
9800 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9801 // List items of map clauses in the same construct must not share
9802 // original storage.
9803 //
9804 // If the expressions are exactly the same or one is a subset of the
9805 // other, it means they are sharing storage.
9806 if (CI == CE && SI == SE) {
9807 if (CurrentRegionOnly) {
9808 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9809 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9810 << RE->getSourceRange();
9811 return true;
9812 } else {
9813 // If we find the same expression in the enclosing data environment,
9814 // that is legal.
9815 IsEnclosedByDataEnvironmentExpr = true;
9816 return false;
9817 }
9818 }
9819
9820 QualType DerivedType = std::prev(CI)->first->getType();
9821 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9822
9823 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9824 // If the type of a list item is a reference to a type T then the type
9825 // will be considered to be T for all purposes of this clause.
9826 if (DerivedType->isReferenceType())
9827 DerivedType = DerivedType->getPointeeType();
9828
9829 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9830 // A variable for which the type is pointer and an array section
9831 // derived from that variable must not appear as list items of map
9832 // clauses of the same construct.
9833 //
9834 // Also, cover one of the cases in:
9835 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9836 // If any part of the original storage of a list item has corresponding
9837 // storage in the device data environment, all of the original storage
9838 // must have corresponding storage in the device data environment.
9839 //
9840 if (DerivedType->isAnyPointerType()) {
9841 if (CI == CE || SI == SE) {
9842 SemaRef.Diag(
9843 DerivedLoc,
9844 diag::err_omp_pointer_mapped_along_with_derived_section)
9845 << DerivedLoc;
9846 } else {
9847 assert(CI != CE && SI != SE);
9848 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9849 << DerivedLoc;
9850 }
9851 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9852 << RE->getSourceRange();
9853 return true;
9854 }
9855
9856 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9857 // List items of map clauses in the same construct must not share
9858 // original storage.
9859 //
9860 // An expression is a subset of the other.
9861 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9862 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9863 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9864 << RE->getSourceRange();
9865 return true;
9866 }
9867
9868 // The current expression uses the same base as other expression in the
9869 // data environment but does not contain it completelly.
9870 if (!CurrentRegionOnly && SI != SE)
9871 EnclosingExpr = RE;
9872
9873 // The current expression is a subset of the expression in the data
9874 // environment.
9875 IsEnclosedByDataEnvironmentExpr |=
9876 (!CurrentRegionOnly && CI != CE && SI == SE);
9877
9878 return false;
9879 });
9880
9881 if (CurrentRegionOnly)
9882 return FoundError;
9883
9884 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9885 // If any part of the original storage of a list item has corresponding
9886 // storage in the device data environment, all of the original storage must
9887 // have corresponding storage in the device data environment.
9888 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9889 // If a list item is an element of a structure, and a different element of
9890 // the structure has a corresponding list item in the device data environment
9891 // prior to a task encountering the construct associated with the map clause,
9892 // then the list item must also have a correspnding list item in the device
9893 // data environment prior to the task encountering the construct.
9894 //
9895 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9896 SemaRef.Diag(ELoc,
9897 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9898 << ERange;
9899 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9900 << EnclosingExpr->getSourceRange();
9901 return true;
9902 }
9903
9904 return FoundError;
9905}
9906
Samuel Antao23abd722016-01-19 20:40:49 +00009907OMPClause *
9908Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9909 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9910 SourceLocation MapLoc, SourceLocation ColonLoc,
9911 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9912 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009913 SmallVector<Expr *, 4> Vars;
9914
9915 for (auto &RE : VarList) {
9916 assert(RE && "Null expr in omp map");
9917 if (isa<DependentScopeDeclRefExpr>(RE)) {
9918 // It will be analyzed later.
9919 Vars.push_back(RE);
9920 continue;
9921 }
9922 SourceLocation ELoc = RE->getExprLoc();
9923
Kelvin Li0bff7af2015-11-23 05:32:03 +00009924 auto *VE = RE->IgnoreParenLValueCasts();
9925
9926 if (VE->isValueDependent() || VE->isTypeDependent() ||
9927 VE->isInstantiationDependent() ||
9928 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009929 // We can only analyze this information once the missing information is
9930 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009931 Vars.push_back(RE);
9932 continue;
9933 }
9934
9935 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009936
Samuel Antao5de996e2016-01-22 20:21:36 +00009937 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9938 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9939 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009940 continue;
9941 }
9942
Samuel Antao5de996e2016-01-22 20:21:36 +00009943 // Obtain the array or member expression bases if required.
9944 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9945 if (!BE)
9946 continue;
9947
9948 // If the base is a reference to a variable, we rely on that variable for
9949 // the following checks. If it is a 'this' expression we rely on the field.
9950 ValueDecl *D = nullptr;
9951 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9952 D = DRE->getDecl();
9953 } else {
9954 auto *ME = cast<MemberExpr>(BE);
9955 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9956 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009957 }
9958 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009959
Samuel Antao5de996e2016-01-22 20:21:36 +00009960 auto *VD = dyn_cast<VarDecl>(D);
9961 auto *FD = dyn_cast<FieldDecl>(D);
9962
9963 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009964 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009965
9966 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9967 // threadprivate variables cannot appear in a map clause.
9968 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009969 auto DVar = DSAStack->getTopDSA(VD, false);
9970 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9971 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9972 continue;
9973 }
9974
Samuel Antao5de996e2016-01-22 20:21:36 +00009975 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9976 // A list item cannot appear in both a map clause and a data-sharing
9977 // attribute clause on the same construct.
9978 //
9979 // TODO: Implement this check - it cannot currently be tested because of
9980 // missing implementation of the other data sharing clauses in target
9981 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009982
Samuel Antao5de996e2016-01-22 20:21:36 +00009983 // Check conflicts with other map clause expressions. We check the conflicts
9984 // with the current construct separately from the enclosing data
9985 // environment, because the restrictions are different.
9986 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9987 /*CurrentRegionOnly=*/true))
9988 break;
9989 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9990 /*CurrentRegionOnly=*/false))
9991 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009992
Samuel Antao5de996e2016-01-22 20:21:36 +00009993 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9994 // If the type of a list item is a reference to a type T then the type will
9995 // be considered to be T for all purposes of this clause.
9996 QualType Type = D->getType();
9997 if (Type->isReferenceType())
9998 Type = Type->getPointeeType();
9999
10000 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010001 // A list item must have a mappable type.
10002 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10003 DSAStack, Type))
10004 continue;
10005
Samuel Antaodf67fc42016-01-19 19:15:56 +000010006 // target enter data
10007 // OpenMP [2.10.2, Restrictions, p. 99]
10008 // A map-type must be specified in all map clauses and must be either
10009 // to or alloc.
10010 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10011 if (DKind == OMPD_target_enter_data &&
10012 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10013 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010014 << (IsMapTypeImplicit ? 1 : 0)
10015 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +000010016 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010017 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +000010018 }
10019
Samuel Antao72590762016-01-19 20:04:50 +000010020 // target exit_data
10021 // OpenMP [2.10.3, Restrictions, p. 102]
10022 // A map-type must be specified in all map clauses and must be either
10023 // from, release, or delete.
10024 DKind = DSAStack->getCurrentDirective();
10025 if (DKind == OMPD_target_exit_data &&
10026 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10027 MapType == OMPC_MAP_delete)) {
10028 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010029 << (IsMapTypeImplicit ? 1 : 0)
10030 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +000010031 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010032 continue;
Samuel Antao72590762016-01-19 20:04:50 +000010033 }
10034
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010035 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10036 // A list item cannot appear in both a map clause and a data-sharing
10037 // attribute clause on the same construct
10038 if (DKind == OMPD_target && VD) {
10039 auto DVar = DSAStack->getTopDSA(VD, false);
10040 if (isOpenMPPrivate(DVar.CKind)) {
10041 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10042 << getOpenMPClauseName(DVar.CKind)
10043 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10044 ReportOriginalDSA(*this, DSAStack, D, DVar);
10045 continue;
10046 }
10047 }
10048
Kelvin Li0bff7af2015-11-23 05:32:03 +000010049 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +000010050 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010051 }
Kelvin Li0bff7af2015-11-23 05:32:03 +000010052
Samuel Antao5de996e2016-01-22 20:21:36 +000010053 // We need to produce a map clause even if we don't have variables so that
10054 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010055 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +000010056 MapTypeModifier, MapType, IsMapTypeImplicit,
10057 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010058}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010059
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010060QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10061 TypeResult ParsedType) {
10062 assert(ParsedType.isUsable());
10063
10064 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10065 if (ReductionType.isNull())
10066 return QualType();
10067
10068 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10069 // A type name in a declare reduction directive cannot be a function type, an
10070 // array type, a reference type, or a type qualified with const, volatile or
10071 // restrict.
10072 if (ReductionType.hasQualifiers()) {
10073 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10074 return QualType();
10075 }
10076
10077 if (ReductionType->isFunctionType()) {
10078 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10079 return QualType();
10080 }
10081 if (ReductionType->isReferenceType()) {
10082 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10083 return QualType();
10084 }
10085 if (ReductionType->isArrayType()) {
10086 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10087 return QualType();
10088 }
10089 return ReductionType;
10090}
10091
10092Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10093 Scope *S, DeclContext *DC, DeclarationName Name,
10094 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10095 AccessSpecifier AS, Decl *PrevDeclInScope) {
10096 SmallVector<Decl *, 8> Decls;
10097 Decls.reserve(ReductionTypes.size());
10098
10099 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10100 ForRedeclaration);
10101 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10102 // A reduction-identifier may not be re-declared in the current scope for the
10103 // same type or for a type that is compatible according to the base language
10104 // rules.
10105 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10106 OMPDeclareReductionDecl *PrevDRD = nullptr;
10107 bool InCompoundScope = true;
10108 if (S != nullptr) {
10109 // Find previous declaration with the same name not referenced in other
10110 // declarations.
10111 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10112 InCompoundScope =
10113 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10114 LookupName(Lookup, S);
10115 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10116 /*AllowInlineNamespace=*/false);
10117 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10118 auto Filter = Lookup.makeFilter();
10119 while (Filter.hasNext()) {
10120 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10121 if (InCompoundScope) {
10122 auto I = UsedAsPrevious.find(PrevDecl);
10123 if (I == UsedAsPrevious.end())
10124 UsedAsPrevious[PrevDecl] = false;
10125 if (auto *D = PrevDecl->getPrevDeclInScope())
10126 UsedAsPrevious[D] = true;
10127 }
10128 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10129 PrevDecl->getLocation();
10130 }
10131 Filter.done();
10132 if (InCompoundScope) {
10133 for (auto &PrevData : UsedAsPrevious) {
10134 if (!PrevData.second) {
10135 PrevDRD = PrevData.first;
10136 break;
10137 }
10138 }
10139 }
10140 } else if (PrevDeclInScope != nullptr) {
10141 auto *PrevDRDInScope = PrevDRD =
10142 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10143 do {
10144 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10145 PrevDRDInScope->getLocation();
10146 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10147 } while (PrevDRDInScope != nullptr);
10148 }
10149 for (auto &TyData : ReductionTypes) {
10150 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10151 bool Invalid = false;
10152 if (I != PreviousRedeclTypes.end()) {
10153 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10154 << TyData.first;
10155 Diag(I->second, diag::note_previous_definition);
10156 Invalid = true;
10157 }
10158 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10159 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10160 Name, TyData.first, PrevDRD);
10161 DC->addDecl(DRD);
10162 DRD->setAccess(AS);
10163 Decls.push_back(DRD);
10164 if (Invalid)
10165 DRD->setInvalidDecl();
10166 else
10167 PrevDRD = DRD;
10168 }
10169
10170 return DeclGroupPtrTy::make(
10171 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10172}
10173
10174void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10175 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10176
10177 // Enter new function scope.
10178 PushFunctionScope();
10179 getCurFunction()->setHasBranchProtectedScope();
10180 getCurFunction()->setHasOMPDeclareReductionCombiner();
10181
10182 if (S != nullptr)
10183 PushDeclContext(S, DRD);
10184 else
10185 CurContext = DRD;
10186
10187 PushExpressionEvaluationContext(PotentiallyEvaluated);
10188
10189 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010190 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10191 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10192 // uses semantics of argument handles by value, but it should be passed by
10193 // reference. C lang does not support references, so pass all parameters as
10194 // pointers.
10195 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010196 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010197 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010198 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10199 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10200 // uses semantics of argument handles by value, but it should be passed by
10201 // reference. C lang does not support references, so pass all parameters as
10202 // pointers.
10203 // Create 'T omp_out;' variable.
10204 auto *OmpOutParm =
10205 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10206 if (S != nullptr) {
10207 PushOnScopeChains(OmpInParm, S);
10208 PushOnScopeChains(OmpOutParm, S);
10209 } else {
10210 DRD->addDecl(OmpInParm);
10211 DRD->addDecl(OmpOutParm);
10212 }
10213}
10214
10215void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10216 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10217 DiscardCleanupsInEvaluationContext();
10218 PopExpressionEvaluationContext();
10219
10220 PopDeclContext();
10221 PopFunctionScopeInfo();
10222
10223 if (Combiner != nullptr)
10224 DRD->setCombiner(Combiner);
10225 else
10226 DRD->setInvalidDecl();
10227}
10228
10229void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10230 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10231
10232 // Enter new function scope.
10233 PushFunctionScope();
10234 getCurFunction()->setHasBranchProtectedScope();
10235
10236 if (S != nullptr)
10237 PushDeclContext(S, DRD);
10238 else
10239 CurContext = DRD;
10240
10241 PushExpressionEvaluationContext(PotentiallyEvaluated);
10242
10243 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010244 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10245 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10246 // uses semantics of argument handles by value, but it should be passed by
10247 // reference. C lang does not support references, so pass all parameters as
10248 // pointers.
10249 // Create 'T omp_priv;' variable.
10250 auto *OmpPrivParm =
10251 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010252 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10253 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10254 // uses semantics of argument handles by value, but it should be passed by
10255 // reference. C lang does not support references, so pass all parameters as
10256 // pointers.
10257 // Create 'T omp_orig;' variable.
10258 auto *OmpOrigParm =
10259 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010260 if (S != nullptr) {
10261 PushOnScopeChains(OmpPrivParm, S);
10262 PushOnScopeChains(OmpOrigParm, S);
10263 } else {
10264 DRD->addDecl(OmpPrivParm);
10265 DRD->addDecl(OmpOrigParm);
10266 }
10267}
10268
10269void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10270 Expr *Initializer) {
10271 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10272 DiscardCleanupsInEvaluationContext();
10273 PopExpressionEvaluationContext();
10274
10275 PopDeclContext();
10276 PopFunctionScopeInfo();
10277
10278 if (Initializer != nullptr)
10279 DRD->setInitializer(Initializer);
10280 else
10281 DRD->setInvalidDecl();
10282}
10283
10284Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10285 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10286 for (auto *D : DeclReductions.get()) {
10287 if (IsValid) {
10288 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10289 if (S != nullptr)
10290 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10291 } else
10292 D->setInvalidDecl();
10293 }
10294 return DeclReductions;
10295}
10296
Kelvin Li099bb8c2015-11-24 20:50:12 +000010297OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10298 SourceLocation StartLoc,
10299 SourceLocation LParenLoc,
10300 SourceLocation EndLoc) {
10301 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010302
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010303 // OpenMP [teams Constrcut, Restrictions]
10304 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010305 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10306 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010307 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010308
10309 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10310}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010311
10312OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10313 SourceLocation StartLoc,
10314 SourceLocation LParenLoc,
10315 SourceLocation EndLoc) {
10316 Expr *ValExpr = ThreadLimit;
10317
10318 // OpenMP [teams Constrcut, Restrictions]
10319 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010320 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10321 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010322 return nullptr;
10323
10324 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10325 EndLoc);
10326}
Alexey Bataeva0569352015-12-01 10:17:31 +000010327
10328OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10329 SourceLocation StartLoc,
10330 SourceLocation LParenLoc,
10331 SourceLocation EndLoc) {
10332 Expr *ValExpr = Priority;
10333
10334 // OpenMP [2.9.1, task Constrcut]
10335 // The priority-value is a non-negative numerical scalar expression.
10336 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10337 /*StrictlyPositive=*/false))
10338 return nullptr;
10339
10340 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10341}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010342
10343OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10344 SourceLocation StartLoc,
10345 SourceLocation LParenLoc,
10346 SourceLocation EndLoc) {
10347 Expr *ValExpr = Grainsize;
10348
10349 // OpenMP [2.9.2, taskloop Constrcut]
10350 // The parameter of the grainsize clause must be a positive integer
10351 // expression.
10352 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10353 /*StrictlyPositive=*/true))
10354 return nullptr;
10355
10356 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10357}
Alexey Bataev382967a2015-12-08 12:06:20 +000010358
10359OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10360 SourceLocation StartLoc,
10361 SourceLocation LParenLoc,
10362 SourceLocation EndLoc) {
10363 Expr *ValExpr = NumTasks;
10364
10365 // OpenMP [2.9.2, taskloop Constrcut]
10366 // The parameter of the num_tasks clause must be a positive integer
10367 // expression.
10368 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10369 /*StrictlyPositive=*/true))
10370 return nullptr;
10371
10372 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10373}
10374
Alexey Bataev28c75412015-12-15 08:19:24 +000010375OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10376 SourceLocation LParenLoc,
10377 SourceLocation EndLoc) {
10378 // OpenMP [2.13.2, critical construct, Description]
10379 // ... where hint-expression is an integer constant expression that evaluates
10380 // to a valid lock hint.
10381 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10382 if (HintExpr.isInvalid())
10383 return nullptr;
10384 return new (Context)
10385 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10386}
10387
Carlo Bertollib4adf552016-01-15 18:50:31 +000010388OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10389 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10390 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10391 SourceLocation EndLoc) {
10392 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10393 std::string Values;
10394 Values += "'";
10395 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10396 Values += "'";
10397 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10398 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10399 return nullptr;
10400 }
10401 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010402 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010403 if (ChunkSize) {
10404 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10405 !ChunkSize->isInstantiationDependent() &&
10406 !ChunkSize->containsUnexpandedParameterPack()) {
10407 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10408 ExprResult Val =
10409 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10410 if (Val.isInvalid())
10411 return nullptr;
10412
10413 ValExpr = Val.get();
10414
10415 // OpenMP [2.7.1, Restrictions]
10416 // chunk_size must be a loop invariant integer expression with a positive
10417 // value.
10418 llvm::APSInt Result;
10419 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10420 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10421 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10422 << "dist_schedule" << ChunkSize->getSourceRange();
10423 return nullptr;
10424 }
10425 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010426 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10427 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10428 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010429 }
10430 }
10431 }
10432
10433 return new (Context)
10434 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010435 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010436}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010437
10438OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10439 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10440 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10441 SourceLocation KindLoc, SourceLocation EndLoc) {
10442 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10443 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10444 Kind != OMPC_DEFAULTMAP_scalar) {
10445 std::string Value;
10446 SourceLocation Loc;
10447 Value += "'";
10448 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10449 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10450 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10451 Loc = MLoc;
10452 } else {
10453 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10454 OMPC_DEFAULTMAP_scalar);
10455 Loc = KindLoc;
10456 }
10457 Value += "'";
10458 Diag(Loc, diag::err_omp_unexpected_clause_value)
10459 << Value << getOpenMPClauseName(OMPC_defaultmap);
10460 return nullptr;
10461 }
10462
10463 return new (Context)
10464 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10465}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010466
10467bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10468 DeclContext *CurLexicalContext = getCurLexicalContext();
10469 if (!CurLexicalContext->isFileContext() &&
10470 !CurLexicalContext->isExternCContext() &&
10471 !CurLexicalContext->isExternCXXContext()) {
10472 Diag(Loc, diag::err_omp_region_not_file_context);
10473 return false;
10474 }
10475 if (IsInOpenMPDeclareTargetContext) {
10476 Diag(Loc, diag::err_omp_enclosed_declare_target);
10477 return false;
10478 }
10479
10480 IsInOpenMPDeclareTargetContext = true;
10481 return true;
10482}
10483
10484void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10485 assert(IsInOpenMPDeclareTargetContext &&
10486 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10487
10488 IsInOpenMPDeclareTargetContext = false;
10489}
10490
10491static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10492 Sema &SemaRef, Decl *D) {
10493 if (!D)
10494 return;
10495 Decl *LD = nullptr;
10496 if (isa<TagDecl>(D)) {
10497 LD = cast<TagDecl>(D)->getDefinition();
10498 } else if (isa<VarDecl>(D)) {
10499 LD = cast<VarDecl>(D)->getDefinition();
10500
10501 // If this is an implicit variable that is legal and we do not need to do
10502 // anything.
10503 if (cast<VarDecl>(D)->isImplicit()) {
10504 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10505 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10506 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10507 return;
10508 }
10509
10510 } else if (isa<FunctionDecl>(D)) {
10511 const FunctionDecl *FD = nullptr;
10512 if (cast<FunctionDecl>(D)->hasBody(FD))
10513 LD = const_cast<FunctionDecl *>(FD);
10514
10515 // If the definition is associated with the current declaration in the
10516 // target region (it can be e.g. a lambda) that is legal and we do not need
10517 // to do anything else.
10518 if (LD == D) {
10519 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10520 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10521 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10522 return;
10523 }
10524 }
10525 if (!LD)
10526 LD = D;
10527 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10528 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10529 // Outlined declaration is not declared target.
10530 if (LD->isOutOfLine()) {
10531 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10532 SemaRef.Diag(SL, diag::note_used_here) << SR;
10533 } else {
10534 DeclContext *DC = LD->getDeclContext();
10535 while (DC) {
10536 if (isa<FunctionDecl>(DC) &&
10537 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10538 break;
10539 DC = DC->getParent();
10540 }
10541 if (DC)
10542 return;
10543
10544 // Is not declared in target context.
10545 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10546 SemaRef.Diag(SL, diag::note_used_here) << SR;
10547 }
10548 // Mark decl as declared target to prevent further diagnostic.
10549 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10550 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10551 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10552 }
10553}
10554
10555static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10556 Sema &SemaRef, DSAStackTy *Stack,
10557 ValueDecl *VD) {
10558 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10559 return true;
10560 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10561 return false;
10562 return true;
10563}
10564
10565void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10566 if (!D || D->isInvalidDecl())
10567 return;
10568 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10569 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10570 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10571 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10572 if (DSAStack->isThreadPrivate(VD)) {
10573 Diag(SL, diag::err_omp_threadprivate_in_target);
10574 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10575 return;
10576 }
10577 }
10578 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10579 // Problem if any with var declared with incomplete type will be reported
10580 // as normal, so no need to check it here.
10581 if ((E || !VD->getType()->isIncompleteType()) &&
10582 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10583 // Mark decl as declared target to prevent further diagnostic.
10584 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
10585 VD->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10586 if (ASTMutationListener *ML = Context.getASTMutationListener())
10587 ML->DeclarationMarkedOpenMPDeclareTarget(VD);
10588 }
10589 return;
10590 }
10591 }
10592 if (!E) {
10593 // Checking declaration inside declare target region.
10594 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10595 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
10596 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10597 if (ASTMutationListener *ML = Context.getASTMutationListener())
10598 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10599 }
10600 return;
10601 }
10602 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10603}