blob: 6458eee56cb31879bd09e87b77eee14f3f492eb4 [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
647 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000648 // in a Construct, C/C++, predetermined, p.4]
649 // Static data members are shared.
650 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
651 // in a Construct, C/C++, predetermined, p.7]
652 // Variables with static storage duration that are declared in a scope
653 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000654 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000655 DSAVarData DVarTemp =
656 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
657 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000658 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000659
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000660 DVar.CKind = OMPC_shared;
661 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000662 }
663
664 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000665 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
666 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000667 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
668 // in a Construct, C/C++, predetermined, p.6]
669 // Variables with const qualified type having no mutable member are
670 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000671 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000672 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000673 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
674 if (auto *CTD = CTSD->getSpecializedTemplate())
675 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000676 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000677 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
678 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000679 // Variables with const-qualified type having no mutable member may be
680 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000681 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
682 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000683 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
684 return DVar;
685
Alexey Bataev758e55e2013-09-06 18:03:48 +0000686 DVar.CKind = OMPC_shared;
687 return DVar;
688 }
689
Alexey Bataev758e55e2013-09-06 18:03:48 +0000690 // Explicitly specified attributes and local variables with predetermined
691 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000692 auto StartI = std::next(Stack.rbegin());
693 auto EndI = std::prev(Stack.rend());
694 if (FromParent && StartI != EndI) {
695 StartI = std::next(StartI);
696 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000697 auto I = std::prev(StartI);
698 if (I->SharingMap.count(D)) {
699 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000700 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000701 DVar.CKind = I->SharingMap[D].Attributes;
702 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000703 }
704
705 return DVar;
706}
707
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000708DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
709 bool FromParent) {
710 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000711 auto StartI = Stack.rbegin();
712 auto EndI = std::prev(Stack.rend());
713 if (FromParent && StartI != EndI) {
714 StartI = std::next(StartI);
715 }
716 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000717}
718
Alexey Bataevf29276e2014-06-18 04:14:57 +0000719template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000720DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000721 DirectivesPredicate DPred,
722 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000723 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000724 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000725 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000726 if (FromParent && StartI != EndI) {
727 StartI = std::next(StartI);
728 }
729 for (auto I = StartI, EE = EndI; I != EE; ++I) {
730 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000731 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000732 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000733 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000734 return DVar;
735 }
736 return DSAVarData();
737}
738
Alexey Bataevf29276e2014-06-18 04:14:57 +0000739template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000740DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000741DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000742 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000743 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000744 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000745 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000746 if (FromParent && StartI != EndI) {
747 StartI = std::next(StartI);
748 }
749 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000750 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000751 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000752 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000753 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000754 return DVar;
755 return DSAVarData();
756 }
757 return DSAVarData();
758}
759
Alexey Bataevaac108a2015-06-23 04:51:00 +0000760bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000761 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000762 unsigned Level) {
763 if (CPred(ClauseKindMode))
764 return true;
765 if (isClauseParsingMode())
766 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000767 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000768 auto StartI = Stack.rbegin();
769 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000770 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000771 return false;
772 std::advance(StartI, Level);
773 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
774 CPred(StartI->SharingMap[D].Attributes);
775}
776
Samuel Antao4be30e92015-10-02 17:14:03 +0000777bool DSAStackTy::hasExplicitDirective(
778 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
779 unsigned Level) {
780 if (isClauseParsingMode())
781 ++Level;
782 auto StartI = Stack.rbegin();
783 auto EndI = std::prev(Stack.rend());
784 if (std::distance(StartI, EndI) <= (int)Level)
785 return false;
786 std::advance(StartI, Level);
787 return DPred(StartI->Directive);
788}
789
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000790template <class NamedDirectivesPredicate>
791bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
792 auto StartI = std::next(Stack.rbegin());
793 auto EndI = std::prev(Stack.rend());
794 if (FromParent && StartI != EndI) {
795 StartI = std::next(StartI);
796 }
797 for (auto I = StartI, EE = EndI; I != EE; ++I) {
798 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
799 return true;
800 }
801 return false;
802}
803
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000804OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
805 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
806 if (I->CurScope == S)
807 return I->Directive;
808 return OMPD_unknown;
809}
810
Alexey Bataev758e55e2013-09-06 18:03:48 +0000811void Sema::InitDataSharingAttributesStack() {
812 VarDataSharingAttributesStack = new DSAStackTy(*this);
813}
814
815#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
816
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000817bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000818 const CapturedRegionScopeInfo *RSI) {
819 assert(LangOpts.OpenMP && "OpenMP is not allowed");
820
821 auto &Ctx = getASTContext();
822 bool IsByRef = true;
823
824 // Find the directive that is associated with the provided scope.
825 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000826 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000827
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000828 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000829 // This table summarizes how a given variable should be passed to the device
830 // given its type and the clauses where it appears. This table is based on
831 // the description in OpenMP 4.5 [2.10.4, target Construct] and
832 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
833 //
834 // =========================================================================
835 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
836 // | |(tofrom:scalar)| | pvt | | | |
837 // =========================================================================
838 // | scl | | | | - | | bycopy|
839 // | scl | | - | x | - | - | bycopy|
840 // | scl | | x | - | - | - | null |
841 // | scl | x | | | - | | byref |
842 // | scl | x | - | x | - | - | bycopy|
843 // | scl | x | x | - | - | - | null |
844 // | scl | | - | - | - | x | byref |
845 // | scl | x | - | - | - | x | byref |
846 //
847 // | agg | n.a. | | | - | | byref |
848 // | agg | n.a. | - | x | - | - | byref |
849 // | agg | n.a. | x | - | - | - | null |
850 // | agg | n.a. | - | - | - | x | byref |
851 // | agg | n.a. | - | - | - | x[] | byref |
852 //
853 // | ptr | n.a. | | | - | | bycopy|
854 // | ptr | n.a. | - | x | - | - | bycopy|
855 // | ptr | n.a. | x | - | - | - | null |
856 // | ptr | n.a. | - | - | - | x | byref |
857 // | ptr | n.a. | - | - | - | x[] | bycopy|
858 // | ptr | n.a. | - | - | x | | bycopy|
859 // | ptr | n.a. | - | - | x | x | bycopy|
860 // | ptr | n.a. | - | - | x | x[] | bycopy|
861 // =========================================================================
862 // Legend:
863 // scl - scalar
864 // ptr - pointer
865 // agg - aggregate
866 // x - applies
867 // - - invalid in this combination
868 // [] - mapped with an array section
869 // byref - should be mapped by reference
870 // byval - should be mapped by value
871 // null - initialize a local variable to null on the device
872 //
873 // Observations:
874 // - All scalar declarations that show up in a map clause have to be passed
875 // by reference, because they may have been mapped in the enclosing data
876 // environment.
877 // - If the scalar value does not fit the size of uintptr, it has to be
878 // passed by reference, regardless the result in the table above.
879 // - For pointers mapped by value that have either an implicit map or an
880 // array section, the runtime library may pass the NULL value to the
881 // device instead of the value passed to it by the compiler.
882
883 // FIXME: Right now, only implicit maps are implemented. Properly mapping
884 // values requires having the map, private, and firstprivate clauses SEMA
885 // and parsing in place, which we don't yet.
886
887 if (Ty->isReferenceType())
888 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
889 IsByRef = !Ty->isScalarType();
890 }
891
892 // When passing data by value, we need to make sure it fits the uintptr size
893 // and alignment, because the runtime library only deals with uintptr types.
894 // If it does not fit the uintptr size, we need to pass the data by reference
895 // instead.
896 if (!IsByRef &&
897 (Ctx.getTypeSizeInChars(Ty) >
898 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000899 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000900 IsByRef = true;
901
902 return IsByRef;
903}
904
Alexey Bataev90c228f2016-02-08 09:29:13 +0000905VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000906 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000907 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000908
909 // If we are attempting to capture a global variable in a directive with
910 // 'target' we return true so that this global is also mapped to the device.
911 //
912 // FIXME: If the declaration is enclosed in a 'declare target' directive,
913 // then it should not be captured. Therefore, an extra check has to be
914 // inserted here once support for 'declare target' is added.
915 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000916 auto *VD = dyn_cast<VarDecl>(D);
917 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000918 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000919 !DSAStack->isClauseParsingMode())
920 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000921 if (DSAStack->getCurScope() &&
922 DSAStack->hasDirective(
923 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
924 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000925 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000926 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000927 false))
928 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000929 }
930
Alexey Bataev48977c32015-08-04 08:10:48 +0000931 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
932 (!DSAStack->isClauseParsingMode() ||
933 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000934 auto &&Info = DSAStack->isLoopControlVariable(D);
935 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000936 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000937 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000938 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000939 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000940 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000941 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000942 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000943 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000944 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000945 if (DVarPrivate.CKind != OMPC_unknown)
946 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000947 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000948 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000949}
950
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000951bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000952 assert(LangOpts.OpenMP && "OpenMP is not allowed");
953 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000954 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000955}
956
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000957bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000958 assert(LangOpts.OpenMP && "OpenMP is not allowed");
959 // Return true if the current level is no longer enclosed in a target region.
960
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000961 auto *VD = dyn_cast<VarDecl>(D);
962 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000963 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
964 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000965}
966
Alexey Bataeved09d242014-05-28 05:53:51 +0000967void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000968
969void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
970 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000971 Scope *CurScope, SourceLocation Loc) {
972 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000973 PushExpressionEvaluationContext(PotentiallyEvaluated);
974}
975
Alexey Bataevaac108a2015-06-23 04:51:00 +0000976void Sema::StartOpenMPClause(OpenMPClauseKind K) {
977 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000978}
979
Alexey Bataevaac108a2015-06-23 04:51:00 +0000980void Sema::EndOpenMPClause() {
981 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000982}
983
Alexey Bataev758e55e2013-09-06 18:03:48 +0000984void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000985 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
986 // A variable of class type (or array thereof) that appears in a lastprivate
987 // clause requires an accessible, unambiguous default constructor for the
988 // class type, unless the list item is also specified in a firstprivate
989 // clause.
990 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000991 for (auto *C : D->clauses()) {
992 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
993 SmallVector<Expr *, 8> PrivateCopies;
994 for (auto *DE : Clause->varlists()) {
995 if (DE->isValueDependent() || DE->isTypeDependent()) {
996 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000997 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000998 }
Alexey Bataev74caaf22016-02-20 04:09:36 +0000999 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001000 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1001 QualType Type = VD->getType().getNonReferenceType();
1002 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001003 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001004 // Generate helper private variable and initialize it with the
1005 // default value. The address of the original variable is replaced
1006 // by the address of the new private variable in CodeGen. This new
1007 // variable is not added to IdResolver, so the code in the OpenMP
1008 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001009 auto *VDPrivate = buildVarDecl(
1010 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001011 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001012 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1013 if (VDPrivate->isInvalidDecl())
1014 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001015 PrivateCopies.push_back(buildDeclRefExpr(
1016 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001017 } else {
1018 // The variable is also a firstprivate, so initialization sequence
1019 // for private copy is generated already.
1020 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001021 }
1022 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001023 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001024 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001025 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001026 }
1027 }
1028 }
1029
Alexey Bataev758e55e2013-09-06 18:03:48 +00001030 DSAStack->pop();
1031 DiscardCleanupsInEvaluationContext();
1032 PopExpressionEvaluationContext();
1033}
1034
Alexey Bataev5a3af132016-03-29 08:58:54 +00001035static bool
1036FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1037 Expr *NumIterations, Sema &SemaRef, Scope *S);
Alexander Musman3276a272015-03-21 10:12:56 +00001038
Alexey Bataeva769e072013-03-22 06:34:35 +00001039namespace {
1040
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001041class VarDeclFilterCCC : public CorrectionCandidateCallback {
1042private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001043 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001044
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001045public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001046 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001047 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001048 NamedDecl *ND = Candidate.getCorrectionDecl();
1049 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1050 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001051 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1052 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001053 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001054 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001055 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001056};
Alexey Bataeved09d242014-05-28 05:53:51 +00001057} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001058
1059ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1060 CXXScopeSpec &ScopeSpec,
1061 const DeclarationNameInfo &Id) {
1062 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1063 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1064
1065 if (Lookup.isAmbiguous())
1066 return ExprError();
1067
1068 VarDecl *VD;
1069 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001070 if (TypoCorrection Corrected = CorrectTypo(
1071 Id, LookupOrdinaryName, CurScope, nullptr,
1072 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001073 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001074 PDiag(Lookup.empty()
1075 ? diag::err_undeclared_var_use_suggest
1076 : diag::err_omp_expected_var_arg_suggest)
1077 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001078 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001079 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001080 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1081 : diag::err_omp_expected_var_arg)
1082 << Id.getName();
1083 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001084 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001085 } else {
1086 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001087 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001088 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1089 return ExprError();
1090 }
1091 }
1092 Lookup.suppressDiagnostics();
1093
1094 // OpenMP [2.9.2, Syntax, C/C++]
1095 // Variables must be file-scope, namespace-scope, or static block-scope.
1096 if (!VD->hasGlobalStorage()) {
1097 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001098 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1099 bool IsDecl =
1100 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001101 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001102 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1103 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001104 return ExprError();
1105 }
1106
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001107 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1108 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001109 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1110 // A threadprivate directive for file-scope variables must appear outside
1111 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001112 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1113 !getCurLexicalContext()->isTranslationUnit()) {
1114 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001115 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1116 bool IsDecl =
1117 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1118 Diag(VD->getLocation(),
1119 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1120 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001121 return ExprError();
1122 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001123 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1124 // A threadprivate directive for static class member variables must appear
1125 // in the class definition, in the same scope in which the member
1126 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001127 if (CanonicalVD->isStaticDataMember() &&
1128 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1129 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001130 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1131 bool IsDecl =
1132 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1133 Diag(VD->getLocation(),
1134 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1135 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001136 return ExprError();
1137 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001138 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1139 // A threadprivate directive for namespace-scope variables must appear
1140 // outside any definition or declaration other than the namespace
1141 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001142 if (CanonicalVD->getDeclContext()->isNamespace() &&
1143 (!getCurLexicalContext()->isFileContext() ||
1144 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1145 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001146 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1147 bool IsDecl =
1148 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1149 Diag(VD->getLocation(),
1150 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1151 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001152 return ExprError();
1153 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001154 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1155 // A threadprivate directive for static block-scope variables must appear
1156 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001157 if (CanonicalVD->isStaticLocal() && CurScope &&
1158 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001159 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001160 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1161 bool IsDecl =
1162 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1163 Diag(VD->getLocation(),
1164 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1165 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001166 return ExprError();
1167 }
1168
1169 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1170 // A threadprivate directive must lexically precede all references to any
1171 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001172 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001173 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001174 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001175 return ExprError();
1176 }
1177
1178 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001179 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1180 SourceLocation(), VD,
1181 /*RefersToEnclosingVariableOrCapture=*/false,
1182 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001183}
1184
Alexey Bataeved09d242014-05-28 05:53:51 +00001185Sema::DeclGroupPtrTy
1186Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1187 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001188 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001189 CurContext->addDecl(D);
1190 return DeclGroupPtrTy::make(DeclGroupRef(D));
1191 }
David Blaikie0403cb12016-01-15 23:43:25 +00001192 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001193}
1194
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001195namespace {
1196class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1197 Sema &SemaRef;
1198
1199public:
1200 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1201 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1202 if (VD->hasLocalStorage()) {
1203 SemaRef.Diag(E->getLocStart(),
1204 diag::err_omp_local_var_in_threadprivate_init)
1205 << E->getSourceRange();
1206 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1207 << VD << VD->getSourceRange();
1208 return true;
1209 }
1210 }
1211 return false;
1212 }
1213 bool VisitStmt(const Stmt *S) {
1214 for (auto Child : S->children()) {
1215 if (Child && Visit(Child))
1216 return true;
1217 }
1218 return false;
1219 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001220 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001221};
1222} // namespace
1223
Alexey Bataeved09d242014-05-28 05:53:51 +00001224OMPThreadPrivateDecl *
1225Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001226 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001227 for (auto &RefExpr : VarList) {
1228 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001229 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1230 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001231
Alexey Bataev376b4a42016-02-09 09:41:09 +00001232 // Mark variable as used.
1233 VD->setReferenced();
1234 VD->markUsed(Context);
1235
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001236 QualType QType = VD->getType();
1237 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1238 // It will be analyzed later.
1239 Vars.push_back(DE);
1240 continue;
1241 }
1242
Alexey Bataeva769e072013-03-22 06:34:35 +00001243 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1244 // A threadprivate variable must not have an incomplete type.
1245 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001246 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001247 continue;
1248 }
1249
1250 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1251 // A threadprivate variable must not have a reference type.
1252 if (VD->getType()->isReferenceType()) {
1253 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001254 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1255 bool IsDecl =
1256 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1257 Diag(VD->getLocation(),
1258 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1259 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001260 continue;
1261 }
1262
Samuel Antaof8b50122015-07-13 22:54:53 +00001263 // Check if this is a TLS variable. If TLS is not being supported, produce
1264 // the corresponding diagnostic.
1265 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1266 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1267 getLangOpts().OpenMPUseTLS &&
1268 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001269 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1270 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001271 Diag(ILoc, diag::err_omp_var_thread_local)
1272 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001273 bool IsDecl =
1274 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1275 Diag(VD->getLocation(),
1276 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1277 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001278 continue;
1279 }
1280
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001281 // Check if initial value of threadprivate variable reference variable with
1282 // local storage (it is not supported by runtime).
1283 if (auto Init = VD->getAnyInitializer()) {
1284 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001285 if (Checker.Visit(Init))
1286 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001287 }
1288
Alexey Bataeved09d242014-05-28 05:53:51 +00001289 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001290 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001291 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1292 Context, SourceRange(Loc, Loc)));
1293 if (auto *ML = Context.getASTMutationListener())
1294 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001295 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001296 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001297 if (!Vars.empty()) {
1298 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1299 Vars);
1300 D->setAccess(AS_public);
1301 }
1302 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001303}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001304
Alexey Bataev7ff55242014-06-19 09:13:45 +00001305static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001306 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001307 bool IsLoopIterVar = false) {
1308 if (DVar.RefExpr) {
1309 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1310 << getOpenMPClauseName(DVar.CKind);
1311 return;
1312 }
1313 enum {
1314 PDSA_StaticMemberShared,
1315 PDSA_StaticLocalVarShared,
1316 PDSA_LoopIterVarPrivate,
1317 PDSA_LoopIterVarLinear,
1318 PDSA_LoopIterVarLastprivate,
1319 PDSA_ConstVarShared,
1320 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001321 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001322 PDSA_LocalVarPrivate,
1323 PDSA_Implicit
1324 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001325 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001326 auto ReportLoc = D->getLocation();
1327 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001328 if (IsLoopIterVar) {
1329 if (DVar.CKind == OMPC_private)
1330 Reason = PDSA_LoopIterVarPrivate;
1331 else if (DVar.CKind == OMPC_lastprivate)
1332 Reason = PDSA_LoopIterVarLastprivate;
1333 else
1334 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001335 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1336 Reason = PDSA_TaskVarFirstprivate;
1337 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001338 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001339 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001340 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001341 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001342 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001343 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001344 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001345 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001346 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001347 ReportHint = true;
1348 Reason = PDSA_LocalVarPrivate;
1349 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001350 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001351 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001352 << Reason << ReportHint
1353 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1354 } else if (DVar.ImplicitDSALoc.isValid()) {
1355 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1356 << getOpenMPClauseName(DVar.CKind);
1357 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001358}
1359
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360namespace {
1361class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1362 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001363 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001364 bool ErrorFound;
1365 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001366 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001367 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001368
Alexey Bataev758e55e2013-09-06 18:03:48 +00001369public:
1370 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001371 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001372 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001373 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1374 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001375
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001376 auto DVar = Stack->getTopDSA(VD, false);
1377 // Check if the variable has explicit DSA set and stop analysis if it so.
1378 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001379
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001380 auto ELoc = E->getExprLoc();
1381 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001382 // The default(none) clause requires that each variable that is referenced
1383 // in the construct, and does not have a predetermined data-sharing
1384 // attribute, must have its data-sharing attribute explicitly determined
1385 // by being listed in a data-sharing attribute clause.
1386 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001387 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001388 VarsWithInheritedDSA.count(VD) == 0) {
1389 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001390 return;
1391 }
1392
1393 // OpenMP [2.9.3.6, Restrictions, p.2]
1394 // A list item that appears in a reduction clause of the innermost
1395 // enclosing worksharing or parallel construct may not be accessed in an
1396 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001397 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001398 [](OpenMPDirectiveKind K) -> bool {
1399 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001400 isOpenMPWorksharingDirective(K) ||
1401 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001402 },
1403 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001404 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1405 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001406 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1407 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001408 return;
1409 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001410
1411 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001412 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001413 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001414 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001415 }
1416 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001417 void VisitMemberExpr(MemberExpr *E) {
1418 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1419 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1420 auto DVar = Stack->getTopDSA(FD, false);
1421 // Check if the variable has explicit DSA set and stop analysis if it
1422 // so.
1423 if (DVar.RefExpr)
1424 return;
1425
1426 auto ELoc = E->getExprLoc();
1427 auto DKind = Stack->getCurrentDirective();
1428 // OpenMP [2.9.3.6, Restrictions, p.2]
1429 // A list item that appears in a reduction clause of the innermost
1430 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001431 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001432 DVar =
1433 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1434 [](OpenMPDirectiveKind K) -> bool {
1435 return isOpenMPParallelDirective(K) ||
1436 isOpenMPWorksharingDirective(K) ||
1437 isOpenMPTeamsDirective(K);
1438 },
1439 false);
1440 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1441 ErrorFound = true;
1442 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1443 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1444 return;
1445 }
1446
1447 // Define implicit data-sharing attributes for task.
1448 DVar = Stack->getImplicitDSA(FD, false);
1449 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1450 ImplicitFirstprivate.push_back(E);
1451 }
1452 }
1453 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001454 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001455 for (auto *C : S->clauses()) {
1456 // Skip analysis of arguments of implicitly defined firstprivate clause
1457 // for task directives.
1458 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1459 for (auto *CC : C->children()) {
1460 if (CC)
1461 Visit(CC);
1462 }
1463 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001464 }
1465 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001466 for (auto *C : S->children()) {
1467 if (C && !isa<OMPExecutableDirective>(C))
1468 Visit(C);
1469 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001470 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001471
1472 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001473 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001474 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001475 return VarsWithInheritedDSA;
1476 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001477
Alexey Bataev7ff55242014-06-19 09:13:45 +00001478 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1479 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001480};
Alexey Bataeved09d242014-05-28 05:53:51 +00001481} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001482
Alexey Bataevbae9a792014-06-27 10:37:06 +00001483void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001484 switch (DKind) {
1485 case OMPD_parallel: {
1486 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001487 QualType KmpInt32PtrTy =
1488 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001489 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001490 std::make_pair(".global_tid.", KmpInt32PtrTy),
1491 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1492 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001493 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001494 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1495 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001496 break;
1497 }
1498 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001499 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001500 std::make_pair(StringRef(), QualType()) // __context with shared vars
1501 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001502 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1503 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001504 break;
1505 }
1506 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001507 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001508 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001509 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001510 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1511 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001512 break;
1513 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001514 case OMPD_for_simd: {
1515 Sema::CapturedParamNameType Params[] = {
1516 std::make_pair(StringRef(), QualType()) // __context with shared vars
1517 };
1518 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1519 Params);
1520 break;
1521 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001522 case OMPD_sections: {
1523 Sema::CapturedParamNameType Params[] = {
1524 std::make_pair(StringRef(), QualType()) // __context with shared vars
1525 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001526 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1527 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001528 break;
1529 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001530 case OMPD_section: {
1531 Sema::CapturedParamNameType Params[] = {
1532 std::make_pair(StringRef(), QualType()) // __context with shared vars
1533 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001534 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1535 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001536 break;
1537 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001538 case OMPD_single: {
1539 Sema::CapturedParamNameType Params[] = {
1540 std::make_pair(StringRef(), QualType()) // __context with shared vars
1541 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001542 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1543 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001544 break;
1545 }
Alexander Musman80c22892014-07-17 08:54:58 +00001546 case OMPD_master: {
1547 Sema::CapturedParamNameType Params[] = {
1548 std::make_pair(StringRef(), QualType()) // __context with shared vars
1549 };
1550 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1551 Params);
1552 break;
1553 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001554 case OMPD_critical: {
1555 Sema::CapturedParamNameType Params[] = {
1556 std::make_pair(StringRef(), QualType()) // __context with shared vars
1557 };
1558 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1559 Params);
1560 break;
1561 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001562 case OMPD_parallel_for: {
1563 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001564 QualType KmpInt32PtrTy =
1565 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001566 Sema::CapturedParamNameType Params[] = {
1567 std::make_pair(".global_tid.", KmpInt32PtrTy),
1568 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1569 std::make_pair(StringRef(), QualType()) // __context with shared vars
1570 };
1571 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1572 Params);
1573 break;
1574 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001575 case OMPD_parallel_for_simd: {
1576 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001577 QualType KmpInt32PtrTy =
1578 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001579 Sema::CapturedParamNameType Params[] = {
1580 std::make_pair(".global_tid.", KmpInt32PtrTy),
1581 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1582 std::make_pair(StringRef(), QualType()) // __context with shared vars
1583 };
1584 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1585 Params);
1586 break;
1587 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001588 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001589 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001590 QualType KmpInt32PtrTy =
1591 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001592 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001593 std::make_pair(".global_tid.", KmpInt32PtrTy),
1594 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001595 std::make_pair(StringRef(), QualType()) // __context with shared vars
1596 };
1597 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1598 Params);
1599 break;
1600 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001601 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001602 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001603 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1604 FunctionProtoType::ExtProtoInfo EPI;
1605 EPI.Variadic = true;
1606 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001607 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001608 std::make_pair(".global_tid.", KmpInt32Ty),
1609 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001610 std::make_pair(".privates.",
1611 Context.VoidPtrTy.withConst().withRestrict()),
1612 std::make_pair(
1613 ".copy_fn.",
1614 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001615 std::make_pair(StringRef(), QualType()) // __context with shared vars
1616 };
1617 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1618 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001619 // Mark this captured region as inlined, because we don't use outlined
1620 // function directly.
1621 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1622 AlwaysInlineAttr::CreateImplicit(
1623 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001624 break;
1625 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001626 case OMPD_ordered: {
1627 Sema::CapturedParamNameType Params[] = {
1628 std::make_pair(StringRef(), QualType()) // __context with shared vars
1629 };
1630 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1631 Params);
1632 break;
1633 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001634 case OMPD_atomic: {
1635 Sema::CapturedParamNameType Params[] = {
1636 std::make_pair(StringRef(), QualType()) // __context with shared vars
1637 };
1638 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1639 Params);
1640 break;
1641 }
Michael Wong65f367f2015-07-21 13:44:28 +00001642 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001643 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001644 case OMPD_target_parallel:
1645 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001646 Sema::CapturedParamNameType Params[] = {
1647 std::make_pair(StringRef(), QualType()) // __context with shared vars
1648 };
1649 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1650 Params);
1651 break;
1652 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001653 case OMPD_teams: {
1654 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001655 QualType KmpInt32PtrTy =
1656 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001657 Sema::CapturedParamNameType Params[] = {
1658 std::make_pair(".global_tid.", KmpInt32PtrTy),
1659 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1660 std::make_pair(StringRef(), QualType()) // __context with shared vars
1661 };
1662 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1663 Params);
1664 break;
1665 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001666 case OMPD_taskgroup: {
1667 Sema::CapturedParamNameType Params[] = {
1668 std::make_pair(StringRef(), QualType()) // __context with shared vars
1669 };
1670 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1671 Params);
1672 break;
1673 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001674 case OMPD_taskloop: {
1675 Sema::CapturedParamNameType Params[] = {
1676 std::make_pair(StringRef(), QualType()) // __context with shared vars
1677 };
1678 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1679 Params);
1680 break;
1681 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001682 case OMPD_taskloop_simd: {
1683 Sema::CapturedParamNameType Params[] = {
1684 std::make_pair(StringRef(), QualType()) // __context with shared vars
1685 };
1686 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1687 Params);
1688 break;
1689 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001690 case OMPD_distribute: {
1691 Sema::CapturedParamNameType Params[] = {
1692 std::make_pair(StringRef(), QualType()) // __context with shared vars
1693 };
1694 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1695 Params);
1696 break;
1697 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001698 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001699 case OMPD_taskyield:
1700 case OMPD_barrier:
1701 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001702 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001703 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001704 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001705 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001706 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001707 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001708 case OMPD_declare_simd:
Alexey Bataev9959db52014-05-06 10:08:46 +00001709 llvm_unreachable("OpenMP Directive is not allowed");
1710 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001711 llvm_unreachable("Unknown OpenMP directive");
1712 }
1713}
1714
Alexey Bataev3392d762016-02-16 11:18:12 +00001715static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001716 Expr *CaptureExpr, bool WithInit,
1717 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001718 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001719 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001720 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001721 QualType Ty = Init->getType();
1722 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1723 if (S.getLangOpts().CPlusPlus)
1724 Ty = C.getLValueReferenceType(Ty);
1725 else {
1726 Ty = C.getPointerType(Ty);
1727 ExprResult Res =
1728 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1729 if (!Res.isUsable())
1730 return nullptr;
1731 Init = Res.get();
1732 }
Alexey Bataev61205072016-03-02 04:57:40 +00001733 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001734 }
1735 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001736 if (!WithInit)
1737 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001738 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001739 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1740 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001741 return CED;
1742}
1743
Alexey Bataev61205072016-03-02 04:57:40 +00001744static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1745 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001746 OMPCapturedExprDecl *CD;
1747 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1748 CD = cast<OMPCapturedExprDecl>(VD);
1749 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001750 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1751 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001752 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001753 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001754}
1755
Alexey Bataev5a3af132016-03-29 08:58:54 +00001756static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1757 if (!Ref) {
1758 auto *CD =
1759 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1760 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1761 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1762 CaptureExpr->getExprLoc());
1763 }
1764 ExprResult Res = Ref;
1765 if (!S.getLangOpts().CPlusPlus &&
1766 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1767 Ref->getType()->isPointerType())
1768 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1769 if (!Res.isUsable())
1770 return ExprError();
1771 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001772}
1773
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001774StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1775 ArrayRef<OMPClause *> Clauses) {
1776 if (!S.isUsable()) {
1777 ActOnCapturedRegionError();
1778 return StmtError();
1779 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001780
1781 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001782 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001783 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001784 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001785 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001786 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001787 Clause->getClauseKind() == OMPC_copyprivate ||
1788 (getLangOpts().OpenMPUseTLS &&
1789 getASTContext().getTargetInfo().isTLSSupported() &&
1790 Clause->getClauseKind() == OMPC_copyin)) {
1791 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001792 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001793 for (auto *VarRef : Clause->children()) {
1794 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001795 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001796 }
1797 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001798 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001799 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001800 // Mark all variables in private list clauses as used in inner region.
1801 // Required for proper codegen of combined directives.
1802 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001803 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001804 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1805 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001806 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1807 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001808 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001809 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1810 if (auto *E = C->getPostUpdateExpr())
1811 MarkDeclarationsReferencedInExpr(E);
1812 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001813 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001814 if (Clause->getClauseKind() == OMPC_schedule)
1815 SC = cast<OMPScheduleClause>(Clause);
1816 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001817 OC = cast<OMPOrderedClause>(Clause);
1818 else if (Clause->getClauseKind() == OMPC_linear)
1819 LCs.push_back(cast<OMPLinearClause>(Clause));
1820 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001821 bool ErrorFound = false;
1822 // OpenMP, 2.7.1 Loop Construct, Restrictions
1823 // The nonmonotonic modifier cannot be specified if an ordered clause is
1824 // specified.
1825 if (SC &&
1826 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1827 SC->getSecondScheduleModifier() ==
1828 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1829 OC) {
1830 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1831 ? SC->getFirstScheduleModifierLoc()
1832 : SC->getSecondScheduleModifierLoc(),
1833 diag::err_omp_schedule_nonmonotonic_ordered)
1834 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1835 ErrorFound = true;
1836 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001837 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1838 for (auto *C : LCs) {
1839 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1840 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1841 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001842 ErrorFound = true;
1843 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001844 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1845 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1846 OC->getNumForLoops()) {
1847 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1848 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1849 ErrorFound = true;
1850 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001851 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001852 ActOnCapturedRegionError();
1853 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001854 }
1855 return ActOnCapturedRegionEnd(S.get());
1856}
1857
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001858static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1859 OpenMPDirectiveKind CurrentRegion,
1860 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001861 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001862 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001863 // Allowed nesting of constructs
1864 // +------------------+-----------------+------------------------------------+
1865 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1866 // +------------------+-----------------+------------------------------------+
1867 // | parallel | parallel | * |
1868 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001869 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001870 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001871 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001872 // | parallel | simd | * |
1873 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001874 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001875 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001876 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001877 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001878 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001879 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001880 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001881 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001882 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001883 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001884 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001885 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001886 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001887 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001888 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001889 // | parallel | target parallel | * |
1890 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001891 // | parallel | target enter | * |
1892 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001893 // | parallel | target exit | * |
1894 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001895 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001896 // | parallel | cancellation | |
1897 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001898 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001899 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001900 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001901 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001902 // +------------------+-----------------+------------------------------------+
1903 // | for | parallel | * |
1904 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001905 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001906 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001907 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001908 // | for | simd | * |
1909 // | for | sections | + |
1910 // | for | section | + |
1911 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001912 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001913 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001914 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001915 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001916 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001917 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001918 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001919 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001920 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001921 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001922 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001923 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001924 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001925 // | for | target parallel | * |
1926 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001927 // | for | target enter | * |
1928 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001929 // | for | target exit | * |
1930 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001931 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001932 // | for | cancellation | |
1933 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001934 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001935 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001936 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001937 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001938 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001939 // | master | parallel | * |
1940 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001941 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001942 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001943 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001944 // | master | simd | * |
1945 // | master | sections | + |
1946 // | master | section | + |
1947 // | master | single | + |
1948 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001949 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001950 // | master |parallel sections| * |
1951 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001952 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001953 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001954 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001955 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001956 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001957 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001958 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001959 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001960 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001961 // | master | target parallel | * |
1962 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001963 // | master | target enter | * |
1964 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001965 // | master | target exit | * |
1966 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001967 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001968 // | master | cancellation | |
1969 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001970 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001971 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001972 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001973 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001974 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001975 // | critical | parallel | * |
1976 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001977 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001978 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001979 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001980 // | critical | simd | * |
1981 // | critical | sections | + |
1982 // | critical | section | + |
1983 // | critical | single | + |
1984 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001985 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001986 // | critical |parallel sections| * |
1987 // | critical | task | * |
1988 // | critical | taskyield | * |
1989 // | critical | barrier | + |
1990 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001991 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001992 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001993 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001994 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001995 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001996 // | critical | target parallel | * |
1997 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001998 // | critical | target enter | * |
1999 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002000 // | critical | target exit | * |
2001 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002002 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002003 // | critical | cancellation | |
2004 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002005 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002006 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002007 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002008 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002009 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002010 // | simd | parallel | |
2011 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002012 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002013 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002014 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002015 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002016 // | simd | sections | |
2017 // | simd | section | |
2018 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002019 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002020 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002021 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002022 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002023 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002024 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002025 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002026 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002027 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002028 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002029 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002030 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002031 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002032 // | simd | target parallel | |
2033 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002034 // | simd | target enter | |
2035 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002036 // | simd | target exit | |
2037 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002038 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002039 // | simd | cancellation | |
2040 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002041 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002042 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002043 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002044 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002045 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002046 // | for simd | parallel | |
2047 // | for simd | for | |
2048 // | for simd | for simd | |
2049 // | for simd | master | |
2050 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002051 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002052 // | for simd | sections | |
2053 // | for simd | section | |
2054 // | for simd | single | |
2055 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002056 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002057 // | for simd |parallel sections| |
2058 // | for simd | task | |
2059 // | for simd | taskyield | |
2060 // | for simd | barrier | |
2061 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002062 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002063 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002064 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002065 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002066 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002067 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002068 // | for simd | target parallel | |
2069 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002070 // | for simd | target enter | |
2071 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002072 // | for simd | target exit | |
2073 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002074 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002075 // | for simd | cancellation | |
2076 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002077 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002078 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002079 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002080 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002081 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002082 // | parallel for simd| parallel | |
2083 // | parallel for simd| for | |
2084 // | parallel for simd| for simd | |
2085 // | parallel for simd| master | |
2086 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002087 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002088 // | parallel for simd| sections | |
2089 // | parallel for simd| section | |
2090 // | parallel for simd| single | |
2091 // | parallel for simd| parallel for | |
2092 // | parallel for simd|parallel for simd| |
2093 // | parallel for simd|parallel sections| |
2094 // | parallel for simd| task | |
2095 // | parallel for simd| taskyield | |
2096 // | parallel for simd| barrier | |
2097 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002098 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002099 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002100 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002101 // | parallel for simd| atomic | |
2102 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002103 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002104 // | parallel for simd| target parallel | |
2105 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002106 // | parallel for simd| target enter | |
2107 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002108 // | parallel for simd| target exit | |
2109 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002110 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002111 // | parallel for simd| cancellation | |
2112 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002113 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002114 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002115 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002116 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002117 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002118 // | sections | parallel | * |
2119 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002120 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002121 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002122 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002123 // | sections | simd | * |
2124 // | sections | sections | + |
2125 // | sections | section | * |
2126 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002127 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002128 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002129 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002130 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002131 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002132 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002133 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002134 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002135 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002136 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002137 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002138 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002139 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002140 // | sections | target parallel | * |
2141 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002142 // | sections | target enter | * |
2143 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002144 // | sections | target exit | * |
2145 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002146 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002147 // | sections | cancellation | |
2148 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002149 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002150 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002151 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002152 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002153 // +------------------+-----------------+------------------------------------+
2154 // | section | parallel | * |
2155 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002156 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002157 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002158 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002159 // | section | simd | * |
2160 // | section | sections | + |
2161 // | section | section | + |
2162 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002163 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002164 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002165 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002166 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002167 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002168 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002169 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002170 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002171 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002172 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002173 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002174 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002175 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002176 // | section | target parallel | * |
2177 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002178 // | section | target enter | * |
2179 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002180 // | section | target exit | * |
2181 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002182 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002183 // | section | cancellation | |
2184 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002185 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002186 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002187 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002188 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002189 // +------------------+-----------------+------------------------------------+
2190 // | single | parallel | * |
2191 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002192 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002193 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002194 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002195 // | single | simd | * |
2196 // | single | sections | + |
2197 // | single | section | + |
2198 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002199 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002200 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002201 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002202 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002203 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002204 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002205 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002206 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002207 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002208 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002209 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002210 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002211 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002212 // | single | target parallel | * |
2213 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002214 // | single | target enter | * |
2215 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002216 // | single | target exit | * |
2217 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002218 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002219 // | single | cancellation | |
2220 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002221 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002222 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002223 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002224 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002225 // +------------------+-----------------+------------------------------------+
2226 // | parallel for | parallel | * |
2227 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002228 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002229 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002230 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002231 // | parallel for | simd | * |
2232 // | parallel for | sections | + |
2233 // | parallel for | section | + |
2234 // | parallel for | single | + |
2235 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002236 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002237 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002238 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002239 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002240 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002241 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002242 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002243 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002244 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002245 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002246 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002247 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002248 // | parallel for | target parallel | * |
2249 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002250 // | parallel for | target enter | * |
2251 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002252 // | parallel for | target exit | * |
2253 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002254 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002255 // | parallel for | cancellation | |
2256 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002257 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002258 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002259 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002260 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002261 // +------------------+-----------------+------------------------------------+
2262 // | parallel sections| parallel | * |
2263 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002264 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002265 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002266 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002267 // | parallel sections| simd | * |
2268 // | parallel sections| sections | + |
2269 // | parallel sections| section | * |
2270 // | parallel sections| single | + |
2271 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002272 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002273 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002274 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002275 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002276 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002277 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002278 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002279 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002280 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002281 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002282 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002283 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002284 // | parallel sections| target parallel | * |
2285 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002286 // | parallel sections| target enter | * |
2287 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002288 // | parallel sections| target exit | * |
2289 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002290 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002291 // | parallel sections| cancellation | |
2292 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002293 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002294 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002295 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002296 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002297 // +------------------+-----------------+------------------------------------+
2298 // | task | parallel | * |
2299 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002300 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002301 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002302 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002303 // | task | simd | * |
2304 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002305 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002306 // | task | single | + |
2307 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002308 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002309 // | task |parallel sections| * |
2310 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002311 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002312 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002313 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002314 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002315 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002316 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002317 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002318 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002319 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002320 // | task | target parallel | * |
2321 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002322 // | task | target enter | * |
2323 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002324 // | task | target exit | * |
2325 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002326 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002327 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002328 // | | point | ! |
2329 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002330 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002331 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002332 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002333 // +------------------+-----------------+------------------------------------+
2334 // | ordered | parallel | * |
2335 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002336 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002337 // | ordered | master | * |
2338 // | ordered | critical | * |
2339 // | ordered | simd | * |
2340 // | ordered | sections | + |
2341 // | ordered | section | + |
2342 // | ordered | single | + |
2343 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002344 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002345 // | ordered |parallel sections| * |
2346 // | ordered | task | * |
2347 // | ordered | taskyield | * |
2348 // | ordered | barrier | + |
2349 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002350 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002351 // | ordered | flush | * |
2352 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002353 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002354 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002355 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002356 // | ordered | target parallel | * |
2357 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002358 // | ordered | target enter | * |
2359 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002360 // | ordered | target exit | * |
2361 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002362 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002363 // | ordered | cancellation | |
2364 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002365 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002366 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002367 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002368 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002369 // +------------------+-----------------+------------------------------------+
2370 // | atomic | parallel | |
2371 // | atomic | for | |
2372 // | atomic | for simd | |
2373 // | atomic | master | |
2374 // | atomic | critical | |
2375 // | atomic | simd | |
2376 // | atomic | sections | |
2377 // | atomic | section | |
2378 // | atomic | single | |
2379 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002380 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002381 // | atomic |parallel sections| |
2382 // | atomic | task | |
2383 // | atomic | taskyield | |
2384 // | atomic | barrier | |
2385 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002386 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002387 // | atomic | flush | |
2388 // | atomic | ordered | |
2389 // | atomic | atomic | |
2390 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002391 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002392 // | atomic | target parallel | |
2393 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002394 // | atomic | target enter | |
2395 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002396 // | atomic | target exit | |
2397 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002398 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002399 // | atomic | cancellation | |
2400 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002401 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002402 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002403 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002404 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002405 // +------------------+-----------------+------------------------------------+
2406 // | target | parallel | * |
2407 // | target | for | * |
2408 // | target | for simd | * |
2409 // | target | master | * |
2410 // | target | critical | * |
2411 // | target | simd | * |
2412 // | target | sections | * |
2413 // | target | section | * |
2414 // | target | single | * |
2415 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002416 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002417 // | target |parallel sections| * |
2418 // | target | task | * |
2419 // | target | taskyield | * |
2420 // | target | barrier | * |
2421 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002422 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002423 // | target | flush | * |
2424 // | target | ordered | * |
2425 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002426 // | target | target | |
2427 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002428 // | target | target parallel | |
2429 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002430 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002431 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002432 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002433 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002434 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002435 // | target | cancellation | |
2436 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002437 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002438 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002439 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002440 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002441 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002442 // | target parallel | parallel | * |
2443 // | target parallel | for | * |
2444 // | target parallel | for simd | * |
2445 // | target parallel | master | * |
2446 // | target parallel | critical | * |
2447 // | target parallel | simd | * |
2448 // | target parallel | sections | * |
2449 // | target parallel | section | * |
2450 // | target parallel | single | * |
2451 // | target parallel | parallel for | * |
2452 // | target parallel |parallel for simd| * |
2453 // | target parallel |parallel sections| * |
2454 // | target parallel | task | * |
2455 // | target parallel | taskyield | * |
2456 // | target parallel | barrier | * |
2457 // | target parallel | taskwait | * |
2458 // | target parallel | taskgroup | * |
2459 // | target parallel | flush | * |
2460 // | target parallel | ordered | * |
2461 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002462 // | target parallel | target | |
2463 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002464 // | target parallel | target parallel | |
2465 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002466 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002467 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002468 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002469 // | | data | |
2470 // | target parallel | teams | |
2471 // | target parallel | cancellation | |
2472 // | | point | ! |
2473 // | target parallel | cancel | ! |
2474 // | target parallel | taskloop | * |
2475 // | target parallel | taskloop simd | * |
2476 // | target parallel | distribute | |
2477 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002478 // | target parallel | parallel | * |
2479 // | for | | |
2480 // | target parallel | for | * |
2481 // | for | | |
2482 // | target parallel | for simd | * |
2483 // | for | | |
2484 // | target parallel | master | * |
2485 // | for | | |
2486 // | target parallel | critical | * |
2487 // | for | | |
2488 // | target parallel | simd | * |
2489 // | for | | |
2490 // | target parallel | sections | * |
2491 // | for | | |
2492 // | target parallel | section | * |
2493 // | for | | |
2494 // | target parallel | single | * |
2495 // | for | | |
2496 // | target parallel | parallel for | * |
2497 // | for | | |
2498 // | target parallel |parallel for simd| * |
2499 // | for | | |
2500 // | target parallel |parallel sections| * |
2501 // | for | | |
2502 // | target parallel | task | * |
2503 // | for | | |
2504 // | target parallel | taskyield | * |
2505 // | for | | |
2506 // | target parallel | barrier | * |
2507 // | for | | |
2508 // | target parallel | taskwait | * |
2509 // | for | | |
2510 // | target parallel | taskgroup | * |
2511 // | for | | |
2512 // | target parallel | flush | * |
2513 // | for | | |
2514 // | target parallel | ordered | * |
2515 // | for | | |
2516 // | target parallel | atomic | * |
2517 // | for | | |
2518 // | target parallel | target | |
2519 // | for | | |
2520 // | target parallel | target parallel | |
2521 // | for | | |
2522 // | target parallel | target parallel | |
2523 // | for | for | |
2524 // | target parallel | target enter | |
2525 // | for | data | |
2526 // | target parallel | target exit | |
2527 // | for | data | |
2528 // | target parallel | teams | |
2529 // | for | | |
2530 // | target parallel | cancellation | |
2531 // | for | point | ! |
2532 // | target parallel | cancel | ! |
2533 // | for | | |
2534 // | target parallel | taskloop | * |
2535 // | for | | |
2536 // | target parallel | taskloop simd | * |
2537 // | for | | |
2538 // | target parallel | distribute | |
2539 // | for | | |
2540 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002541 // | teams | parallel | * |
2542 // | teams | for | + |
2543 // | teams | for simd | + |
2544 // | teams | master | + |
2545 // | teams | critical | + |
2546 // | teams | simd | + |
2547 // | teams | sections | + |
2548 // | teams | section | + |
2549 // | teams | single | + |
2550 // | teams | parallel for | * |
2551 // | teams |parallel for simd| * |
2552 // | teams |parallel sections| * |
2553 // | teams | task | + |
2554 // | teams | taskyield | + |
2555 // | teams | barrier | + |
2556 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002557 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002558 // | teams | flush | + |
2559 // | teams | ordered | + |
2560 // | teams | atomic | + |
2561 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002562 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002563 // | teams | target parallel | + |
2564 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002565 // | teams | target enter | + |
2566 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002567 // | teams | target exit | + |
2568 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002569 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002570 // | teams | cancellation | |
2571 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002572 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002573 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002574 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002575 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002576 // +------------------+-----------------+------------------------------------+
2577 // | taskloop | parallel | * |
2578 // | taskloop | for | + |
2579 // | taskloop | for simd | + |
2580 // | taskloop | master | + |
2581 // | taskloop | critical | * |
2582 // | taskloop | simd | * |
2583 // | taskloop | sections | + |
2584 // | taskloop | section | + |
2585 // | taskloop | single | + |
2586 // | taskloop | parallel for | * |
2587 // | taskloop |parallel for simd| * |
2588 // | taskloop |parallel sections| * |
2589 // | taskloop | task | * |
2590 // | taskloop | taskyield | * |
2591 // | taskloop | barrier | + |
2592 // | taskloop | taskwait | * |
2593 // | taskloop | taskgroup | * |
2594 // | taskloop | flush | * |
2595 // | taskloop | ordered | + |
2596 // | taskloop | atomic | * |
2597 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002598 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002599 // | taskloop | target parallel | * |
2600 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002601 // | taskloop | target enter | * |
2602 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002603 // | taskloop | target exit | * |
2604 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002605 // | taskloop | teams | + |
2606 // | taskloop | cancellation | |
2607 // | | point | |
2608 // | taskloop | cancel | |
2609 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002610 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002611 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002612 // | taskloop simd | parallel | |
2613 // | taskloop simd | for | |
2614 // | taskloop simd | for simd | |
2615 // | taskloop simd | master | |
2616 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002617 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002618 // | taskloop simd | sections | |
2619 // | taskloop simd | section | |
2620 // | taskloop simd | single | |
2621 // | taskloop simd | parallel for | |
2622 // | taskloop simd |parallel for simd| |
2623 // | taskloop simd |parallel sections| |
2624 // | taskloop simd | task | |
2625 // | taskloop simd | taskyield | |
2626 // | taskloop simd | barrier | |
2627 // | taskloop simd | taskwait | |
2628 // | taskloop simd | taskgroup | |
2629 // | taskloop simd | flush | |
2630 // | taskloop simd | ordered | + (with simd clause) |
2631 // | taskloop simd | atomic | |
2632 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002633 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002634 // | taskloop simd | target parallel | |
2635 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002636 // | taskloop simd | target enter | |
2637 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002638 // | taskloop simd | target exit | |
2639 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002640 // | taskloop simd | teams | |
2641 // | taskloop simd | cancellation | |
2642 // | | point | |
2643 // | taskloop simd | cancel | |
2644 // | taskloop simd | taskloop | |
2645 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002646 // | taskloop simd | distribute | |
2647 // +------------------+-----------------+------------------------------------+
2648 // | distribute | parallel | * |
2649 // | distribute | for | * |
2650 // | distribute | for simd | * |
2651 // | distribute | master | * |
2652 // | distribute | critical | * |
2653 // | distribute | simd | * |
2654 // | distribute | sections | * |
2655 // | distribute | section | * |
2656 // | distribute | single | * |
2657 // | distribute | parallel for | * |
2658 // | distribute |parallel for simd| * |
2659 // | distribute |parallel sections| * |
2660 // | distribute | task | * |
2661 // | distribute | taskyield | * |
2662 // | distribute | barrier | * |
2663 // | distribute | taskwait | * |
2664 // | distribute | taskgroup | * |
2665 // | distribute | flush | * |
2666 // | distribute | ordered | + |
2667 // | distribute | atomic | * |
2668 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002669 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002670 // | distribute | target parallel | |
2671 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002672 // | distribute | target enter | |
2673 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002674 // | distribute | target exit | |
2675 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002676 // | distribute | teams | |
2677 // | distribute | cancellation | + |
2678 // | | point | |
2679 // | distribute | cancel | + |
2680 // | distribute | taskloop | * |
2681 // | distribute | taskloop simd | * |
2682 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002683 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002684 if (Stack->getCurScope()) {
2685 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002686 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002687 bool NestingProhibited = false;
2688 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002689 enum {
2690 NoRecommend,
2691 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002692 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002693 ShouldBeInTargetRegion,
2694 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002695 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002696 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2697 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002698 // OpenMP [2.16, Nesting of Regions]
2699 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002700 // OpenMP [2.8.1,simd Construct, Restrictions]
2701 // An ordered construct with the simd clause is the only OpenMP construct
2702 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002703 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2704 return true;
2705 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002706 if (ParentRegion == OMPD_atomic) {
2707 // OpenMP [2.16, Nesting of Regions]
2708 // OpenMP constructs may not be nested inside an atomic region.
2709 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2710 return true;
2711 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002712 if (CurrentRegion == OMPD_section) {
2713 // OpenMP [2.7.2, sections Construct, Restrictions]
2714 // Orphaned section directives are prohibited. That is, the section
2715 // directives must appear within the sections construct and must not be
2716 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002717 if (ParentRegion != OMPD_sections &&
2718 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002719 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2720 << (ParentRegion != OMPD_unknown)
2721 << getOpenMPDirectiveName(ParentRegion);
2722 return true;
2723 }
2724 return false;
2725 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002726 // Allow some constructs to be orphaned (they could be used in functions,
2727 // called from OpenMP regions with the required preconditions).
2728 if (ParentRegion == OMPD_unknown)
2729 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002730 if (CurrentRegion == OMPD_cancellation_point ||
2731 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002732 // OpenMP [2.16, Nesting of Regions]
2733 // A cancellation point construct for which construct-type-clause is
2734 // taskgroup must be nested inside a task construct. A cancellation
2735 // point construct for which construct-type-clause is not taskgroup must
2736 // be closely nested inside an OpenMP construct that matches the type
2737 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002738 // A cancel construct for which construct-type-clause is taskgroup must be
2739 // nested inside a task construct. A cancel construct for which
2740 // construct-type-clause is not taskgroup must be closely nested inside an
2741 // OpenMP construct that matches the type specified in
2742 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002743 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002744 !((CancelRegion == OMPD_parallel &&
2745 (ParentRegion == OMPD_parallel ||
2746 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002747 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002748 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2749 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002750 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2751 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002752 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2753 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002754 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002755 // OpenMP [2.16, Nesting of Regions]
2756 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002757 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002758 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002759 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002760 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002761 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2762 // OpenMP [2.16, Nesting of Regions]
2763 // A critical region may not be nested (closely or otherwise) inside a
2764 // critical region with the same name. Note that this restriction is not
2765 // sufficient to prevent deadlock.
2766 SourceLocation PreviousCriticalLoc;
2767 bool DeadLock =
2768 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2769 OpenMPDirectiveKind K,
2770 const DeclarationNameInfo &DNI,
2771 SourceLocation Loc)
2772 ->bool {
2773 if (K == OMPD_critical &&
2774 DNI.getName() == CurrentName.getName()) {
2775 PreviousCriticalLoc = Loc;
2776 return true;
2777 } else
2778 return false;
2779 },
2780 false /* skip top directive */);
2781 if (DeadLock) {
2782 SemaRef.Diag(StartLoc,
2783 diag::err_omp_prohibited_region_critical_same_name)
2784 << CurrentName.getName();
2785 if (PreviousCriticalLoc.isValid())
2786 SemaRef.Diag(PreviousCriticalLoc,
2787 diag::note_omp_previous_critical_region);
2788 return true;
2789 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002790 } else if (CurrentRegion == OMPD_barrier) {
2791 // OpenMP [2.16, Nesting of Regions]
2792 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002793 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002794 NestingProhibited =
2795 isOpenMPWorksharingDirective(ParentRegion) ||
2796 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002797 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002798 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002799 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002800 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002801 // OpenMP [2.16, Nesting of Regions]
2802 // A worksharing region may not be closely nested inside a worksharing,
2803 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002804 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002805 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002806 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002807 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002808 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002809 Recommend = ShouldBeInParallelRegion;
2810 } else if (CurrentRegion == OMPD_ordered) {
2811 // OpenMP [2.16, Nesting of Regions]
2812 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002813 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002814 // An ordered region must be closely nested inside a loop region (or
2815 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002816 // OpenMP [2.8.1,simd Construct, Restrictions]
2817 // An ordered construct with the simd clause is the only OpenMP construct
2818 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002819 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002820 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002821 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002822 !(isOpenMPSimdDirective(ParentRegion) ||
2823 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002824 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002825 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2826 // OpenMP [2.16, Nesting of Regions]
2827 // If specified, a teams construct must be contained within a target
2828 // construct.
2829 NestingProhibited = ParentRegion != OMPD_target;
2830 Recommend = ShouldBeInTargetRegion;
2831 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2832 }
2833 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2834 // OpenMP [2.16, Nesting of Regions]
2835 // distribute, parallel, parallel sections, parallel workshare, and the
2836 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2837 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002838 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2839 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002840 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002841 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002842 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2843 // OpenMP 4.5 [2.17 Nesting of Regions]
2844 // The region associated with the distribute construct must be strictly
2845 // nested inside a teams region
2846 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2847 Recommend = ShouldBeInTeamsRegion;
2848 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002849 if (!NestingProhibited &&
2850 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2851 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2852 // OpenMP 4.5 [2.17 Nesting of Regions]
2853 // If a target, target update, target data, target enter data, or
2854 // target exit data construct is encountered during execution of a
2855 // target region, the behavior is unspecified.
2856 NestingProhibited = Stack->hasDirective(
2857 [&OffendingRegion](OpenMPDirectiveKind K,
2858 const DeclarationNameInfo &DNI,
2859 SourceLocation Loc) -> bool {
2860 if (isOpenMPTargetExecutionDirective(K)) {
2861 OffendingRegion = K;
2862 return true;
2863 } else
2864 return false;
2865 },
2866 false /* don't skip top directive */);
2867 CloseNesting = false;
2868 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002869 if (NestingProhibited) {
2870 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002871 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2872 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002873 return true;
2874 }
2875 }
2876 return false;
2877}
2878
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002879static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2880 ArrayRef<OMPClause *> Clauses,
2881 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2882 bool ErrorFound = false;
2883 unsigned NamedModifiersNumber = 0;
2884 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2885 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002886 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002887 for (const auto *C : Clauses) {
2888 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2889 // At most one if clause without a directive-name-modifier can appear on
2890 // the directive.
2891 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2892 if (FoundNameModifiers[CurNM]) {
2893 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2894 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2895 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2896 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002897 } else if (CurNM != OMPD_unknown) {
2898 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002899 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002900 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002901 FoundNameModifiers[CurNM] = IC;
2902 if (CurNM == OMPD_unknown)
2903 continue;
2904 // Check if the specified name modifier is allowed for the current
2905 // directive.
2906 // At most one if clause with the particular directive-name-modifier can
2907 // appear on the directive.
2908 bool MatchFound = false;
2909 for (auto NM : AllowedNameModifiers) {
2910 if (CurNM == NM) {
2911 MatchFound = true;
2912 break;
2913 }
2914 }
2915 if (!MatchFound) {
2916 S.Diag(IC->getNameModifierLoc(),
2917 diag::err_omp_wrong_if_directive_name_modifier)
2918 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2919 ErrorFound = true;
2920 }
2921 }
2922 }
2923 // If any if clause on the directive includes a directive-name-modifier then
2924 // all if clauses on the directive must include a directive-name-modifier.
2925 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2926 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2927 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2928 diag::err_omp_no_more_if_clause);
2929 } else {
2930 std::string Values;
2931 std::string Sep(", ");
2932 unsigned AllowedCnt = 0;
2933 unsigned TotalAllowedNum =
2934 AllowedNameModifiers.size() - NamedModifiersNumber;
2935 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2936 ++Cnt) {
2937 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2938 if (!FoundNameModifiers[NM]) {
2939 Values += "'";
2940 Values += getOpenMPDirectiveName(NM);
2941 Values += "'";
2942 if (AllowedCnt + 2 == TotalAllowedNum)
2943 Values += " or ";
2944 else if (AllowedCnt + 1 != TotalAllowedNum)
2945 Values += Sep;
2946 ++AllowedCnt;
2947 }
2948 }
2949 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2950 diag::err_omp_unnamed_if_clause)
2951 << (TotalAllowedNum > 1) << Values;
2952 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002953 for (auto Loc : NameModifierLoc) {
2954 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2955 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002956 ErrorFound = true;
2957 }
2958 return ErrorFound;
2959}
2960
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002961StmtResult Sema::ActOnOpenMPExecutableDirective(
2962 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2963 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2964 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002965 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002966 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2967 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002968 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002969
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002970 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002971 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002972 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002973 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002974 if (AStmt) {
2975 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2976
2977 // Check default data sharing attributes for referenced variables.
2978 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2979 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2980 if (DSAChecker.isErrorFound())
2981 return StmtError();
2982 // Generate list of implicitly defined firstprivate variables.
2983 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002984
2985 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2986 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2987 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2988 SourceLocation(), SourceLocation())) {
2989 ClausesWithImplicit.push_back(Implicit);
2990 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2991 DSAChecker.getImplicitFirstprivate().size();
2992 } else
2993 ErrorFound = true;
2994 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002995 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002996
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002997 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002998 switch (Kind) {
2999 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003000 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3001 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003002 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003003 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003004 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003005 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3006 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003007 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003008 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003009 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3010 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003011 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003012 case OMPD_for_simd:
3013 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3014 EndLoc, VarsWithInheritedDSA);
3015 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003016 case OMPD_sections:
3017 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3018 EndLoc);
3019 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003020 case OMPD_section:
3021 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003022 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003023 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3024 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003025 case OMPD_single:
3026 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3027 EndLoc);
3028 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003029 case OMPD_master:
3030 assert(ClausesWithImplicit.empty() &&
3031 "No clauses are allowed for 'omp master' directive");
3032 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3033 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003034 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003035 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3036 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003037 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003038 case OMPD_parallel_for:
3039 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3040 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003041 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003042 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003043 case OMPD_parallel_for_simd:
3044 Res = ActOnOpenMPParallelForSimdDirective(
3045 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003046 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003047 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003048 case OMPD_parallel_sections:
3049 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3050 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003051 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003052 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003053 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003054 Res =
3055 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003056 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003057 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003058 case OMPD_taskyield:
3059 assert(ClausesWithImplicit.empty() &&
3060 "No clauses are allowed for 'omp taskyield' directive");
3061 assert(AStmt == nullptr &&
3062 "No associated statement allowed for 'omp taskyield' directive");
3063 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3064 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003065 case OMPD_barrier:
3066 assert(ClausesWithImplicit.empty() &&
3067 "No clauses are allowed for 'omp barrier' directive");
3068 assert(AStmt == nullptr &&
3069 "No associated statement allowed for 'omp barrier' directive");
3070 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3071 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003072 case OMPD_taskwait:
3073 assert(ClausesWithImplicit.empty() &&
3074 "No clauses are allowed for 'omp taskwait' directive");
3075 assert(AStmt == nullptr &&
3076 "No associated statement allowed for 'omp taskwait' directive");
3077 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3078 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003079 case OMPD_taskgroup:
3080 assert(ClausesWithImplicit.empty() &&
3081 "No clauses are allowed for 'omp taskgroup' directive");
3082 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3083 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003084 case OMPD_flush:
3085 assert(AStmt == nullptr &&
3086 "No associated statement allowed for 'omp flush' directive");
3087 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3088 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003089 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003090 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3091 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003092 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003093 case OMPD_atomic:
3094 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3095 EndLoc);
3096 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003097 case OMPD_teams:
3098 Res =
3099 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3100 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003101 case OMPD_target:
3102 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3103 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003104 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003105 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003106 case OMPD_target_parallel:
3107 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3108 StartLoc, EndLoc);
3109 AllowedNameModifiers.push_back(OMPD_target);
3110 AllowedNameModifiers.push_back(OMPD_parallel);
3111 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003112 case OMPD_target_parallel_for:
3113 Res = ActOnOpenMPTargetParallelForDirective(
3114 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3115 AllowedNameModifiers.push_back(OMPD_target);
3116 AllowedNameModifiers.push_back(OMPD_parallel);
3117 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003118 case OMPD_cancellation_point:
3119 assert(ClausesWithImplicit.empty() &&
3120 "No clauses are allowed for 'omp cancellation point' directive");
3121 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3122 "cancellation point' directive");
3123 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3124 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003125 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003126 assert(AStmt == nullptr &&
3127 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003128 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3129 CancelRegion);
3130 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003131 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003132 case OMPD_target_data:
3133 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3134 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003135 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003136 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003137 case OMPD_target_enter_data:
3138 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3139 EndLoc);
3140 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3141 break;
Samuel Antao72590762016-01-19 20:04:50 +00003142 case OMPD_target_exit_data:
3143 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3144 EndLoc);
3145 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3146 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003147 case OMPD_taskloop:
3148 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3149 EndLoc, VarsWithInheritedDSA);
3150 AllowedNameModifiers.push_back(OMPD_taskloop);
3151 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003152 case OMPD_taskloop_simd:
3153 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3154 EndLoc, VarsWithInheritedDSA);
3155 AllowedNameModifiers.push_back(OMPD_taskloop);
3156 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003157 case OMPD_distribute:
3158 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3159 EndLoc, VarsWithInheritedDSA);
3160 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003161 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003162 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003163 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003164 llvm_unreachable("OpenMP Directive is not allowed");
3165 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003166 llvm_unreachable("Unknown OpenMP directive");
3167 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003168
Alexey Bataev4acb8592014-07-07 13:01:15 +00003169 for (auto P : VarsWithInheritedDSA) {
3170 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3171 << P.first << P.second->getSourceRange();
3172 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003173 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3174
3175 if (!AllowedNameModifiers.empty())
3176 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3177 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003178
Alexey Bataeved09d242014-05-28 05:53:51 +00003179 if (ErrorFound)
3180 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003181 return Res;
3182}
3183
Alexey Bataev587e1de2016-03-30 10:43:55 +00003184Sema::DeclGroupPtrTy
3185Sema::ActOnOpenMPDeclareSimdDirective(DeclGroupPtrTy DG,
3186 SourceLocation StartLoc) {
3187 if (!DG || DG.get().isNull())
3188 return DeclGroupPtrTy();
3189
3190 if (!DG.get().isSingleDecl()) {
3191 Diag(StartLoc, diag::err_omp_single_decl_in_declare_simd);
3192 return DG;
3193 }
3194 auto *ADecl = DG.get().getSingleDecl();
3195 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3196 ADecl = FTD->getTemplatedDecl();
3197
3198 if (!isa<FunctionDecl>(ADecl)) {
3199 Diag(ADecl->getLocation(), diag::err_omp_function_expected)
3200 << ADecl->getDeclContext()->isFileContext();
3201 return DeclGroupPtrTy();
3202 }
3203
3204 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3205 Context, SourceRange(StartLoc, StartLoc));
3206 ADecl->addAttr(NewAttr);
3207 return ConvertDeclToDeclGroup(ADecl);
3208}
3209
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003210StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3211 Stmt *AStmt,
3212 SourceLocation StartLoc,
3213 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003214 if (!AStmt)
3215 return StmtError();
3216
Alexey Bataev9959db52014-05-06 10:08:46 +00003217 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3218 // 1.2.2 OpenMP Language Terminology
3219 // Structured block - An executable statement with a single entry at the
3220 // top and a single exit at the bottom.
3221 // The point of exit cannot be a branch out of the structured block.
3222 // longjmp() and throw() must not violate the entry/exit criteria.
3223 CS->getCapturedDecl()->setNothrow();
3224
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003225 getCurFunction()->setHasBranchProtectedScope();
3226
Alexey Bataev25e5b442015-09-15 12:52:43 +00003227 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3228 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003229}
3230
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003231namespace {
3232/// \brief Helper class for checking canonical form of the OpenMP loops and
3233/// extracting iteration space of each loop in the loop nest, that will be used
3234/// for IR generation.
3235class OpenMPIterationSpaceChecker {
3236 /// \brief Reference to Sema.
3237 Sema &SemaRef;
3238 /// \brief A location for diagnostics (when there is no some better location).
3239 SourceLocation DefaultLoc;
3240 /// \brief A location for diagnostics (when increment is not compatible).
3241 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003242 /// \brief A source location for referring to loop init later.
3243 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003244 /// \brief A source location for referring to condition later.
3245 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003246 /// \brief A source location for referring to increment later.
3247 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003248 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003249 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003250 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003251 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003252 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003253 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003254 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003255 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003256 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003257 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003258 /// \brief This flag is true when condition is one of:
3259 /// Var < UB
3260 /// Var <= UB
3261 /// UB > Var
3262 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003263 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003264 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003265 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003266 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003267 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003268
3269public:
3270 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003271 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003272 /// \brief Check init-expr for canonical loop form and save loop counter
3273 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003274 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003275 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3276 /// for less/greater and for strict/non-strict comparison.
3277 bool CheckCond(Expr *S);
3278 /// \brief Check incr-expr for canonical loop form and return true if it
3279 /// does not conform, otherwise save loop step (#Step).
3280 bool CheckInc(Expr *S);
3281 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003282 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003283 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003284 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003285 /// \brief Source range of the loop init.
3286 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3287 /// \brief Source range of the loop condition.
3288 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3289 /// \brief Source range of the loop increment.
3290 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3291 /// \brief True if the step should be subtracted.
3292 bool ShouldSubtractStep() const { return SubtractStep; }
3293 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003294 Expr *
3295 BuildNumIterations(Scope *S, const bool LimitedType,
3296 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003297 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003298 Expr *BuildPreCond(Scope *S, Expr *Cond,
3299 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003300 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003301 DeclRefExpr *
3302 BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003303 /// \brief Build reference expression to the private counter be used for
3304 /// codegen.
3305 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003306 /// \brief Build initization of the counter be used for codegen.
3307 Expr *BuildCounterInit() const;
3308 /// \brief Build step of the counter be used for codegen.
3309 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003310 /// \brief Return true if any expression is dependent.
3311 bool Dependent() const;
3312
3313private:
3314 /// \brief Check the right-hand side of an assignment in the increment
3315 /// expression.
3316 bool CheckIncRHS(Expr *RHS);
3317 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003318 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003319 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003320 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003321 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003322 /// \brief Helper to set loop increment.
3323 bool SetStep(Expr *NewStep, bool Subtract);
3324};
3325
3326bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003327 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003328 assert(!LB && !UB && !Step);
3329 return false;
3330 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003331 return LCDecl->getType()->isDependentType() ||
3332 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3333 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003334}
3335
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003336static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003337 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3338 E = ExprTemp->getSubExpr();
3339
3340 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3341 E = MTE->GetTemporaryExpr();
3342
3343 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3344 E = Binder->getSubExpr();
3345
3346 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3347 E = ICE->getSubExprAsWritten();
3348 return E->IgnoreParens();
3349}
3350
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003351bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3352 Expr *NewLCRefExpr,
3353 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003354 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003355 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003356 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003357 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003358 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003359 LCDecl = getCanonicalDecl(NewLCDecl);
3360 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003361 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3362 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003363 if ((Ctor->isCopyOrMoveConstructor() ||
3364 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3365 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003366 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003367 LB = NewLB;
3368 return false;
3369}
3370
3371bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003372 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003373 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003374 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3375 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003376 if (!NewUB)
3377 return true;
3378 UB = NewUB;
3379 TestIsLessOp = LessOp;
3380 TestIsStrictOp = StrictOp;
3381 ConditionSrcRange = SR;
3382 ConditionLoc = SL;
3383 return false;
3384}
3385
3386bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3387 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003388 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003389 if (!NewStep)
3390 return true;
3391 if (!NewStep->isValueDependent()) {
3392 // Check that the step is integer expression.
3393 SourceLocation StepLoc = NewStep->getLocStart();
3394 ExprResult Val =
3395 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3396 if (Val.isInvalid())
3397 return true;
3398 NewStep = Val.get();
3399
3400 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3401 // If test-expr is of form var relational-op b and relational-op is < or
3402 // <= then incr-expr must cause var to increase on each iteration of the
3403 // loop. If test-expr is of form var relational-op b and relational-op is
3404 // > or >= then incr-expr must cause var to decrease on each iteration of
3405 // the loop.
3406 // If test-expr is of form b relational-op var and relational-op is < or
3407 // <= then incr-expr must cause var to decrease on each iteration of the
3408 // loop. If test-expr is of form b relational-op var and relational-op is
3409 // > or >= then incr-expr must cause var to increase on each iteration of
3410 // the loop.
3411 llvm::APSInt Result;
3412 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3413 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3414 bool IsConstNeg =
3415 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003416 bool IsConstPos =
3417 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003418 bool IsConstZero = IsConstant && !Result.getBoolValue();
3419 if (UB && (IsConstZero ||
3420 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003421 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003422 SemaRef.Diag(NewStep->getExprLoc(),
3423 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003424 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003425 SemaRef.Diag(ConditionLoc,
3426 diag::note_omp_loop_cond_requres_compatible_incr)
3427 << TestIsLessOp << ConditionSrcRange;
3428 return true;
3429 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003430 if (TestIsLessOp == Subtract) {
3431 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3432 NewStep).get();
3433 Subtract = !Subtract;
3434 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003435 }
3436
3437 Step = NewStep;
3438 SubtractStep = Subtract;
3439 return false;
3440}
3441
Alexey Bataev9c821032015-04-30 04:23:23 +00003442bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003443 // Check init-expr for canonical loop form and save loop counter
3444 // variable - #Var and its initialization value - #LB.
3445 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3446 // var = lb
3447 // integer-type var = lb
3448 // random-access-iterator-type var = lb
3449 // pointer-type var = lb
3450 //
3451 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003452 if (EmitDiags) {
3453 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3454 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003455 return true;
3456 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003457 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003458 if (Expr *E = dyn_cast<Expr>(S))
3459 S = E->IgnoreParens();
3460 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003461 if (BO->getOpcode() == BO_Assign) {
3462 auto *LHS = BO->getLHS()->IgnoreParens();
3463 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3464 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3465 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3466 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3467 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3468 }
3469 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3470 if (ME->isArrow() &&
3471 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3472 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3473 }
3474 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003475 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3476 if (DS->isSingleDecl()) {
3477 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003478 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003479 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003480 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003481 SemaRef.Diag(S->getLocStart(),
3482 diag::ext_omp_loop_not_canonical_init)
3483 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003484 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003485 }
3486 }
3487 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003488 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3489 if (CE->getOperator() == OO_Equal) {
3490 auto *LHS = CE->getArg(0);
3491 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3492 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3493 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3494 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3495 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3496 }
3497 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3498 if (ME->isArrow() &&
3499 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3500 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3501 }
3502 }
3503 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003504
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003505 if (Dependent() || SemaRef.CurContext->isDependentContext())
3506 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003507 if (EmitDiags) {
3508 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3509 << S->getSourceRange();
3510 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003511 return true;
3512}
3513
Alexey Bataev23b69422014-06-18 07:08:49 +00003514/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003515/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003516static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003517 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003518 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003519 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003520 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3521 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003522 if ((Ctor->isCopyOrMoveConstructor() ||
3523 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3524 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003525 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003526 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3527 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3528 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3529 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3530 return getCanonicalDecl(ME->getMemberDecl());
3531 return getCanonicalDecl(VD);
3532 }
3533 }
3534 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3535 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3536 return getCanonicalDecl(ME->getMemberDecl());
3537 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003538}
3539
3540bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3541 // Check test-expr for canonical form, save upper-bound UB, flags for
3542 // less/greater and for strict/non-strict comparison.
3543 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3544 // var relational-op b
3545 // b relational-op var
3546 //
3547 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003548 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003549 return true;
3550 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003551 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003552 SourceLocation CondLoc = S->getLocStart();
3553 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3554 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003555 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003556 return SetUB(BO->getRHS(),
3557 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3558 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3559 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003560 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003561 return SetUB(BO->getLHS(),
3562 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3563 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3564 BO->getSourceRange(), BO->getOperatorLoc());
3565 }
3566 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3567 if (CE->getNumArgs() == 2) {
3568 auto Op = CE->getOperator();
3569 switch (Op) {
3570 case OO_Greater:
3571 case OO_GreaterEqual:
3572 case OO_Less:
3573 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003574 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003575 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3576 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3577 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003578 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003579 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3580 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3581 CE->getOperatorLoc());
3582 break;
3583 default:
3584 break;
3585 }
3586 }
3587 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003588 if (Dependent() || SemaRef.CurContext->isDependentContext())
3589 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003590 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003591 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003592 return true;
3593}
3594
3595bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3596 // RHS of canonical loop form increment can be:
3597 // var + incr
3598 // incr + var
3599 // var - incr
3600 //
3601 RHS = RHS->IgnoreParenImpCasts();
3602 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3603 if (BO->isAdditiveOp()) {
3604 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003605 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003606 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003607 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003608 return SetStep(BO->getLHS(), false);
3609 }
3610 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3611 bool IsAdd = CE->getOperator() == OO_Plus;
3612 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003613 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003614 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003615 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003616 return SetStep(CE->getArg(0), false);
3617 }
3618 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003619 if (Dependent() || SemaRef.CurContext->isDependentContext())
3620 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003621 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003622 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003623 return true;
3624}
3625
3626bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3627 // Check incr-expr for canonical loop form and return true if it
3628 // does not conform.
3629 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3630 // ++var
3631 // var++
3632 // --var
3633 // var--
3634 // var += incr
3635 // var -= incr
3636 // var = var + incr
3637 // var = incr + var
3638 // var = var - incr
3639 //
3640 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003641 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003642 return true;
3643 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003644 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003645 S = S->IgnoreParens();
3646 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003647 if (UO->isIncrementDecrementOp() &&
3648 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003649 return SetStep(
3650 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3651 (UO->isDecrementOp() ? -1 : 1)).get(),
3652 false);
3653 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3654 switch (BO->getOpcode()) {
3655 case BO_AddAssign:
3656 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003657 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003658 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3659 break;
3660 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003661 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003662 return CheckIncRHS(BO->getRHS());
3663 break;
3664 default:
3665 break;
3666 }
3667 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3668 switch (CE->getOperator()) {
3669 case OO_PlusPlus:
3670 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003671 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003672 return SetStep(
3673 SemaRef.ActOnIntegerConstant(
3674 CE->getLocStart(),
3675 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3676 false);
3677 break;
3678 case OO_PlusEqual:
3679 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003680 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003681 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3682 break;
3683 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003684 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003685 return CheckIncRHS(CE->getArg(1));
3686 break;
3687 default:
3688 break;
3689 }
3690 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003691 if (Dependent() || SemaRef.CurContext->isDependentContext())
3692 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003693 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003694 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003695 return true;
3696}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003697
Alexey Bataev5a3af132016-03-29 08:58:54 +00003698static ExprResult
3699tryBuildCapture(Sema &SemaRef, Expr *Capture,
3700 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3701 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3702 return SemaRef.PerformImplicitConversion(
3703 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3704 /*AllowExplicit=*/true);
3705 auto I = Captures.find(Capture);
3706 if (I != Captures.end())
3707 return buildCapture(SemaRef, Capture, I->second);
3708 DeclRefExpr *Ref = nullptr;
3709 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3710 Captures[Capture] = Ref;
3711 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003712}
3713
Alexander Musmana5f070a2014-10-01 06:03:56 +00003714/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003715Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3716 Scope *S, const bool LimitedType,
3717 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003718 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003719 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003720 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003721 SemaRef.getLangOpts().CPlusPlus) {
3722 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003723 auto *UBExpr = TestIsLessOp ? UB : LB;
3724 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00003725 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3726 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003727 if (!Upper || !Lower)
3728 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003729
3730 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3731
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003732 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003733 // BuildBinOp already emitted error, this one is to point user to upper
3734 // and lower bound, and to tell what is passed to 'operator-'.
3735 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3736 << Upper->getSourceRange() << Lower->getSourceRange();
3737 return nullptr;
3738 }
3739 }
3740
3741 if (!Diff.isUsable())
3742 return nullptr;
3743
3744 // Upper - Lower [- 1]
3745 if (TestIsStrictOp)
3746 Diff = SemaRef.BuildBinOp(
3747 S, DefaultLoc, BO_Sub, Diff.get(),
3748 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3749 if (!Diff.isUsable())
3750 return nullptr;
3751
3752 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00003753 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3754 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003755 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003756 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003757 if (!Diff.isUsable())
3758 return nullptr;
3759
3760 // Parentheses (for dumping/debugging purposes only).
3761 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3762 if (!Diff.isUsable())
3763 return nullptr;
3764
3765 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003766 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003767 if (!Diff.isUsable())
3768 return nullptr;
3769
Alexander Musman174b3ca2014-10-06 11:16:29 +00003770 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003771 QualType Type = Diff.get()->getType();
3772 auto &C = SemaRef.Context;
3773 bool UseVarType = VarType->hasIntegerRepresentation() &&
3774 C.getTypeSize(Type) > C.getTypeSize(VarType);
3775 if (!Type->isIntegerType() || UseVarType) {
3776 unsigned NewSize =
3777 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3778 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3779 : Type->hasSignedIntegerRepresentation();
3780 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003781 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3782 Diff = SemaRef.PerformImplicitConversion(
3783 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3784 if (!Diff.isUsable())
3785 return nullptr;
3786 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003787 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003788 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003789 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3790 if (NewSize != C.getTypeSize(Type)) {
3791 if (NewSize < C.getTypeSize(Type)) {
3792 assert(NewSize == 64 && "incorrect loop var size");
3793 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3794 << InitSrcRange << ConditionSrcRange;
3795 }
3796 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003797 NewSize, Type->hasSignedIntegerRepresentation() ||
3798 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00003799 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3800 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3801 Sema::AA_Converting, true);
3802 if (!Diff.isUsable())
3803 return nullptr;
3804 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003805 }
3806 }
3807
Alexander Musmana5f070a2014-10-01 06:03:56 +00003808 return Diff.get();
3809}
3810
Alexey Bataev5a3af132016-03-29 08:58:54 +00003811Expr *OpenMPIterationSpaceChecker::BuildPreCond(
3812 Scope *S, Expr *Cond,
3813 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003814 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3815 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3816 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003817
Alexey Bataev5a3af132016-03-29 08:58:54 +00003818 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
3819 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
3820 if (!NewLB.isUsable() || !NewUB.isUsable())
3821 return nullptr;
3822
Alexey Bataev62dbb972015-04-22 11:59:37 +00003823 auto CondExpr = SemaRef.BuildBinOp(
3824 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3825 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003826 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003827 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003828 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
3829 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00003830 CondExpr = SemaRef.PerformImplicitConversion(
3831 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3832 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003833 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003834 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3835 // Otherwise use original loop conditon and evaluate it in runtime.
3836 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3837}
3838
Alexander Musmana5f070a2014-10-01 06:03:56 +00003839/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003840DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
3841 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
3842 auto *VD = dyn_cast<VarDecl>(LCDecl);
3843 if (!VD) {
3844 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
3845 auto *Ref = buildDeclRefExpr(
3846 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
3847 Captures.insert(std::make_pair(LCRef, Ref));
3848 return Ref;
3849 }
3850 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00003851 DefaultLoc);
3852}
3853
3854Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003855 if (LCDecl && !LCDecl->isInvalidDecl()) {
3856 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003857 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003858 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
3859 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003860 if (PrivateVar->isInvalidDecl())
3861 return nullptr;
3862 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3863 }
3864 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003865}
3866
3867/// \brief Build initization of the counter be used for codegen.
3868Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3869
3870/// \brief Build step of the counter be used for codegen.
3871Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3872
3873/// \brief Iteration space of a single for loop.
3874struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003875 /// \brief Condition of the loop.
3876 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003877 /// \brief This expression calculates the number of iterations in the loop.
3878 /// It is always possible to calculate it before starting the loop.
3879 Expr *NumIterations;
3880 /// \brief The loop counter variable.
3881 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003882 /// \brief Private loop counter variable.
3883 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003884 /// \brief This is initializer for the initial value of #CounterVar.
3885 Expr *CounterInit;
3886 /// \brief This is step for the #CounterVar used to generate its update:
3887 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3888 Expr *CounterStep;
3889 /// \brief Should step be subtracted?
3890 bool Subtract;
3891 /// \brief Source range of the loop init.
3892 SourceRange InitSrcRange;
3893 /// \brief Source range of the loop condition.
3894 SourceRange CondSrcRange;
3895 /// \brief Source range of the loop increment.
3896 SourceRange IncSrcRange;
3897};
3898
Alexey Bataev23b69422014-06-18 07:08:49 +00003899} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003900
Alexey Bataev9c821032015-04-30 04:23:23 +00003901void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3902 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3903 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003904 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3905 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003906 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3907 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003908 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3909 if (auto *D = ISC.GetLoopDecl()) {
3910 auto *VD = dyn_cast<VarDecl>(D);
3911 if (!VD) {
3912 if (auto *Private = IsOpenMPCapturedDecl(D))
3913 VD = Private;
3914 else {
3915 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
3916 /*WithInit=*/false);
3917 VD = cast<VarDecl>(Ref->getDecl());
3918 }
3919 }
3920 DSAStack->addLoopControlVariable(D, VD);
3921 }
3922 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003923 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003924 }
3925}
3926
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003927/// \brief Called on a for stmt to check and extract its iteration space
3928/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003929static bool CheckOpenMPIterationSpace(
3930 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3931 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003932 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003933 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00003934 LoopIterationSpace &ResultIterSpace,
3935 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003936 // OpenMP [2.6, Canonical Loop Form]
3937 // for (init-expr; test-expr; incr-expr) structured-block
3938 auto For = dyn_cast_or_null<ForStmt>(S);
3939 if (!For) {
3940 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003941 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3942 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3943 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3944 if (NestedLoopCount > 1) {
3945 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3946 SemaRef.Diag(DSA.getConstructLoc(),
3947 diag::note_omp_collapse_ordered_expr)
3948 << 2 << CollapseLoopCountExpr->getSourceRange()
3949 << OrderedLoopCountExpr->getSourceRange();
3950 else if (CollapseLoopCountExpr)
3951 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3952 diag::note_omp_collapse_ordered_expr)
3953 << 0 << CollapseLoopCountExpr->getSourceRange();
3954 else
3955 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3956 diag::note_omp_collapse_ordered_expr)
3957 << 1 << OrderedLoopCountExpr->getSourceRange();
3958 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959 return true;
3960 }
3961 assert(For->getBody());
3962
3963 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3964
3965 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003966 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003967 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003968 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003969
3970 bool HasErrors = false;
3971
3972 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003973 if (auto *LCDecl = ISC.GetLoopDecl()) {
3974 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003975
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003976 // OpenMP [2.6, Canonical Loop Form]
3977 // Var is one of the following:
3978 // A variable of signed or unsigned integer type.
3979 // For C++, a variable of a random access iterator type.
3980 // For C, a variable of a pointer type.
3981 auto VarType = LCDecl->getType().getNonReferenceType();
3982 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3983 !VarType->isPointerType() &&
3984 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3985 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3986 << SemaRef.getLangOpts().CPlusPlus;
3987 HasErrors = true;
3988 }
3989
3990 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
3991 // a Construct
3992 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3993 // parallel for construct is (are) private.
3994 // The loop iteration variable in the associated for-loop of a simd
3995 // construct with just one associated for-loop is linear with a
3996 // constant-linear-step that is the increment of the associated for-loop.
3997 // Exclude loop var from the list of variables with implicitly defined data
3998 // sharing attributes.
3999 VarsWithImplicitDSA.erase(LCDecl);
4000
4001 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4002 // in a Construct, C/C++].
4003 // The loop iteration variable in the associated for-loop of a simd
4004 // construct with just one associated for-loop may be listed in a linear
4005 // clause with a constant-linear-step that is the increment of the
4006 // associated for-loop.
4007 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4008 // parallel for construct may be listed in a private or lastprivate clause.
4009 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4010 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4011 // declared in the loop and it is predetermined as a private.
4012 auto PredeterminedCKind =
4013 isOpenMPSimdDirective(DKind)
4014 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4015 : OMPC_private;
4016 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4017 DVar.CKind != PredeterminedCKind) ||
4018 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4019 isOpenMPDistributeDirective(DKind)) &&
4020 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4021 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4022 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4023 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4024 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4025 << getOpenMPClauseName(PredeterminedCKind);
4026 if (DVar.RefExpr == nullptr)
4027 DVar.CKind = PredeterminedCKind;
4028 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4029 HasErrors = true;
4030 } else if (LoopDeclRefExpr != nullptr) {
4031 // Make the loop iteration variable private (for worksharing constructs),
4032 // linear (for simd directives with the only one associated loop) or
4033 // lastprivate (for simd directives with several collapsed or ordered
4034 // loops).
4035 if (DVar.CKind == OMPC_unknown)
4036 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, MatchesAlways(),
4037 /*FromParent=*/false);
4038 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4039 }
4040
4041 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4042
4043 // Check test-expr.
4044 HasErrors |= ISC.CheckCond(For->getCond());
4045
4046 // Check incr-expr.
4047 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004048 }
4049
Alexander Musmana5f070a2014-10-01 06:03:56 +00004050 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004051 return HasErrors;
4052
Alexander Musmana5f070a2014-10-01 06:03:56 +00004053 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004054 ResultIterSpace.PreCond =
4055 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004056 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004057 DSA.getCurScope(),
4058 (isOpenMPWorksharingDirective(DKind) ||
4059 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4060 Captures);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004061 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures);
Alexey Bataeva8899172015-08-06 12:30:57 +00004062 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004063 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4064 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4065 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4066 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4067 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4068 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4069
Alexey Bataev62dbb972015-04-22 11:59:37 +00004070 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4071 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004072 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004073 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004074 ResultIterSpace.CounterInit == nullptr ||
4075 ResultIterSpace.CounterStep == nullptr);
4076
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004077 return HasErrors;
4078}
4079
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004080/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004081static ExprResult
4082BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4083 ExprResult Start,
4084 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004085 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004086 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4087 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004088 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004089 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004090 VarRef.get()->getType())) {
4091 NewStart = SemaRef.PerformImplicitConversion(
4092 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4093 /*AllowExplicit=*/true);
4094 if (!NewStart.isUsable())
4095 return ExprError();
4096 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004097
4098 auto Init =
4099 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4100 return Init;
4101}
4102
Alexander Musmana5f070a2014-10-01 06:03:56 +00004103/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004104static ExprResult
4105BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4106 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4107 ExprResult Step, bool Subtract,
4108 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004109 // Add parentheses (for debugging purposes only).
4110 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4111 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4112 !Step.isUsable())
4113 return ExprError();
4114
Alexey Bataev5a3af132016-03-29 08:58:54 +00004115 ExprResult NewStep = Step;
4116 if (Captures)
4117 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004118 if (NewStep.isInvalid())
4119 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004120 ExprResult Update =
4121 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004122 if (!Update.isUsable())
4123 return ExprError();
4124
Alexey Bataevc0214e02016-02-16 12:13:49 +00004125 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4126 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004127 ExprResult NewStart = Start;
4128 if (Captures)
4129 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004130 if (NewStart.isInvalid())
4131 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004132
Alexey Bataevc0214e02016-02-16 12:13:49 +00004133 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4134 ExprResult SavedUpdate = Update;
4135 ExprResult UpdateVal;
4136 if (VarRef.get()->getType()->isOverloadableType() ||
4137 NewStart.get()->getType()->isOverloadableType() ||
4138 Update.get()->getType()->isOverloadableType()) {
4139 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4140 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4141 Update =
4142 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4143 if (Update.isUsable()) {
4144 UpdateVal =
4145 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4146 VarRef.get(), SavedUpdate.get());
4147 if (UpdateVal.isUsable()) {
4148 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4149 UpdateVal.get());
4150 }
4151 }
4152 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4153 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004154
Alexey Bataevc0214e02016-02-16 12:13:49 +00004155 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4156 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4157 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4158 NewStart.get(), SavedUpdate.get());
4159 if (!Update.isUsable())
4160 return ExprError();
4161
Alexey Bataev11481f52016-02-17 10:29:05 +00004162 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4163 VarRef.get()->getType())) {
4164 Update = SemaRef.PerformImplicitConversion(
4165 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4166 if (!Update.isUsable())
4167 return ExprError();
4168 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004169
4170 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4171 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004172 return Update;
4173}
4174
4175/// \brief Convert integer expression \a E to make it have at least \a Bits
4176/// bits.
4177static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4178 Sema &SemaRef) {
4179 if (E == nullptr)
4180 return ExprError();
4181 auto &C = SemaRef.Context;
4182 QualType OldType = E->getType();
4183 unsigned HasBits = C.getTypeSize(OldType);
4184 if (HasBits >= Bits)
4185 return ExprResult(E);
4186 // OK to convert to signed, because new type has more bits than old.
4187 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4188 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4189 true);
4190}
4191
4192/// \brief Check if the given expression \a E is a constant integer that fits
4193/// into \a Bits bits.
4194static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4195 if (E == nullptr)
4196 return false;
4197 llvm::APSInt Result;
4198 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4199 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4200 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004201}
4202
Alexey Bataev5a3af132016-03-29 08:58:54 +00004203/// Build preinits statement for the given declarations.
4204static Stmt *buildPreInits(ASTContext &Context,
4205 SmallVectorImpl<Decl *> &PreInits) {
4206 if (!PreInits.empty()) {
4207 return new (Context) DeclStmt(
4208 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4209 SourceLocation(), SourceLocation());
4210 }
4211 return nullptr;
4212}
4213
4214/// Build preinits statement for the given declarations.
4215static Stmt *buildPreInits(ASTContext &Context,
4216 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4217 if (!Captures.empty()) {
4218 SmallVector<Decl *, 16> PreInits;
4219 for (auto &Pair : Captures)
4220 PreInits.push_back(Pair.second->getDecl());
4221 return buildPreInits(Context, PreInits);
4222 }
4223 return nullptr;
4224}
4225
4226/// Build postupdate expression for the given list of postupdates expressions.
4227static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4228 Expr *PostUpdate = nullptr;
4229 if (!PostUpdates.empty()) {
4230 for (auto *E : PostUpdates) {
4231 Expr *ConvE = S.BuildCStyleCastExpr(
4232 E->getExprLoc(),
4233 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4234 E->getExprLoc(), E)
4235 .get();
4236 PostUpdate = PostUpdate
4237 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4238 PostUpdate, ConvE)
4239 .get()
4240 : ConvE;
4241 }
4242 }
4243 return PostUpdate;
4244}
4245
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004246/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004247/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4248/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004249static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004250CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4251 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4252 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004253 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004254 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004255 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004256 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004257 // Found 'collapse' clause - calculate collapse number.
4258 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004259 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004260 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004261 }
4262 if (OrderedLoopCountExpr) {
4263 // Found 'ordered' clause - calculate collapse number.
4264 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004265 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4266 if (Result.getLimitedValue() < NestedLoopCount) {
4267 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4268 diag::err_omp_wrong_ordered_loop_count)
4269 << OrderedLoopCountExpr->getSourceRange();
4270 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4271 diag::note_collapse_loop_count)
4272 << CollapseLoopCountExpr->getSourceRange();
4273 }
4274 NestedLoopCount = Result.getLimitedValue();
4275 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004276 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004277 // This is helper routine for loop directives (e.g., 'for', 'simd',
4278 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004279 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004280 SmallVector<LoopIterationSpace, 4> IterSpaces;
4281 IterSpaces.resize(NestedLoopCount);
4282 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004283 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004284 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004285 NestedLoopCount, CollapseLoopCountExpr,
4286 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004287 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004288 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004289 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004290 // OpenMP [2.8.1, simd construct, Restrictions]
4291 // All loops associated with the construct must be perfectly nested; that
4292 // is, there must be no intervening code nor any OpenMP directive between
4293 // any two loops.
4294 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004295 }
4296
Alexander Musmana5f070a2014-10-01 06:03:56 +00004297 Built.clear(/* size */ NestedLoopCount);
4298
4299 if (SemaRef.CurContext->isDependentContext())
4300 return NestedLoopCount;
4301
4302 // An example of what is generated for the following code:
4303 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004304 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004305 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004306 // for (k = 0; k < NK; ++k)
4307 // for (j = J0; j < NJ; j+=2) {
4308 // <loop body>
4309 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004310 //
4311 // We generate the code below.
4312 // Note: the loop body may be outlined in CodeGen.
4313 // Note: some counters may be C++ classes, operator- is used to find number of
4314 // iterations and operator+= to calculate counter value.
4315 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4316 // or i64 is currently supported).
4317 //
4318 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4319 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4320 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4321 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4322 // // similar updates for vars in clauses (e.g. 'linear')
4323 // <loop body (using local i and j)>
4324 // }
4325 // i = NI; // assign final values of counters
4326 // j = NJ;
4327 //
4328
4329 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4330 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004331 // Precondition tests if there is at least one iteration (all conditions are
4332 // true).
4333 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004334 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004335 ExprResult LastIteration32 = WidenIterationCount(
4336 32 /* Bits */, SemaRef.PerformImplicitConversion(
4337 N0->IgnoreImpCasts(), N0->getType(),
4338 Sema::AA_Converting, /*AllowExplicit=*/true)
4339 .get(),
4340 SemaRef);
4341 ExprResult LastIteration64 = WidenIterationCount(
4342 64 /* Bits */, SemaRef.PerformImplicitConversion(
4343 N0->IgnoreImpCasts(), N0->getType(),
4344 Sema::AA_Converting, /*AllowExplicit=*/true)
4345 .get(),
4346 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004347
4348 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4349 return NestedLoopCount;
4350
4351 auto &C = SemaRef.Context;
4352 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4353
4354 Scope *CurScope = DSA.getCurScope();
4355 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004356 if (PreCond.isUsable()) {
4357 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4358 PreCond.get(), IterSpaces[Cnt].PreCond);
4359 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004360 auto N = IterSpaces[Cnt].NumIterations;
4361 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4362 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004363 LastIteration32 = SemaRef.BuildBinOp(
4364 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4365 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4366 Sema::AA_Converting,
4367 /*AllowExplicit=*/true)
4368 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004369 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004370 LastIteration64 = SemaRef.BuildBinOp(
4371 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4372 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4373 Sema::AA_Converting,
4374 /*AllowExplicit=*/true)
4375 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004376 }
4377
4378 // Choose either the 32-bit or 64-bit version.
4379 ExprResult LastIteration = LastIteration64;
4380 if (LastIteration32.isUsable() &&
4381 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4382 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4383 FitsInto(
4384 32 /* Bits */,
4385 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4386 LastIteration64.get(), SemaRef)))
4387 LastIteration = LastIteration32;
4388
4389 if (!LastIteration.isUsable())
4390 return 0;
4391
4392 // Save the number of iterations.
4393 ExprResult NumIterations = LastIteration;
4394 {
4395 LastIteration = SemaRef.BuildBinOp(
4396 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4397 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4398 if (!LastIteration.isUsable())
4399 return 0;
4400 }
4401
4402 // Calculate the last iteration number beforehand instead of doing this on
4403 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4404 llvm::APSInt Result;
4405 bool IsConstant =
4406 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4407 ExprResult CalcLastIteration;
4408 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004409 ExprResult SaveRef =
4410 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004411 LastIteration = SaveRef;
4412
4413 // Prepare SaveRef + 1.
4414 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004415 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004416 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4417 if (!NumIterations.isUsable())
4418 return 0;
4419 }
4420
4421 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4422
Alexander Musmanc6388682014-12-15 07:07:06 +00004423 QualType VType = LastIteration.get()->getType();
4424 // Build variables passed into runtime, nesessary for worksharing directives.
4425 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004426 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4427 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004428 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004429 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4430 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004431 SemaRef.AddInitializerToDecl(
4432 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4433 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4434
4435 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004436 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4437 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004438 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4439 /*DirectInit*/ false,
4440 /*TypeMayContainAuto*/ false);
4441
4442 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4443 // This will be used to implement clause 'lastprivate'.
4444 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004445 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4446 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004447 SemaRef.AddInitializerToDecl(
4448 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4449 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4450
4451 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004452 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4453 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004454 SemaRef.AddInitializerToDecl(
4455 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4456 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4457
4458 // Build expression: UB = min(UB, LastIteration)
4459 // It is nesessary for CodeGen of directives with static scheduling.
4460 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4461 UB.get(), LastIteration.get());
4462 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4463 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4464 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4465 CondOp.get());
4466 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4467 }
4468
4469 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004470 ExprResult IV;
4471 ExprResult Init;
4472 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004473 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4474 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004475 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004476 isOpenMPTaskLoopDirective(DKind) ||
4477 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004478 ? LB.get()
4479 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4480 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4481 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004482 }
4483
Alexander Musmanc6388682014-12-15 07:07:06 +00004484 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004485 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004486 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004487 (isOpenMPWorksharingDirective(DKind) ||
4488 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004489 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4490 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4491 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004492
4493 // Loop increment (IV = IV + 1)
4494 SourceLocation IncLoc;
4495 ExprResult Inc =
4496 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4497 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4498 if (!Inc.isUsable())
4499 return 0;
4500 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004501 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4502 if (!Inc.isUsable())
4503 return 0;
4504
4505 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4506 // Used for directives with static scheduling.
4507 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004508 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4509 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004510 // LB + ST
4511 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4512 if (!NextLB.isUsable())
4513 return 0;
4514 // LB = LB + ST
4515 NextLB =
4516 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4517 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4518 if (!NextLB.isUsable())
4519 return 0;
4520 // UB + ST
4521 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4522 if (!NextUB.isUsable())
4523 return 0;
4524 // UB = UB + ST
4525 NextUB =
4526 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4527 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4528 if (!NextUB.isUsable())
4529 return 0;
4530 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004531
4532 // Build updates and final values of the loop counters.
4533 bool HasErrors = false;
4534 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004535 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004536 Built.Updates.resize(NestedLoopCount);
4537 Built.Finals.resize(NestedLoopCount);
4538 {
4539 ExprResult Div;
4540 // Go from inner nested loop to outer.
4541 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4542 LoopIterationSpace &IS = IterSpaces[Cnt];
4543 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4544 // Build: Iter = (IV / Div) % IS.NumIters
4545 // where Div is product of previous iterations' IS.NumIters.
4546 ExprResult Iter;
4547 if (Div.isUsable()) {
4548 Iter =
4549 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4550 } else {
4551 Iter = IV;
4552 assert((Cnt == (int)NestedLoopCount - 1) &&
4553 "unusable div expected on first iteration only");
4554 }
4555
4556 if (Cnt != 0 && Iter.isUsable())
4557 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4558 IS.NumIterations);
4559 if (!Iter.isUsable()) {
4560 HasErrors = true;
4561 break;
4562 }
4563
Alexey Bataev39f915b82015-05-08 10:41:21 +00004564 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4565 auto *CounterVar = buildDeclRefExpr(
4566 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4567 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4568 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004569 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004570 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004571 if (!Init.isUsable()) {
4572 HasErrors = true;
4573 break;
4574 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004575 ExprResult Update = BuildCounterUpdate(
4576 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4577 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004578 if (!Update.isUsable()) {
4579 HasErrors = true;
4580 break;
4581 }
4582
4583 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4584 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004585 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004586 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004587 if (!Final.isUsable()) {
4588 HasErrors = true;
4589 break;
4590 }
4591
4592 // Build Div for the next iteration: Div <- Div * IS.NumIters
4593 if (Cnt != 0) {
4594 if (Div.isUnset())
4595 Div = IS.NumIterations;
4596 else
4597 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4598 IS.NumIterations);
4599
4600 // Add parentheses (for debugging purposes only).
4601 if (Div.isUsable())
4602 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4603 if (!Div.isUsable()) {
4604 HasErrors = true;
4605 break;
4606 }
4607 }
4608 if (!Update.isUsable() || !Final.isUsable()) {
4609 HasErrors = true;
4610 break;
4611 }
4612 // Save results
4613 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004614 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004615 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004616 Built.Updates[Cnt] = Update.get();
4617 Built.Finals[Cnt] = Final.get();
4618 }
4619 }
4620
4621 if (HasErrors)
4622 return 0;
4623
4624 // Save results
4625 Built.IterationVarRef = IV.get();
4626 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004627 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004628 Built.CalcLastIteration =
4629 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004630 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004631 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004632 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004633 Built.Init = Init.get();
4634 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004635 Built.LB = LB.get();
4636 Built.UB = UB.get();
4637 Built.IL = IL.get();
4638 Built.ST = ST.get();
4639 Built.EUB = EUB.get();
4640 Built.NLB = NextLB.get();
4641 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004642
Alexey Bataevabfc0692014-06-25 06:52:00 +00004643 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004644}
4645
Alexey Bataev10e775f2015-07-30 11:36:16 +00004646static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004647 auto CollapseClauses =
4648 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4649 if (CollapseClauses.begin() != CollapseClauses.end())
4650 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004651 return nullptr;
4652}
4653
Alexey Bataev10e775f2015-07-30 11:36:16 +00004654static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004655 auto OrderedClauses =
4656 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4657 if (OrderedClauses.begin() != OrderedClauses.end())
4658 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004659 return nullptr;
4660}
4661
Alexey Bataev66b15b52015-08-21 11:14:16 +00004662static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4663 const Expr *Safelen) {
4664 llvm::APSInt SimdlenRes, SafelenRes;
4665 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4666 Simdlen->isInstantiationDependent() ||
4667 Simdlen->containsUnexpandedParameterPack())
4668 return false;
4669 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4670 Safelen->isInstantiationDependent() ||
4671 Safelen->containsUnexpandedParameterPack())
4672 return false;
4673 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4674 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4675 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4676 // If both simdlen and safelen clauses are specified, the value of the simdlen
4677 // parameter must be less than or equal to the value of the safelen parameter.
4678 if (SimdlenRes > SafelenRes) {
4679 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4680 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4681 return true;
4682 }
4683 return false;
4684}
4685
Alexey Bataev4acb8592014-07-07 13:01:15 +00004686StmtResult Sema::ActOnOpenMPSimdDirective(
4687 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4688 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004689 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004690 if (!AStmt)
4691 return StmtError();
4692
4693 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004694 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004695 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4696 // define the nested loops number.
4697 unsigned NestedLoopCount = CheckOpenMPLoop(
4698 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4699 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004700 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004701 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004702
Alexander Musmana5f070a2014-10-01 06:03:56 +00004703 assert((CurContext->isDependentContext() || B.builtAll()) &&
4704 "omp simd loop exprs were not built");
4705
Alexander Musman3276a272015-03-21 10:12:56 +00004706 if (!CurContext->isDependentContext()) {
4707 // Finalize the clauses that need pre-built expressions for CodeGen.
4708 for (auto C : Clauses) {
4709 if (auto LC = dyn_cast<OMPLinearClause>(C))
4710 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4711 B.NumIterations, *this, CurScope))
4712 return StmtError();
4713 }
4714 }
4715
Alexey Bataev66b15b52015-08-21 11:14:16 +00004716 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4717 // If both simdlen and safelen clauses are specified, the value of the simdlen
4718 // parameter must be less than or equal to the value of the safelen parameter.
4719 OMPSafelenClause *Safelen = nullptr;
4720 OMPSimdlenClause *Simdlen = nullptr;
4721 for (auto *Clause : Clauses) {
4722 if (Clause->getClauseKind() == OMPC_safelen)
4723 Safelen = cast<OMPSafelenClause>(Clause);
4724 else if (Clause->getClauseKind() == OMPC_simdlen)
4725 Simdlen = cast<OMPSimdlenClause>(Clause);
4726 if (Safelen && Simdlen)
4727 break;
4728 }
4729 if (Simdlen && Safelen &&
4730 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4731 Safelen->getSafelen()))
4732 return StmtError();
4733
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004734 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004735 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4736 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004737}
4738
Alexey Bataev4acb8592014-07-07 13:01:15 +00004739StmtResult Sema::ActOnOpenMPForDirective(
4740 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4741 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004742 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004743 if (!AStmt)
4744 return StmtError();
4745
4746 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004747 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004748 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4749 // define the nested loops number.
4750 unsigned NestedLoopCount = CheckOpenMPLoop(
4751 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4752 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004753 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004754 return StmtError();
4755
Alexander Musmana5f070a2014-10-01 06:03:56 +00004756 assert((CurContext->isDependentContext() || B.builtAll()) &&
4757 "omp for loop exprs were not built");
4758
Alexey Bataev54acd402015-08-04 11:18:19 +00004759 if (!CurContext->isDependentContext()) {
4760 // Finalize the clauses that need pre-built expressions for CodeGen.
4761 for (auto C : Clauses) {
4762 if (auto LC = dyn_cast<OMPLinearClause>(C))
4763 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4764 B.NumIterations, *this, CurScope))
4765 return StmtError();
4766 }
4767 }
4768
Alexey Bataevf29276e2014-06-18 04:14:57 +00004769 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004770 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004771 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004772}
4773
Alexander Musmanf82886e2014-09-18 05:12:34 +00004774StmtResult Sema::ActOnOpenMPForSimdDirective(
4775 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4776 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004777 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004778 if (!AStmt)
4779 return StmtError();
4780
4781 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004782 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004783 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4784 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004785 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004786 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4787 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4788 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004789 if (NestedLoopCount == 0)
4790 return StmtError();
4791
Alexander Musmanc6388682014-12-15 07:07:06 +00004792 assert((CurContext->isDependentContext() || B.builtAll()) &&
4793 "omp for simd loop exprs were not built");
4794
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004795 if (!CurContext->isDependentContext()) {
4796 // Finalize the clauses that need pre-built expressions for CodeGen.
4797 for (auto C : Clauses) {
4798 if (auto LC = dyn_cast<OMPLinearClause>(C))
4799 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4800 B.NumIterations, *this, CurScope))
4801 return StmtError();
4802 }
4803 }
4804
Alexey Bataev66b15b52015-08-21 11:14:16 +00004805 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4806 // If both simdlen and safelen clauses are specified, the value of the simdlen
4807 // parameter must be less than or equal to the value of the safelen parameter.
4808 OMPSafelenClause *Safelen = nullptr;
4809 OMPSimdlenClause *Simdlen = nullptr;
4810 for (auto *Clause : Clauses) {
4811 if (Clause->getClauseKind() == OMPC_safelen)
4812 Safelen = cast<OMPSafelenClause>(Clause);
4813 else if (Clause->getClauseKind() == OMPC_simdlen)
4814 Simdlen = cast<OMPSimdlenClause>(Clause);
4815 if (Safelen && Simdlen)
4816 break;
4817 }
4818 if (Simdlen && Safelen &&
4819 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4820 Safelen->getSafelen()))
4821 return StmtError();
4822
Alexander Musmanf82886e2014-09-18 05:12:34 +00004823 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004824 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4825 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004826}
4827
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004828StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4829 Stmt *AStmt,
4830 SourceLocation StartLoc,
4831 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004832 if (!AStmt)
4833 return StmtError();
4834
4835 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004836 auto BaseStmt = AStmt;
4837 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4838 BaseStmt = CS->getCapturedStmt();
4839 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4840 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004841 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004842 return StmtError();
4843 // All associated statements must be '#pragma omp section' except for
4844 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004845 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004846 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4847 if (SectionStmt)
4848 Diag(SectionStmt->getLocStart(),
4849 diag::err_omp_sections_substmt_not_section);
4850 return StmtError();
4851 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004852 cast<OMPSectionDirective>(SectionStmt)
4853 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004854 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004855 } else {
4856 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4857 return StmtError();
4858 }
4859
4860 getCurFunction()->setHasBranchProtectedScope();
4861
Alexey Bataev25e5b442015-09-15 12:52:43 +00004862 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4863 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004864}
4865
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004866StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4867 SourceLocation StartLoc,
4868 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004869 if (!AStmt)
4870 return StmtError();
4871
4872 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004873
4874 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004875 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004876
Alexey Bataev25e5b442015-09-15 12:52:43 +00004877 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4878 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004879}
4880
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004881StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4882 Stmt *AStmt,
4883 SourceLocation StartLoc,
4884 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004885 if (!AStmt)
4886 return StmtError();
4887
4888 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004889
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004890 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004891
Alexey Bataev3255bf32015-01-19 05:20:46 +00004892 // OpenMP [2.7.3, single Construct, Restrictions]
4893 // The copyprivate clause must not be used with the nowait clause.
4894 OMPClause *Nowait = nullptr;
4895 OMPClause *Copyprivate = nullptr;
4896 for (auto *Clause : Clauses) {
4897 if (Clause->getClauseKind() == OMPC_nowait)
4898 Nowait = Clause;
4899 else if (Clause->getClauseKind() == OMPC_copyprivate)
4900 Copyprivate = Clause;
4901 if (Copyprivate && Nowait) {
4902 Diag(Copyprivate->getLocStart(),
4903 diag::err_omp_single_copyprivate_with_nowait);
4904 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4905 return StmtError();
4906 }
4907 }
4908
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004909 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4910}
4911
Alexander Musman80c22892014-07-17 08:54:58 +00004912StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4913 SourceLocation StartLoc,
4914 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004915 if (!AStmt)
4916 return StmtError();
4917
4918 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004919
4920 getCurFunction()->setHasBranchProtectedScope();
4921
4922 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4923}
4924
Alexey Bataev28c75412015-12-15 08:19:24 +00004925StmtResult Sema::ActOnOpenMPCriticalDirective(
4926 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4927 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004928 if (!AStmt)
4929 return StmtError();
4930
4931 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004932
Alexey Bataev28c75412015-12-15 08:19:24 +00004933 bool ErrorFound = false;
4934 llvm::APSInt Hint;
4935 SourceLocation HintLoc;
4936 bool DependentHint = false;
4937 for (auto *C : Clauses) {
4938 if (C->getClauseKind() == OMPC_hint) {
4939 if (!DirName.getName()) {
4940 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4941 ErrorFound = true;
4942 }
4943 Expr *E = cast<OMPHintClause>(C)->getHint();
4944 if (E->isTypeDependent() || E->isValueDependent() ||
4945 E->isInstantiationDependent())
4946 DependentHint = true;
4947 else {
4948 Hint = E->EvaluateKnownConstInt(Context);
4949 HintLoc = C->getLocStart();
4950 }
4951 }
4952 }
4953 if (ErrorFound)
4954 return StmtError();
4955 auto Pair = DSAStack->getCriticalWithHint(DirName);
4956 if (Pair.first && DirName.getName() && !DependentHint) {
4957 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4958 Diag(StartLoc, diag::err_omp_critical_with_hint);
4959 if (HintLoc.isValid()) {
4960 Diag(HintLoc, diag::note_omp_critical_hint_here)
4961 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4962 } else
4963 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4964 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4965 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4966 << 1
4967 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4968 /*Radix=*/10, /*Signed=*/false);
4969 } else
4970 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4971 }
4972 }
4973
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004974 getCurFunction()->setHasBranchProtectedScope();
4975
Alexey Bataev28c75412015-12-15 08:19:24 +00004976 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4977 Clauses, AStmt);
4978 if (!Pair.first && DirName.getName() && !DependentHint)
4979 DSAStack->addCriticalWithHint(Dir, Hint);
4980 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004981}
4982
Alexey Bataev4acb8592014-07-07 13:01:15 +00004983StmtResult Sema::ActOnOpenMPParallelForDirective(
4984 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4985 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004986 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004987 if (!AStmt)
4988 return StmtError();
4989
Alexey Bataev4acb8592014-07-07 13:01:15 +00004990 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4991 // 1.2.2 OpenMP Language Terminology
4992 // Structured block - An executable statement with a single entry at the
4993 // top and a single exit at the bottom.
4994 // The point of exit cannot be a branch out of the structured block.
4995 // longjmp() and throw() must not violate the entry/exit criteria.
4996 CS->getCapturedDecl()->setNothrow();
4997
Alexander Musmanc6388682014-12-15 07:07:06 +00004998 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004999 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5000 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005001 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005002 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5003 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5004 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005005 if (NestedLoopCount == 0)
5006 return StmtError();
5007
Alexander Musmana5f070a2014-10-01 06:03:56 +00005008 assert((CurContext->isDependentContext() || B.builtAll()) &&
5009 "omp parallel for loop exprs were not built");
5010
Alexey Bataev54acd402015-08-04 11:18:19 +00005011 if (!CurContext->isDependentContext()) {
5012 // Finalize the clauses that need pre-built expressions for CodeGen.
5013 for (auto C : Clauses) {
5014 if (auto LC = dyn_cast<OMPLinearClause>(C))
5015 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5016 B.NumIterations, *this, CurScope))
5017 return StmtError();
5018 }
5019 }
5020
Alexey Bataev4acb8592014-07-07 13:01:15 +00005021 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005022 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005023 NestedLoopCount, Clauses, AStmt, B,
5024 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005025}
5026
Alexander Musmane4e893b2014-09-23 09:33:00 +00005027StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5028 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5029 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005030 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005031 if (!AStmt)
5032 return StmtError();
5033
Alexander Musmane4e893b2014-09-23 09:33:00 +00005034 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5035 // 1.2.2 OpenMP Language Terminology
5036 // Structured block - An executable statement with a single entry at the
5037 // top and a single exit at the bottom.
5038 // The point of exit cannot be a branch out of the structured block.
5039 // longjmp() and throw() must not violate the entry/exit criteria.
5040 CS->getCapturedDecl()->setNothrow();
5041
Alexander Musmanc6388682014-12-15 07:07:06 +00005042 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005043 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5044 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005045 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005046 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5047 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5048 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005049 if (NestedLoopCount == 0)
5050 return StmtError();
5051
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005052 if (!CurContext->isDependentContext()) {
5053 // Finalize the clauses that need pre-built expressions for CodeGen.
5054 for (auto C : Clauses) {
5055 if (auto LC = dyn_cast<OMPLinearClause>(C))
5056 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5057 B.NumIterations, *this, CurScope))
5058 return StmtError();
5059 }
5060 }
5061
Alexey Bataev66b15b52015-08-21 11:14:16 +00005062 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5063 // If both simdlen and safelen clauses are specified, the value of the simdlen
5064 // parameter must be less than or equal to the value of the safelen parameter.
5065 OMPSafelenClause *Safelen = nullptr;
5066 OMPSimdlenClause *Simdlen = nullptr;
5067 for (auto *Clause : Clauses) {
5068 if (Clause->getClauseKind() == OMPC_safelen)
5069 Safelen = cast<OMPSafelenClause>(Clause);
5070 else if (Clause->getClauseKind() == OMPC_simdlen)
5071 Simdlen = cast<OMPSimdlenClause>(Clause);
5072 if (Safelen && Simdlen)
5073 break;
5074 }
5075 if (Simdlen && Safelen &&
5076 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5077 Safelen->getSafelen()))
5078 return StmtError();
5079
Alexander Musmane4e893b2014-09-23 09:33:00 +00005080 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005081 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005082 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005083}
5084
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005085StmtResult
5086Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5087 Stmt *AStmt, SourceLocation StartLoc,
5088 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005089 if (!AStmt)
5090 return StmtError();
5091
5092 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005093 auto BaseStmt = AStmt;
5094 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5095 BaseStmt = CS->getCapturedStmt();
5096 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5097 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005098 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005099 return StmtError();
5100 // All associated statements must be '#pragma omp section' except for
5101 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005102 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005103 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5104 if (SectionStmt)
5105 Diag(SectionStmt->getLocStart(),
5106 diag::err_omp_parallel_sections_substmt_not_section);
5107 return StmtError();
5108 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005109 cast<OMPSectionDirective>(SectionStmt)
5110 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005111 }
5112 } else {
5113 Diag(AStmt->getLocStart(),
5114 diag::err_omp_parallel_sections_not_compound_stmt);
5115 return StmtError();
5116 }
5117
5118 getCurFunction()->setHasBranchProtectedScope();
5119
Alexey Bataev25e5b442015-09-15 12:52:43 +00005120 return OMPParallelSectionsDirective::Create(
5121 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005122}
5123
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005124StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5125 Stmt *AStmt, SourceLocation StartLoc,
5126 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005127 if (!AStmt)
5128 return StmtError();
5129
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005130 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5131 // 1.2.2 OpenMP Language Terminology
5132 // Structured block - An executable statement with a single entry at the
5133 // top and a single exit at the bottom.
5134 // The point of exit cannot be a branch out of the structured block.
5135 // longjmp() and throw() must not violate the entry/exit criteria.
5136 CS->getCapturedDecl()->setNothrow();
5137
5138 getCurFunction()->setHasBranchProtectedScope();
5139
Alexey Bataev25e5b442015-09-15 12:52:43 +00005140 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5141 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005142}
5143
Alexey Bataev68446b72014-07-18 07:47:19 +00005144StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5145 SourceLocation EndLoc) {
5146 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5147}
5148
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005149StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5150 SourceLocation EndLoc) {
5151 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5152}
5153
Alexey Bataev2df347a2014-07-18 10:17:07 +00005154StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5155 SourceLocation EndLoc) {
5156 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5157}
5158
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005159StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5160 SourceLocation StartLoc,
5161 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005162 if (!AStmt)
5163 return StmtError();
5164
5165 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005166
5167 getCurFunction()->setHasBranchProtectedScope();
5168
5169 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5170}
5171
Alexey Bataev6125da92014-07-21 11:26:11 +00005172StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5173 SourceLocation StartLoc,
5174 SourceLocation EndLoc) {
5175 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5176 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5177}
5178
Alexey Bataev346265e2015-09-25 10:37:12 +00005179StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5180 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005181 SourceLocation StartLoc,
5182 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005183 OMPClause *DependFound = nullptr;
5184 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005185 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005186 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005187 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005188 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005189 for (auto *C : Clauses) {
5190 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5191 DependFound = C;
5192 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5193 if (DependSourceClause) {
5194 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5195 << getOpenMPDirectiveName(OMPD_ordered)
5196 << getOpenMPClauseName(OMPC_depend) << 2;
5197 ErrorFound = true;
5198 } else
5199 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005200 if (DependSinkClause) {
5201 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5202 << 0;
5203 ErrorFound = true;
5204 }
5205 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5206 if (DependSourceClause) {
5207 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5208 << 1;
5209 ErrorFound = true;
5210 }
5211 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005212 }
5213 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005214 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005215 else if (C->getClauseKind() == OMPC_simd)
5216 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005217 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005218 if (!ErrorFound && !SC &&
5219 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005220 // OpenMP [2.8.1,simd Construct, Restrictions]
5221 // An ordered construct with the simd clause is the only OpenMP construct
5222 // that can appear in the simd region.
5223 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005224 ErrorFound = true;
5225 } else if (DependFound && (TC || SC)) {
5226 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5227 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5228 ErrorFound = true;
5229 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5230 Diag(DependFound->getLocStart(),
5231 diag::err_omp_ordered_directive_without_param);
5232 ErrorFound = true;
5233 } else if (TC || Clauses.empty()) {
5234 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5235 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5236 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5237 << (TC != nullptr);
5238 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5239 ErrorFound = true;
5240 }
5241 }
5242 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005243 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005244
5245 if (AStmt) {
5246 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5247
5248 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005249 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005250
5251 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005252}
5253
Alexey Bataev1d160b12015-03-13 12:27:31 +00005254namespace {
5255/// \brief Helper class for checking expression in 'omp atomic [update]'
5256/// construct.
5257class OpenMPAtomicUpdateChecker {
5258 /// \brief Error results for atomic update expressions.
5259 enum ExprAnalysisErrorCode {
5260 /// \brief A statement is not an expression statement.
5261 NotAnExpression,
5262 /// \brief Expression is not builtin binary or unary operation.
5263 NotABinaryOrUnaryExpression,
5264 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5265 NotAnUnaryIncDecExpression,
5266 /// \brief An expression is not of scalar type.
5267 NotAScalarType,
5268 /// \brief A binary operation is not an assignment operation.
5269 NotAnAssignmentOp,
5270 /// \brief RHS part of the binary operation is not a binary expression.
5271 NotABinaryExpression,
5272 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5273 /// expression.
5274 NotABinaryOperator,
5275 /// \brief RHS binary operation does not have reference to the updated LHS
5276 /// part.
5277 NotAnUpdateExpression,
5278 /// \brief No errors is found.
5279 NoError
5280 };
5281 /// \brief Reference to Sema.
5282 Sema &SemaRef;
5283 /// \brief A location for note diagnostics (when error is found).
5284 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005285 /// \brief 'x' lvalue part of the source atomic expression.
5286 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005287 /// \brief 'expr' rvalue part of the source atomic expression.
5288 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005289 /// \brief Helper expression of the form
5290 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5291 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5292 Expr *UpdateExpr;
5293 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5294 /// important for non-associative operations.
5295 bool IsXLHSInRHSPart;
5296 BinaryOperatorKind Op;
5297 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005298 /// \brief true if the source expression is a postfix unary operation, false
5299 /// if it is a prefix unary operation.
5300 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005301
5302public:
5303 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005304 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005305 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005306 /// \brief Check specified statement that it is suitable for 'atomic update'
5307 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005308 /// expression. If DiagId and NoteId == 0, then only check is performed
5309 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005310 /// \param DiagId Diagnostic which should be emitted if error is found.
5311 /// \param NoteId Diagnostic note for the main error message.
5312 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005313 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005314 /// \brief Return the 'x' lvalue part of the source atomic expression.
5315 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005316 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5317 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005318 /// \brief Return the update expression used in calculation of the updated
5319 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5320 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5321 Expr *getUpdateExpr() const { return UpdateExpr; }
5322 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5323 /// false otherwise.
5324 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5325
Alexey Bataevb78ca832015-04-01 03:33:17 +00005326 /// \brief true if the source expression is a postfix unary operation, false
5327 /// if it is a prefix unary operation.
5328 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5329
Alexey Bataev1d160b12015-03-13 12:27:31 +00005330private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005331 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5332 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005333};
5334} // namespace
5335
5336bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5337 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5338 ExprAnalysisErrorCode ErrorFound = NoError;
5339 SourceLocation ErrorLoc, NoteLoc;
5340 SourceRange ErrorRange, NoteRange;
5341 // Allowed constructs are:
5342 // x = x binop expr;
5343 // x = expr binop x;
5344 if (AtomicBinOp->getOpcode() == BO_Assign) {
5345 X = AtomicBinOp->getLHS();
5346 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5347 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5348 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5349 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5350 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005351 Op = AtomicInnerBinOp->getOpcode();
5352 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005353 auto *LHS = AtomicInnerBinOp->getLHS();
5354 auto *RHS = AtomicInnerBinOp->getRHS();
5355 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5356 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5357 /*Canonical=*/true);
5358 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5359 /*Canonical=*/true);
5360 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5361 /*Canonical=*/true);
5362 if (XId == LHSId) {
5363 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005364 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005365 } else if (XId == RHSId) {
5366 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005367 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005368 } else {
5369 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5370 ErrorRange = AtomicInnerBinOp->getSourceRange();
5371 NoteLoc = X->getExprLoc();
5372 NoteRange = X->getSourceRange();
5373 ErrorFound = NotAnUpdateExpression;
5374 }
5375 } else {
5376 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5377 ErrorRange = AtomicInnerBinOp->getSourceRange();
5378 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5379 NoteRange = SourceRange(NoteLoc, NoteLoc);
5380 ErrorFound = NotABinaryOperator;
5381 }
5382 } else {
5383 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5384 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5385 ErrorFound = NotABinaryExpression;
5386 }
5387 } else {
5388 ErrorLoc = AtomicBinOp->getExprLoc();
5389 ErrorRange = AtomicBinOp->getSourceRange();
5390 NoteLoc = AtomicBinOp->getOperatorLoc();
5391 NoteRange = SourceRange(NoteLoc, NoteLoc);
5392 ErrorFound = NotAnAssignmentOp;
5393 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005394 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005395 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5396 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5397 return true;
5398 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005399 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005400 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005401}
5402
5403bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5404 unsigned NoteId) {
5405 ExprAnalysisErrorCode ErrorFound = NoError;
5406 SourceLocation ErrorLoc, NoteLoc;
5407 SourceRange ErrorRange, NoteRange;
5408 // Allowed constructs are:
5409 // x++;
5410 // x--;
5411 // ++x;
5412 // --x;
5413 // x binop= expr;
5414 // x = x binop expr;
5415 // x = expr binop x;
5416 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5417 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5418 if (AtomicBody->getType()->isScalarType() ||
5419 AtomicBody->isInstantiationDependent()) {
5420 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5421 AtomicBody->IgnoreParenImpCasts())) {
5422 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005423 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005424 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005425 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005426 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005427 X = AtomicCompAssignOp->getLHS();
5428 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005429 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5430 AtomicBody->IgnoreParenImpCasts())) {
5431 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005432 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5433 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005434 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005435 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5436 // Check for Unary Operation
5437 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005438 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005439 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5440 OpLoc = AtomicUnaryOp->getOperatorLoc();
5441 X = AtomicUnaryOp->getSubExpr();
5442 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5443 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005444 } else {
5445 ErrorFound = NotAnUnaryIncDecExpression;
5446 ErrorLoc = AtomicUnaryOp->getExprLoc();
5447 ErrorRange = AtomicUnaryOp->getSourceRange();
5448 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5449 NoteRange = SourceRange(NoteLoc, NoteLoc);
5450 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005451 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005452 ErrorFound = NotABinaryOrUnaryExpression;
5453 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5454 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5455 }
5456 } else {
5457 ErrorFound = NotAScalarType;
5458 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5459 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5460 }
5461 } else {
5462 ErrorFound = NotAnExpression;
5463 NoteLoc = ErrorLoc = S->getLocStart();
5464 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5465 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005466 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005467 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5468 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5469 return true;
5470 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005471 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005472 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005473 // Build an update expression of form 'OpaqueValueExpr(x) binop
5474 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5475 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5476 auto *OVEX = new (SemaRef.getASTContext())
5477 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5478 auto *OVEExpr = new (SemaRef.getASTContext())
5479 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5480 auto Update =
5481 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5482 IsXLHSInRHSPart ? OVEExpr : OVEX);
5483 if (Update.isInvalid())
5484 return true;
5485 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5486 Sema::AA_Casting);
5487 if (Update.isInvalid())
5488 return true;
5489 UpdateExpr = Update.get();
5490 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005491 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005492}
5493
Alexey Bataev0162e452014-07-22 10:10:35 +00005494StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5495 Stmt *AStmt,
5496 SourceLocation StartLoc,
5497 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005498 if (!AStmt)
5499 return StmtError();
5500
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005501 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005502 // 1.2.2 OpenMP Language Terminology
5503 // Structured block - An executable statement with a single entry at the
5504 // top and a single exit at the bottom.
5505 // The point of exit cannot be a branch out of the structured block.
5506 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005507 OpenMPClauseKind AtomicKind = OMPC_unknown;
5508 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005509 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005510 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005511 C->getClauseKind() == OMPC_update ||
5512 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005513 if (AtomicKind != OMPC_unknown) {
5514 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5515 << SourceRange(C->getLocStart(), C->getLocEnd());
5516 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5517 << getOpenMPClauseName(AtomicKind);
5518 } else {
5519 AtomicKind = C->getClauseKind();
5520 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005521 }
5522 }
5523 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005524
Alexey Bataev459dec02014-07-24 06:46:57 +00005525 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005526 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5527 Body = EWC->getSubExpr();
5528
Alexey Bataev62cec442014-11-18 10:14:22 +00005529 Expr *X = nullptr;
5530 Expr *V = nullptr;
5531 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005532 Expr *UE = nullptr;
5533 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005534 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005535 // OpenMP [2.12.6, atomic Construct]
5536 // In the next expressions:
5537 // * x and v (as applicable) are both l-value expressions with scalar type.
5538 // * During the execution of an atomic region, multiple syntactic
5539 // occurrences of x must designate the same storage location.
5540 // * Neither of v and expr (as applicable) may access the storage location
5541 // designated by x.
5542 // * Neither of x and expr (as applicable) may access the storage location
5543 // designated by v.
5544 // * expr is an expression with scalar type.
5545 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5546 // * binop, binop=, ++, and -- are not overloaded operators.
5547 // * The expression x binop expr must be numerically equivalent to x binop
5548 // (expr). This requirement is satisfied if the operators in expr have
5549 // precedence greater than binop, or by using parentheses around expr or
5550 // subexpressions of expr.
5551 // * The expression expr binop x must be numerically equivalent to (expr)
5552 // binop x. This requirement is satisfied if the operators in expr have
5553 // precedence equal to or greater than binop, or by using parentheses around
5554 // expr or subexpressions of expr.
5555 // * For forms that allow multiple occurrences of x, the number of times
5556 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005557 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005558 enum {
5559 NotAnExpression,
5560 NotAnAssignmentOp,
5561 NotAScalarType,
5562 NotAnLValue,
5563 NoError
5564 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005565 SourceLocation ErrorLoc, NoteLoc;
5566 SourceRange ErrorRange, NoteRange;
5567 // If clause is read:
5568 // v = x;
5569 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5570 auto AtomicBinOp =
5571 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5572 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5573 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5574 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5575 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5576 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5577 if (!X->isLValue() || !V->isLValue()) {
5578 auto NotLValueExpr = X->isLValue() ? V : X;
5579 ErrorFound = NotAnLValue;
5580 ErrorLoc = AtomicBinOp->getExprLoc();
5581 ErrorRange = AtomicBinOp->getSourceRange();
5582 NoteLoc = NotLValueExpr->getExprLoc();
5583 NoteRange = NotLValueExpr->getSourceRange();
5584 }
5585 } else if (!X->isInstantiationDependent() ||
5586 !V->isInstantiationDependent()) {
5587 auto NotScalarExpr =
5588 (X->isInstantiationDependent() || X->getType()->isScalarType())
5589 ? V
5590 : X;
5591 ErrorFound = NotAScalarType;
5592 ErrorLoc = AtomicBinOp->getExprLoc();
5593 ErrorRange = AtomicBinOp->getSourceRange();
5594 NoteLoc = NotScalarExpr->getExprLoc();
5595 NoteRange = NotScalarExpr->getSourceRange();
5596 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005597 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005598 ErrorFound = NotAnAssignmentOp;
5599 ErrorLoc = AtomicBody->getExprLoc();
5600 ErrorRange = AtomicBody->getSourceRange();
5601 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5602 : AtomicBody->getExprLoc();
5603 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5604 : AtomicBody->getSourceRange();
5605 }
5606 } else {
5607 ErrorFound = NotAnExpression;
5608 NoteLoc = ErrorLoc = Body->getLocStart();
5609 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005610 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005611 if (ErrorFound != NoError) {
5612 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5613 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005614 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5615 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005616 return StmtError();
5617 } else if (CurContext->isDependentContext())
5618 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005619 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005620 enum {
5621 NotAnExpression,
5622 NotAnAssignmentOp,
5623 NotAScalarType,
5624 NotAnLValue,
5625 NoError
5626 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005627 SourceLocation ErrorLoc, NoteLoc;
5628 SourceRange ErrorRange, NoteRange;
5629 // If clause is write:
5630 // x = expr;
5631 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5632 auto AtomicBinOp =
5633 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5634 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005635 X = AtomicBinOp->getLHS();
5636 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005637 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5638 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5639 if (!X->isLValue()) {
5640 ErrorFound = NotAnLValue;
5641 ErrorLoc = AtomicBinOp->getExprLoc();
5642 ErrorRange = AtomicBinOp->getSourceRange();
5643 NoteLoc = X->getExprLoc();
5644 NoteRange = X->getSourceRange();
5645 }
5646 } else if (!X->isInstantiationDependent() ||
5647 !E->isInstantiationDependent()) {
5648 auto NotScalarExpr =
5649 (X->isInstantiationDependent() || X->getType()->isScalarType())
5650 ? E
5651 : X;
5652 ErrorFound = NotAScalarType;
5653 ErrorLoc = AtomicBinOp->getExprLoc();
5654 ErrorRange = AtomicBinOp->getSourceRange();
5655 NoteLoc = NotScalarExpr->getExprLoc();
5656 NoteRange = NotScalarExpr->getSourceRange();
5657 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005658 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005659 ErrorFound = NotAnAssignmentOp;
5660 ErrorLoc = AtomicBody->getExprLoc();
5661 ErrorRange = AtomicBody->getSourceRange();
5662 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5663 : AtomicBody->getExprLoc();
5664 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5665 : AtomicBody->getSourceRange();
5666 }
5667 } else {
5668 ErrorFound = NotAnExpression;
5669 NoteLoc = ErrorLoc = Body->getLocStart();
5670 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005671 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005672 if (ErrorFound != NoError) {
5673 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5674 << ErrorRange;
5675 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5676 << NoteRange;
5677 return StmtError();
5678 } else if (CurContext->isDependentContext())
5679 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005680 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005681 // If clause is update:
5682 // x++;
5683 // x--;
5684 // ++x;
5685 // --x;
5686 // x binop= expr;
5687 // x = x binop expr;
5688 // x = expr binop x;
5689 OpenMPAtomicUpdateChecker Checker(*this);
5690 if (Checker.checkStatement(
5691 Body, (AtomicKind == OMPC_update)
5692 ? diag::err_omp_atomic_update_not_expression_statement
5693 : diag::err_omp_atomic_not_expression_statement,
5694 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005695 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005696 if (!CurContext->isDependentContext()) {
5697 E = Checker.getExpr();
5698 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005699 UE = Checker.getUpdateExpr();
5700 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005701 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005702 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005703 enum {
5704 NotAnAssignmentOp,
5705 NotACompoundStatement,
5706 NotTwoSubstatements,
5707 NotASpecificExpression,
5708 NoError
5709 } ErrorFound = NoError;
5710 SourceLocation ErrorLoc, NoteLoc;
5711 SourceRange ErrorRange, NoteRange;
5712 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5713 // If clause is a capture:
5714 // v = x++;
5715 // v = x--;
5716 // v = ++x;
5717 // v = --x;
5718 // v = x binop= expr;
5719 // v = x = x binop expr;
5720 // v = x = expr binop x;
5721 auto *AtomicBinOp =
5722 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5723 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5724 V = AtomicBinOp->getLHS();
5725 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5726 OpenMPAtomicUpdateChecker Checker(*this);
5727 if (Checker.checkStatement(
5728 Body, diag::err_omp_atomic_capture_not_expression_statement,
5729 diag::note_omp_atomic_update))
5730 return StmtError();
5731 E = Checker.getExpr();
5732 X = Checker.getX();
5733 UE = Checker.getUpdateExpr();
5734 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5735 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005736 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005737 ErrorLoc = AtomicBody->getExprLoc();
5738 ErrorRange = AtomicBody->getSourceRange();
5739 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5740 : AtomicBody->getExprLoc();
5741 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5742 : AtomicBody->getSourceRange();
5743 ErrorFound = NotAnAssignmentOp;
5744 }
5745 if (ErrorFound != NoError) {
5746 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5747 << ErrorRange;
5748 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5749 return StmtError();
5750 } else if (CurContext->isDependentContext()) {
5751 UE = V = E = X = nullptr;
5752 }
5753 } else {
5754 // If clause is a capture:
5755 // { v = x; x = expr; }
5756 // { v = x; x++; }
5757 // { v = x; x--; }
5758 // { v = x; ++x; }
5759 // { v = x; --x; }
5760 // { v = x; x binop= expr; }
5761 // { v = x; x = x binop expr; }
5762 // { v = x; x = expr binop x; }
5763 // { x++; v = x; }
5764 // { x--; v = x; }
5765 // { ++x; v = x; }
5766 // { --x; v = x; }
5767 // { x binop= expr; v = x; }
5768 // { x = x binop expr; v = x; }
5769 // { x = expr binop x; v = x; }
5770 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5771 // Check that this is { expr1; expr2; }
5772 if (CS->size() == 2) {
5773 auto *First = CS->body_front();
5774 auto *Second = CS->body_back();
5775 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5776 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5777 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5778 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5779 // Need to find what subexpression is 'v' and what is 'x'.
5780 OpenMPAtomicUpdateChecker Checker(*this);
5781 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5782 BinaryOperator *BinOp = nullptr;
5783 if (IsUpdateExprFound) {
5784 BinOp = dyn_cast<BinaryOperator>(First);
5785 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5786 }
5787 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5788 // { v = x; x++; }
5789 // { v = x; x--; }
5790 // { v = x; ++x; }
5791 // { v = x; --x; }
5792 // { v = x; x binop= expr; }
5793 // { v = x; x = x binop expr; }
5794 // { v = x; x = expr binop x; }
5795 // Check that the first expression has form v = x.
5796 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5797 llvm::FoldingSetNodeID XId, PossibleXId;
5798 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5799 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5800 IsUpdateExprFound = XId == PossibleXId;
5801 if (IsUpdateExprFound) {
5802 V = BinOp->getLHS();
5803 X = Checker.getX();
5804 E = Checker.getExpr();
5805 UE = Checker.getUpdateExpr();
5806 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005807 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005808 }
5809 }
5810 if (!IsUpdateExprFound) {
5811 IsUpdateExprFound = !Checker.checkStatement(First);
5812 BinOp = nullptr;
5813 if (IsUpdateExprFound) {
5814 BinOp = dyn_cast<BinaryOperator>(Second);
5815 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5816 }
5817 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5818 // { x++; v = x; }
5819 // { x--; v = x; }
5820 // { ++x; v = x; }
5821 // { --x; v = x; }
5822 // { x binop= expr; v = x; }
5823 // { x = x binop expr; v = x; }
5824 // { x = expr binop x; v = x; }
5825 // Check that the second expression has form v = x.
5826 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5827 llvm::FoldingSetNodeID XId, PossibleXId;
5828 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5829 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5830 IsUpdateExprFound = XId == PossibleXId;
5831 if (IsUpdateExprFound) {
5832 V = BinOp->getLHS();
5833 X = Checker.getX();
5834 E = Checker.getExpr();
5835 UE = Checker.getUpdateExpr();
5836 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005837 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005838 }
5839 }
5840 }
5841 if (!IsUpdateExprFound) {
5842 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005843 auto *FirstExpr = dyn_cast<Expr>(First);
5844 auto *SecondExpr = dyn_cast<Expr>(Second);
5845 if (!FirstExpr || !SecondExpr ||
5846 !(FirstExpr->isInstantiationDependent() ||
5847 SecondExpr->isInstantiationDependent())) {
5848 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5849 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005850 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005851 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5852 : First->getLocStart();
5853 NoteRange = ErrorRange = FirstBinOp
5854 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005855 : SourceRange(ErrorLoc, ErrorLoc);
5856 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005857 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5858 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5859 ErrorFound = NotAnAssignmentOp;
5860 NoteLoc = ErrorLoc = SecondBinOp
5861 ? SecondBinOp->getOperatorLoc()
5862 : Second->getLocStart();
5863 NoteRange = ErrorRange =
5864 SecondBinOp ? SecondBinOp->getSourceRange()
5865 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005866 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005867 auto *PossibleXRHSInFirst =
5868 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5869 auto *PossibleXLHSInSecond =
5870 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5871 llvm::FoldingSetNodeID X1Id, X2Id;
5872 PossibleXRHSInFirst->Profile(X1Id, Context,
5873 /*Canonical=*/true);
5874 PossibleXLHSInSecond->Profile(X2Id, Context,
5875 /*Canonical=*/true);
5876 IsUpdateExprFound = X1Id == X2Id;
5877 if (IsUpdateExprFound) {
5878 V = FirstBinOp->getLHS();
5879 X = SecondBinOp->getLHS();
5880 E = SecondBinOp->getRHS();
5881 UE = nullptr;
5882 IsXLHSInRHSPart = false;
5883 IsPostfixUpdate = true;
5884 } else {
5885 ErrorFound = NotASpecificExpression;
5886 ErrorLoc = FirstBinOp->getExprLoc();
5887 ErrorRange = FirstBinOp->getSourceRange();
5888 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5889 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5890 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005891 }
5892 }
5893 }
5894 }
5895 } else {
5896 NoteLoc = ErrorLoc = Body->getLocStart();
5897 NoteRange = ErrorRange =
5898 SourceRange(Body->getLocStart(), Body->getLocStart());
5899 ErrorFound = NotTwoSubstatements;
5900 }
5901 } else {
5902 NoteLoc = ErrorLoc = Body->getLocStart();
5903 NoteRange = ErrorRange =
5904 SourceRange(Body->getLocStart(), Body->getLocStart());
5905 ErrorFound = NotACompoundStatement;
5906 }
5907 if (ErrorFound != NoError) {
5908 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5909 << ErrorRange;
5910 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5911 return StmtError();
5912 } else if (CurContext->isDependentContext()) {
5913 UE = V = E = X = nullptr;
5914 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005915 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005916 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005917
5918 getCurFunction()->setHasBranchProtectedScope();
5919
Alexey Bataev62cec442014-11-18 10:14:22 +00005920 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005921 X, V, E, UE, IsXLHSInRHSPart,
5922 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005923}
5924
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005925StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5926 Stmt *AStmt,
5927 SourceLocation StartLoc,
5928 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005929 if (!AStmt)
5930 return StmtError();
5931
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005932 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5933 // 1.2.2 OpenMP Language Terminology
5934 // Structured block - An executable statement with a single entry at the
5935 // top and a single exit at the bottom.
5936 // The point of exit cannot be a branch out of the structured block.
5937 // longjmp() and throw() must not violate the entry/exit criteria.
5938 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005939
Alexey Bataev13314bf2014-10-09 04:18:56 +00005940 // OpenMP [2.16, Nesting of Regions]
5941 // If specified, a teams construct must be contained within a target
5942 // construct. That target construct must contain no statements or directives
5943 // outside of the teams construct.
5944 if (DSAStack->hasInnerTeamsRegion()) {
5945 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5946 bool OMPTeamsFound = true;
5947 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5948 auto I = CS->body_begin();
5949 while (I != CS->body_end()) {
5950 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5951 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5952 OMPTeamsFound = false;
5953 break;
5954 }
5955 ++I;
5956 }
5957 assert(I != CS->body_end() && "Not found statement");
5958 S = *I;
5959 }
5960 if (!OMPTeamsFound) {
5961 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5962 Diag(DSAStack->getInnerTeamsRegionLoc(),
5963 diag::note_omp_nested_teams_construct_here);
5964 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5965 << isa<OMPExecutableDirective>(S);
5966 return StmtError();
5967 }
5968 }
5969
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005970 getCurFunction()->setHasBranchProtectedScope();
5971
5972 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5973}
5974
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005975StmtResult
5976Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5977 Stmt *AStmt, SourceLocation StartLoc,
5978 SourceLocation EndLoc) {
5979 if (!AStmt)
5980 return StmtError();
5981
5982 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5983 // 1.2.2 OpenMP Language Terminology
5984 // Structured block - An executable statement with a single entry at the
5985 // top and a single exit at the bottom.
5986 // The point of exit cannot be a branch out of the structured block.
5987 // longjmp() and throw() must not violate the entry/exit criteria.
5988 CS->getCapturedDecl()->setNothrow();
5989
5990 getCurFunction()->setHasBranchProtectedScope();
5991
5992 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5993 AStmt);
5994}
5995
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005996StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5997 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5998 SourceLocation EndLoc,
5999 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6000 if (!AStmt)
6001 return StmtError();
6002
6003 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6004 // 1.2.2 OpenMP Language Terminology
6005 // Structured block - An executable statement with a single entry at the
6006 // top and a single exit at the bottom.
6007 // The point of exit cannot be a branch out of the structured block.
6008 // longjmp() and throw() must not violate the entry/exit criteria.
6009 CS->getCapturedDecl()->setNothrow();
6010
6011 OMPLoopDirective::HelperExprs B;
6012 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6013 // define the nested loops number.
6014 unsigned NestedLoopCount =
6015 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6016 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6017 VarsWithImplicitDSA, B);
6018 if (NestedLoopCount == 0)
6019 return StmtError();
6020
6021 assert((CurContext->isDependentContext() || B.builtAll()) &&
6022 "omp target parallel for loop exprs were not built");
6023
6024 if (!CurContext->isDependentContext()) {
6025 // Finalize the clauses that need pre-built expressions for CodeGen.
6026 for (auto C : Clauses) {
6027 if (auto LC = dyn_cast<OMPLinearClause>(C))
6028 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6029 B.NumIterations, *this, CurScope))
6030 return StmtError();
6031 }
6032 }
6033
6034 getCurFunction()->setHasBranchProtectedScope();
6035 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6036 NestedLoopCount, Clauses, AStmt,
6037 B, DSAStack->isCancelRegion());
6038}
6039
Samuel Antaodf67fc42016-01-19 19:15:56 +00006040/// \brief Check for existence of a map clause in the list of clauses.
6041static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6042 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6043 I != E; ++I) {
6044 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6045 return true;
6046 }
6047 }
6048
6049 return false;
6050}
6051
Michael Wong65f367f2015-07-21 13:44:28 +00006052StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6053 Stmt *AStmt,
6054 SourceLocation StartLoc,
6055 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006056 if (!AStmt)
6057 return StmtError();
6058
6059 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6060
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006061 // OpenMP [2.10.1, Restrictions, p. 97]
6062 // At least one map clause must appear on the directive.
6063 if (!HasMapClause(Clauses)) {
6064 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6065 getOpenMPDirectiveName(OMPD_target_data);
6066 return StmtError();
6067 }
6068
Michael Wong65f367f2015-07-21 13:44:28 +00006069 getCurFunction()->setHasBranchProtectedScope();
6070
6071 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6072 AStmt);
6073}
6074
Samuel Antaodf67fc42016-01-19 19:15:56 +00006075StmtResult
6076Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6077 SourceLocation StartLoc,
6078 SourceLocation EndLoc) {
6079 // OpenMP [2.10.2, Restrictions, p. 99]
6080 // At least one map clause must appear on the directive.
6081 if (!HasMapClause(Clauses)) {
6082 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6083 << getOpenMPDirectiveName(OMPD_target_enter_data);
6084 return StmtError();
6085 }
6086
6087 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6088 Clauses);
6089}
6090
Samuel Antao72590762016-01-19 20:04:50 +00006091StmtResult
6092Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6093 SourceLocation StartLoc,
6094 SourceLocation EndLoc) {
6095 // OpenMP [2.10.3, Restrictions, p. 102]
6096 // At least one map clause must appear on the directive.
6097 if (!HasMapClause(Clauses)) {
6098 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6099 << getOpenMPDirectiveName(OMPD_target_exit_data);
6100 return StmtError();
6101 }
6102
6103 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6104}
6105
Alexey Bataev13314bf2014-10-09 04:18:56 +00006106StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6107 Stmt *AStmt, SourceLocation StartLoc,
6108 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006109 if (!AStmt)
6110 return StmtError();
6111
Alexey Bataev13314bf2014-10-09 04:18:56 +00006112 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6113 // 1.2.2 OpenMP Language Terminology
6114 // Structured block - An executable statement with a single entry at the
6115 // top and a single exit at the bottom.
6116 // The point of exit cannot be a branch out of the structured block.
6117 // longjmp() and throw() must not violate the entry/exit criteria.
6118 CS->getCapturedDecl()->setNothrow();
6119
6120 getCurFunction()->setHasBranchProtectedScope();
6121
6122 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6123}
6124
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006125StmtResult
6126Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6127 SourceLocation EndLoc,
6128 OpenMPDirectiveKind CancelRegion) {
6129 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6130 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6131 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6132 << getOpenMPDirectiveName(CancelRegion);
6133 return StmtError();
6134 }
6135 if (DSAStack->isParentNowaitRegion()) {
6136 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6137 return StmtError();
6138 }
6139 if (DSAStack->isParentOrderedRegion()) {
6140 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6141 return StmtError();
6142 }
6143 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6144 CancelRegion);
6145}
6146
Alexey Bataev87933c72015-09-18 08:07:34 +00006147StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6148 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006149 SourceLocation EndLoc,
6150 OpenMPDirectiveKind CancelRegion) {
6151 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6152 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6153 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6154 << getOpenMPDirectiveName(CancelRegion);
6155 return StmtError();
6156 }
6157 if (DSAStack->isParentNowaitRegion()) {
6158 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6159 return StmtError();
6160 }
6161 if (DSAStack->isParentOrderedRegion()) {
6162 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6163 return StmtError();
6164 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006165 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006166 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6167 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006168}
6169
Alexey Bataev382967a2015-12-08 12:06:20 +00006170static bool checkGrainsizeNumTasksClauses(Sema &S,
6171 ArrayRef<OMPClause *> Clauses) {
6172 OMPClause *PrevClause = nullptr;
6173 bool ErrorFound = false;
6174 for (auto *C : Clauses) {
6175 if (C->getClauseKind() == OMPC_grainsize ||
6176 C->getClauseKind() == OMPC_num_tasks) {
6177 if (!PrevClause)
6178 PrevClause = C;
6179 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6180 S.Diag(C->getLocStart(),
6181 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6182 << getOpenMPClauseName(C->getClauseKind())
6183 << getOpenMPClauseName(PrevClause->getClauseKind());
6184 S.Diag(PrevClause->getLocStart(),
6185 diag::note_omp_previous_grainsize_num_tasks)
6186 << getOpenMPClauseName(PrevClause->getClauseKind());
6187 ErrorFound = true;
6188 }
6189 }
6190 }
6191 return ErrorFound;
6192}
6193
Alexey Bataev49f6e782015-12-01 04:18:41 +00006194StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6195 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6196 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006197 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006198 if (!AStmt)
6199 return StmtError();
6200
6201 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6202 OMPLoopDirective::HelperExprs B;
6203 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6204 // define the nested loops number.
6205 unsigned NestedLoopCount =
6206 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006207 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006208 VarsWithImplicitDSA, B);
6209 if (NestedLoopCount == 0)
6210 return StmtError();
6211
6212 assert((CurContext->isDependentContext() || B.builtAll()) &&
6213 "omp for loop exprs were not built");
6214
Alexey Bataev382967a2015-12-08 12:06:20 +00006215 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6216 // The grainsize clause and num_tasks clause are mutually exclusive and may
6217 // not appear on the same taskloop directive.
6218 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6219 return StmtError();
6220
Alexey Bataev49f6e782015-12-01 04:18:41 +00006221 getCurFunction()->setHasBranchProtectedScope();
6222 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6223 NestedLoopCount, Clauses, AStmt, B);
6224}
6225
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006226StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6227 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6228 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006229 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006230 if (!AStmt)
6231 return StmtError();
6232
6233 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6234 OMPLoopDirective::HelperExprs B;
6235 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6236 // define the nested loops number.
6237 unsigned NestedLoopCount =
6238 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6239 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6240 VarsWithImplicitDSA, B);
6241 if (NestedLoopCount == 0)
6242 return StmtError();
6243
6244 assert((CurContext->isDependentContext() || B.builtAll()) &&
6245 "omp for loop exprs were not built");
6246
Alexey Bataev5a3af132016-03-29 08:58:54 +00006247 if (!CurContext->isDependentContext()) {
6248 // Finalize the clauses that need pre-built expressions for CodeGen.
6249 for (auto C : Clauses) {
6250 if (auto LC = dyn_cast<OMPLinearClause>(C))
6251 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6252 B.NumIterations, *this, CurScope))
6253 return StmtError();
6254 }
6255 }
6256
Alexey Bataev382967a2015-12-08 12:06:20 +00006257 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6258 // The grainsize clause and num_tasks clause are mutually exclusive and may
6259 // not appear on the same taskloop directive.
6260 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6261 return StmtError();
6262
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006263 getCurFunction()->setHasBranchProtectedScope();
6264 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6265 NestedLoopCount, Clauses, AStmt, B);
6266}
6267
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006268StmtResult Sema::ActOnOpenMPDistributeDirective(
6269 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6270 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006271 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006272 if (!AStmt)
6273 return StmtError();
6274
6275 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6276 OMPLoopDirective::HelperExprs B;
6277 // In presence of clause 'collapse' with number of loops, it will
6278 // define the nested loops number.
6279 unsigned NestedLoopCount =
6280 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6281 nullptr /*ordered not a clause on distribute*/, AStmt,
6282 *this, *DSAStack, VarsWithImplicitDSA, B);
6283 if (NestedLoopCount == 0)
6284 return StmtError();
6285
6286 assert((CurContext->isDependentContext() || B.builtAll()) &&
6287 "omp for loop exprs were not built");
6288
6289 getCurFunction()->setHasBranchProtectedScope();
6290 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6291 NestedLoopCount, Clauses, AStmt, B);
6292}
6293
Alexey Bataeved09d242014-05-28 05:53:51 +00006294OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006295 SourceLocation StartLoc,
6296 SourceLocation LParenLoc,
6297 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006298 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006299 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006300 case OMPC_final:
6301 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6302 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006303 case OMPC_num_threads:
6304 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6305 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006306 case OMPC_safelen:
6307 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6308 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006309 case OMPC_simdlen:
6310 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6311 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006312 case OMPC_collapse:
6313 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6314 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006315 case OMPC_ordered:
6316 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6317 break;
Michael Wonge710d542015-08-07 16:16:36 +00006318 case OMPC_device:
6319 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6320 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006321 case OMPC_num_teams:
6322 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6323 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006324 case OMPC_thread_limit:
6325 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6326 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006327 case OMPC_priority:
6328 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6329 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006330 case OMPC_grainsize:
6331 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6332 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006333 case OMPC_num_tasks:
6334 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6335 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006336 case OMPC_hint:
6337 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6338 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006339 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006340 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006341 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006342 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006343 case OMPC_private:
6344 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006345 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006346 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006347 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006348 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006349 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006350 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006351 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006352 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006353 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006354 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006355 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006356 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006357 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006358 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006359 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006360 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006361 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006362 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006363 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006364 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006365 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006366 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006367 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006368 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006369 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006370 llvm_unreachable("Clause is not allowed.");
6371 }
6372 return Res;
6373}
6374
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006375OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6376 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006377 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006378 SourceLocation NameModifierLoc,
6379 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006380 SourceLocation EndLoc) {
6381 Expr *ValExpr = Condition;
6382 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6383 !Condition->isInstantiationDependent() &&
6384 !Condition->containsUnexpandedParameterPack()) {
6385 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006386 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006387 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006388 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006389
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006390 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006391 }
6392
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006393 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6394 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006395}
6396
Alexey Bataev3778b602014-07-17 07:32:53 +00006397OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6398 SourceLocation StartLoc,
6399 SourceLocation LParenLoc,
6400 SourceLocation EndLoc) {
6401 Expr *ValExpr = Condition;
6402 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6403 !Condition->isInstantiationDependent() &&
6404 !Condition->containsUnexpandedParameterPack()) {
6405 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6406 Condition->getExprLoc(), Condition);
6407 if (Val.isInvalid())
6408 return nullptr;
6409
6410 ValExpr = Val.get();
6411 }
6412
6413 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6414}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006415ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6416 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006417 if (!Op)
6418 return ExprError();
6419
6420 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6421 public:
6422 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006423 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006424 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6425 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006426 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6427 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006428 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6429 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006430 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6431 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006432 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6433 QualType T,
6434 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006435 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6436 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006437 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6438 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006439 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006440 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006441 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006442 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6443 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006444 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6445 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006446 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6447 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006448 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006449 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006450 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006451 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6452 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006453 llvm_unreachable("conversion functions are permitted");
6454 }
6455 } ConvertDiagnoser;
6456 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6457}
6458
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006459static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006460 OpenMPClauseKind CKind,
6461 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006462 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6463 !ValExpr->isInstantiationDependent()) {
6464 SourceLocation Loc = ValExpr->getExprLoc();
6465 ExprResult Value =
6466 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6467 if (Value.isInvalid())
6468 return false;
6469
6470 ValExpr = Value.get();
6471 // The expression must evaluate to a non-negative integer value.
6472 llvm::APSInt Result;
6473 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006474 Result.isSigned() &&
6475 !((!StrictlyPositive && Result.isNonNegative()) ||
6476 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006477 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006478 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6479 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006480 return false;
6481 }
6482 }
6483 return true;
6484}
6485
Alexey Bataev568a8332014-03-06 06:15:19 +00006486OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6487 SourceLocation StartLoc,
6488 SourceLocation LParenLoc,
6489 SourceLocation EndLoc) {
6490 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006491
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006492 // OpenMP [2.5, Restrictions]
6493 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006494 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6495 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006496 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006497
Alexey Bataeved09d242014-05-28 05:53:51 +00006498 return new (Context)
6499 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006500}
6501
Alexey Bataev62c87d22014-03-21 04:51:18 +00006502ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006503 OpenMPClauseKind CKind,
6504 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006505 if (!E)
6506 return ExprError();
6507 if (E->isValueDependent() || E->isTypeDependent() ||
6508 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006509 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006510 llvm::APSInt Result;
6511 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6512 if (ICE.isInvalid())
6513 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006514 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6515 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006516 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006517 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6518 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006519 return ExprError();
6520 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006521 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6522 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6523 << E->getSourceRange();
6524 return ExprError();
6525 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006526 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6527 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006528 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006529 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006530 return ICE;
6531}
6532
6533OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6534 SourceLocation LParenLoc,
6535 SourceLocation EndLoc) {
6536 // OpenMP [2.8.1, simd construct, Description]
6537 // The parameter of the safelen clause must be a constant
6538 // positive integer expression.
6539 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6540 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006541 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006542 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006543 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006544}
6545
Alexey Bataev66b15b52015-08-21 11:14:16 +00006546OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6547 SourceLocation LParenLoc,
6548 SourceLocation EndLoc) {
6549 // OpenMP [2.8.1, simd construct, Description]
6550 // The parameter of the simdlen clause must be a constant
6551 // positive integer expression.
6552 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6553 if (Simdlen.isInvalid())
6554 return nullptr;
6555 return new (Context)
6556 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6557}
6558
Alexander Musman64d33f12014-06-04 07:53:32 +00006559OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6560 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006561 SourceLocation LParenLoc,
6562 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006563 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006564 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006565 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006566 // The parameter of the collapse clause must be a constant
6567 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006568 ExprResult NumForLoopsResult =
6569 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6570 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006571 return nullptr;
6572 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006573 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006574}
6575
Alexey Bataev10e775f2015-07-30 11:36:16 +00006576OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6577 SourceLocation EndLoc,
6578 SourceLocation LParenLoc,
6579 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006580 // OpenMP [2.7.1, loop construct, Description]
6581 // OpenMP [2.8.1, simd construct, Description]
6582 // OpenMP [2.9.6, distribute construct, Description]
6583 // The parameter of the ordered clause must be a constant
6584 // positive integer expression if any.
6585 if (NumForLoops && LParenLoc.isValid()) {
6586 ExprResult NumForLoopsResult =
6587 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6588 if (NumForLoopsResult.isInvalid())
6589 return nullptr;
6590 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006591 } else
6592 NumForLoops = nullptr;
6593 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006594 return new (Context)
6595 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6596}
6597
Alexey Bataeved09d242014-05-28 05:53:51 +00006598OMPClause *Sema::ActOnOpenMPSimpleClause(
6599 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6600 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006601 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006602 switch (Kind) {
6603 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006604 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006605 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6606 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006607 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006608 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006609 Res = ActOnOpenMPProcBindClause(
6610 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6611 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006612 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006613 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006614 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006615 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006616 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006617 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006618 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006619 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006620 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006621 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006622 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006623 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006624 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006625 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006626 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006627 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006628 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006629 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006630 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006631 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006632 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006633 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006634 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006635 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006636 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006637 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006638 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006639 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006640 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006641 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006642 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006643 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006644 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006645 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006646 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006647 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006648 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006649 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006650 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006651 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006652 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006653 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006654 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006655 llvm_unreachable("Clause is not allowed.");
6656 }
6657 return Res;
6658}
6659
Alexey Bataev6402bca2015-12-28 07:25:51 +00006660static std::string
6661getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6662 ArrayRef<unsigned> Exclude = llvm::None) {
6663 std::string Values;
6664 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6665 unsigned Skipped = Exclude.size();
6666 auto S = Exclude.begin(), E = Exclude.end();
6667 for (unsigned i = First; i < Last; ++i) {
6668 if (std::find(S, E, i) != E) {
6669 --Skipped;
6670 continue;
6671 }
6672 Values += "'";
6673 Values += getOpenMPSimpleClauseTypeName(K, i);
6674 Values += "'";
6675 if (i == Bound - Skipped)
6676 Values += " or ";
6677 else if (i != Bound + 1 - Skipped)
6678 Values += ", ";
6679 }
6680 return Values;
6681}
6682
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006683OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6684 SourceLocation KindKwLoc,
6685 SourceLocation StartLoc,
6686 SourceLocation LParenLoc,
6687 SourceLocation EndLoc) {
6688 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006689 static_assert(OMPC_DEFAULT_unknown > 0,
6690 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006691 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006692 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6693 /*Last=*/OMPC_DEFAULT_unknown)
6694 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006695 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006696 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006697 switch (Kind) {
6698 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006699 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006700 break;
6701 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006702 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006703 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006704 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006705 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006706 break;
6707 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006708 return new (Context)
6709 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006710}
6711
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006712OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6713 SourceLocation KindKwLoc,
6714 SourceLocation StartLoc,
6715 SourceLocation LParenLoc,
6716 SourceLocation EndLoc) {
6717 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006718 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006719 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6720 /*Last=*/OMPC_PROC_BIND_unknown)
6721 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006722 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006723 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006724 return new (Context)
6725 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006726}
6727
Alexey Bataev56dafe82014-06-20 07:16:17 +00006728OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006729 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006730 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006731 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006732 SourceLocation EndLoc) {
6733 OMPClause *Res = nullptr;
6734 switch (Kind) {
6735 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006736 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6737 assert(Argument.size() == NumberOfElements &&
6738 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006739 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006740 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6741 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6742 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6743 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6744 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006745 break;
6746 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006747 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6748 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6749 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6750 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006751 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006752 case OMPC_dist_schedule:
6753 Res = ActOnOpenMPDistScheduleClause(
6754 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6755 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6756 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006757 case OMPC_defaultmap:
6758 enum { Modifier, DefaultmapKind };
6759 Res = ActOnOpenMPDefaultmapClause(
6760 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6761 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6762 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6763 ArgumentLoc[DefaultmapKind], EndLoc);
6764 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006765 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006766 case OMPC_num_threads:
6767 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006768 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006769 case OMPC_collapse:
6770 case OMPC_default:
6771 case OMPC_proc_bind:
6772 case OMPC_private:
6773 case OMPC_firstprivate:
6774 case OMPC_lastprivate:
6775 case OMPC_shared:
6776 case OMPC_reduction:
6777 case OMPC_linear:
6778 case OMPC_aligned:
6779 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006780 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006781 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006782 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006783 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006784 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006785 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006786 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006787 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006788 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006789 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006790 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006791 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006792 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006793 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006794 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006795 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006796 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006797 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006798 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006799 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006800 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006801 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006802 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006803 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006804 case OMPC_unknown:
6805 llvm_unreachable("Clause is not allowed.");
6806 }
6807 return Res;
6808}
6809
Alexey Bataev6402bca2015-12-28 07:25:51 +00006810static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6811 OpenMPScheduleClauseModifier M2,
6812 SourceLocation M1Loc, SourceLocation M2Loc) {
6813 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6814 SmallVector<unsigned, 2> Excluded;
6815 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6816 Excluded.push_back(M2);
6817 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6818 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6819 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6820 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6821 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6822 << getListOfPossibleValues(OMPC_schedule,
6823 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6824 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6825 Excluded)
6826 << getOpenMPClauseName(OMPC_schedule);
6827 return true;
6828 }
6829 return false;
6830}
6831
Alexey Bataev56dafe82014-06-20 07:16:17 +00006832OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006833 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006834 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006835 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6836 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6837 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6838 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6839 return nullptr;
6840 // OpenMP, 2.7.1, Loop Construct, Restrictions
6841 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6842 // but not both.
6843 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6844 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6845 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6846 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6847 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6848 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6849 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6850 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6851 return nullptr;
6852 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006853 if (Kind == OMPC_SCHEDULE_unknown) {
6854 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006855 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6856 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6857 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6858 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6859 Exclude);
6860 } else {
6861 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6862 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006863 }
6864 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6865 << Values << getOpenMPClauseName(OMPC_schedule);
6866 return nullptr;
6867 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006868 // OpenMP, 2.7.1, Loop Construct, Restrictions
6869 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6870 // schedule(guided).
6871 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6872 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6873 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6874 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6875 diag::err_omp_schedule_nonmonotonic_static);
6876 return nullptr;
6877 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006878 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00006879 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006880 if (ChunkSize) {
6881 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6882 !ChunkSize->isInstantiationDependent() &&
6883 !ChunkSize->containsUnexpandedParameterPack()) {
6884 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6885 ExprResult Val =
6886 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6887 if (Val.isInvalid())
6888 return nullptr;
6889
6890 ValExpr = Val.get();
6891
6892 // OpenMP [2.7.1, Restrictions]
6893 // chunk_size must be a loop invariant integer expression with a positive
6894 // value.
6895 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006896 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6897 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6898 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006899 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006900 return nullptr;
6901 }
6902 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00006903 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
6904 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
6905 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006906 }
6907 }
6908 }
6909
Alexey Bataev6402bca2015-12-28 07:25:51 +00006910 return new (Context)
6911 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00006912 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006913}
6914
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006915OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6916 SourceLocation StartLoc,
6917 SourceLocation EndLoc) {
6918 OMPClause *Res = nullptr;
6919 switch (Kind) {
6920 case OMPC_ordered:
6921 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6922 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006923 case OMPC_nowait:
6924 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6925 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006926 case OMPC_untied:
6927 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6928 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006929 case OMPC_mergeable:
6930 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6931 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006932 case OMPC_read:
6933 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6934 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006935 case OMPC_write:
6936 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6937 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006938 case OMPC_update:
6939 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6940 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006941 case OMPC_capture:
6942 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6943 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006944 case OMPC_seq_cst:
6945 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6946 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006947 case OMPC_threads:
6948 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6949 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006950 case OMPC_simd:
6951 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6952 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006953 case OMPC_nogroup:
6954 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6955 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006956 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006957 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006958 case OMPC_num_threads:
6959 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006960 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006961 case OMPC_collapse:
6962 case OMPC_schedule:
6963 case OMPC_private:
6964 case OMPC_firstprivate:
6965 case OMPC_lastprivate:
6966 case OMPC_shared:
6967 case OMPC_reduction:
6968 case OMPC_linear:
6969 case OMPC_aligned:
6970 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006971 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006972 case OMPC_default:
6973 case OMPC_proc_bind:
6974 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006975 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006976 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006977 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006978 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006979 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006980 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006981 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006982 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006983 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006984 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006985 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006986 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006987 case OMPC_unknown:
6988 llvm_unreachable("Clause is not allowed.");
6989 }
6990 return Res;
6991}
6992
Alexey Bataev236070f2014-06-20 11:19:47 +00006993OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6994 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006995 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006996 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6997}
6998
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006999OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7000 SourceLocation EndLoc) {
7001 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7002}
7003
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007004OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7005 SourceLocation EndLoc) {
7006 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7007}
7008
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007009OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7010 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007011 return new (Context) OMPReadClause(StartLoc, EndLoc);
7012}
7013
Alexey Bataevdea47612014-07-23 07:46:59 +00007014OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7015 SourceLocation EndLoc) {
7016 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7017}
7018
Alexey Bataev67a4f222014-07-23 10:25:33 +00007019OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7020 SourceLocation EndLoc) {
7021 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7022}
7023
Alexey Bataev459dec02014-07-24 06:46:57 +00007024OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7025 SourceLocation EndLoc) {
7026 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7027}
7028
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007029OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7030 SourceLocation EndLoc) {
7031 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7032}
7033
Alexey Bataev346265e2015-09-25 10:37:12 +00007034OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7035 SourceLocation EndLoc) {
7036 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7037}
7038
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007039OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7040 SourceLocation EndLoc) {
7041 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7042}
7043
Alexey Bataevb825de12015-12-07 10:51:44 +00007044OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7045 SourceLocation EndLoc) {
7046 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7047}
7048
Alexey Bataevc5e02582014-06-16 07:08:35 +00007049OMPClause *Sema::ActOnOpenMPVarListClause(
7050 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7051 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7052 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007053 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007054 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7055 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7056 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007057 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007058 switch (Kind) {
7059 case OMPC_private:
7060 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7061 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007062 case OMPC_firstprivate:
7063 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7064 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007065 case OMPC_lastprivate:
7066 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7067 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007068 case OMPC_shared:
7069 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7070 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007071 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007072 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7073 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007074 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007075 case OMPC_linear:
7076 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007077 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007078 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007079 case OMPC_aligned:
7080 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7081 ColonLoc, EndLoc);
7082 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007083 case OMPC_copyin:
7084 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7085 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007086 case OMPC_copyprivate:
7087 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7088 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007089 case OMPC_flush:
7090 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7091 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007092 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007093 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7094 StartLoc, LParenLoc, EndLoc);
7095 break;
7096 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007097 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7098 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7099 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007100 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007101 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007102 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007103 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007104 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007105 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007106 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007107 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007108 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007109 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007110 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007111 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007112 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007113 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007114 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007115 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007116 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007117 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007118 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007119 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007120 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007121 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007122 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007123 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007124 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007125 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007126 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007127 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007128 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007129 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007130 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007131 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007132 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007133 llvm_unreachable("Clause is not allowed.");
7134 }
7135 return Res;
7136}
7137
Alexey Bataev90c228f2016-02-08 09:29:13 +00007138ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007139 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007140 ExprResult Res = BuildDeclRefExpr(
7141 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7142 if (!Res.isUsable())
7143 return ExprError();
7144 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7145 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7146 if (!Res.isUsable())
7147 return ExprError();
7148 }
7149 if (VK != VK_LValue && Res.get()->isGLValue()) {
7150 Res = DefaultLvalueConversion(Res.get());
7151 if (!Res.isUsable())
7152 return ExprError();
7153 }
7154 return Res;
7155}
7156
Alexey Bataev60da77e2016-02-29 05:54:20 +00007157static std::pair<ValueDecl *, bool>
7158getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7159 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007160 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7161 RefExpr->containsUnexpandedParameterPack())
7162 return std::make_pair(nullptr, true);
7163
Alexey Bataevd985eda2016-02-10 11:29:16 +00007164 // OpenMP [3.1, C/C++]
7165 // A list item is a variable name.
7166 // OpenMP [2.9.3.3, Restrictions, p.1]
7167 // A variable that is part of another variable (as an array or
7168 // structure element) cannot appear in a private clause.
7169 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007170 enum {
7171 NoArrayExpr = -1,
7172 ArraySubscript = 0,
7173 OMPArraySection = 1
7174 } IsArrayExpr = NoArrayExpr;
7175 if (AllowArraySection) {
7176 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7177 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7178 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7179 Base = TempASE->getBase()->IgnoreParenImpCasts();
7180 RefExpr = Base;
7181 IsArrayExpr = ArraySubscript;
7182 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7183 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7184 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7185 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7186 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7187 Base = TempASE->getBase()->IgnoreParenImpCasts();
7188 RefExpr = Base;
7189 IsArrayExpr = OMPArraySection;
7190 }
7191 }
7192 ELoc = RefExpr->getExprLoc();
7193 ERange = RefExpr->getSourceRange();
7194 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007195 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7196 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7197 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7198 (S.getCurrentThisType().isNull() || !ME ||
7199 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7200 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007201 if (IsArrayExpr != NoArrayExpr)
7202 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7203 << ERange;
7204 else {
7205 S.Diag(ELoc,
7206 AllowArraySection
7207 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7208 : diag::err_omp_expected_var_name_member_expr)
7209 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7210 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007211 return std::make_pair(nullptr, false);
7212 }
7213 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7214}
7215
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007216OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7217 SourceLocation StartLoc,
7218 SourceLocation LParenLoc,
7219 SourceLocation EndLoc) {
7220 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007221 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007222 for (auto &RefExpr : VarList) {
7223 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007224 SourceLocation ELoc;
7225 SourceRange ERange;
7226 Expr *SimpleRefExpr = RefExpr;
7227 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007228 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007229 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007230 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007231 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007232 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007233 ValueDecl *D = Res.first;
7234 if (!D)
7235 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007236
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007237 QualType Type = D->getType();
7238 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007239
7240 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7241 // A variable that appears in a private clause must not have an incomplete
7242 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007243 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007244 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007245 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007246
Alexey Bataev758e55e2013-09-06 18:03:48 +00007247 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7248 // in a Construct]
7249 // Variables with the predetermined data-sharing attributes may not be
7250 // listed in data-sharing attributes clauses, except for the cases
7251 // listed below. For these exceptions only, listing a predetermined
7252 // variable in a data-sharing attribute clause is allowed and overrides
7253 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007254 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007255 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007256 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7257 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007258 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007259 continue;
7260 }
7261
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007262 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007263 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007264 DSAStack->getCurrentDirective() == OMPD_task) {
7265 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7266 << getOpenMPClauseName(OMPC_private) << Type
7267 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7268 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007269 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007270 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007271 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007272 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007273 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007274 continue;
7275 }
7276
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007277 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7278 // A list item cannot appear in both a map clause and a data-sharing
7279 // attribute clause on the same construct
7280 if (DSAStack->getCurrentDirective() == OMPD_target) {
7281 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7282 [&](Expr *RE) -> bool {return true;})) {
7283 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7284 << getOpenMPClauseName(OMPC_private)
7285 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7286 ReportOriginalDSA(*this, DSAStack, D, DVar);
7287 continue;
7288 }
7289 }
7290
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007291 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7292 // A variable of class type (or array thereof) that appears in a private
7293 // clause requires an accessible, unambiguous default constructor for the
7294 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007295 // Generate helper private variable and initialize it with the default
7296 // value. The address of the original variable is replaced by the address of
7297 // the new private variable in CodeGen. This new variable is not added to
7298 // IdResolver, so the code in the OpenMP region uses original variable for
7299 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007300 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007301 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7302 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007303 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007304 if (VDPrivate->isInvalidDecl())
7305 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007306 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007307 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007308
Alexey Bataev90c228f2016-02-08 09:29:13 +00007309 DeclRefExpr *Ref = nullptr;
7310 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007311 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007312 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7313 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007314 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007315 }
7316
Alexey Bataeved09d242014-05-28 05:53:51 +00007317 if (Vars.empty())
7318 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007319
Alexey Bataev03b340a2014-10-21 03:16:40 +00007320 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7321 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007322}
7323
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007324namespace {
7325class DiagsUninitializedSeveretyRAII {
7326private:
7327 DiagnosticsEngine &Diags;
7328 SourceLocation SavedLoc;
7329 bool IsIgnored;
7330
7331public:
7332 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7333 bool IsIgnored)
7334 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7335 if (!IsIgnored) {
7336 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7337 /*Map*/ diag::Severity::Ignored, Loc);
7338 }
7339 }
7340 ~DiagsUninitializedSeveretyRAII() {
7341 if (!IsIgnored)
7342 Diags.popMappings(SavedLoc);
7343 }
7344};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007345}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007346
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007347OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7348 SourceLocation StartLoc,
7349 SourceLocation LParenLoc,
7350 SourceLocation EndLoc) {
7351 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007352 SmallVector<Expr *, 8> PrivateCopies;
7353 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007354 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007355 bool IsImplicitClause =
7356 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7357 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7358
Alexey Bataeved09d242014-05-28 05:53:51 +00007359 for (auto &RefExpr : VarList) {
7360 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007361 SourceLocation ELoc;
7362 SourceRange ERange;
7363 Expr *SimpleRefExpr = RefExpr;
7364 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007365 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007366 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007367 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007368 PrivateCopies.push_back(nullptr);
7369 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007370 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007371 ValueDecl *D = Res.first;
7372 if (!D)
7373 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007374
Alexey Bataev60da77e2016-02-29 05:54:20 +00007375 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007376 QualType Type = D->getType();
7377 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007378
7379 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7380 // A variable that appears in a private clause must not have an incomplete
7381 // type or a reference type.
7382 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007383 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007384 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007385 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007386
7387 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7388 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007389 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007390 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007391 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007392
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007393 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007394 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007395 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007396 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007397 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007398 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007399 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7400 // A list item that specifies a given variable may not appear in more
7401 // than one clause on the same directive, except that a variable may be
7402 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007403 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007404 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007405 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007406 << getOpenMPClauseName(DVar.CKind)
7407 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007408 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007409 continue;
7410 }
7411
7412 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7413 // in a Construct]
7414 // Variables with the predetermined data-sharing attributes may not be
7415 // listed in data-sharing attributes clauses, except for the cases
7416 // listed below. For these exceptions only, listing a predetermined
7417 // variable in a data-sharing attribute clause is allowed and overrides
7418 // the variable's predetermined data-sharing attributes.
7419 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7420 // in a Construct, C/C++, p.2]
7421 // Variables with const-qualified type having no mutable member may be
7422 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007423 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007424 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7425 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007426 << getOpenMPClauseName(DVar.CKind)
7427 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007428 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007429 continue;
7430 }
7431
Alexey Bataevf29276e2014-06-18 04:14:57 +00007432 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007433 // OpenMP [2.9.3.4, Restrictions, p.2]
7434 // A list item that is private within a parallel region must not appear
7435 // in a firstprivate clause on a worksharing construct if any of the
7436 // worksharing regions arising from the worksharing construct ever bind
7437 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007438 if (isOpenMPWorksharingDirective(CurrDir) &&
7439 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007440 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007441 if (DVar.CKind != OMPC_shared &&
7442 (isOpenMPParallelDirective(DVar.DKind) ||
7443 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007444 Diag(ELoc, diag::err_omp_required_access)
7445 << getOpenMPClauseName(OMPC_firstprivate)
7446 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007447 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007448 continue;
7449 }
7450 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007451 // OpenMP [2.9.3.4, Restrictions, p.3]
7452 // A list item that appears in a reduction clause of a parallel construct
7453 // must not appear in a firstprivate clause on a worksharing or task
7454 // construct if any of the worksharing or task regions arising from the
7455 // worksharing or task construct ever bind to any of the parallel regions
7456 // arising from the parallel construct.
7457 // OpenMP [2.9.3.4, Restrictions, p.4]
7458 // A list item that appears in a reduction clause in worksharing
7459 // construct must not appear in a firstprivate clause in a task construct
7460 // encountered during execution of any of the worksharing regions arising
7461 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007462 if (CurrDir == OMPD_task) {
7463 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007464 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007465 [](OpenMPDirectiveKind K) -> bool {
7466 return isOpenMPParallelDirective(K) ||
7467 isOpenMPWorksharingDirective(K);
7468 },
7469 false);
7470 if (DVar.CKind == OMPC_reduction &&
7471 (isOpenMPParallelDirective(DVar.DKind) ||
7472 isOpenMPWorksharingDirective(DVar.DKind))) {
7473 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7474 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007475 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007476 continue;
7477 }
7478 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007479
7480 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7481 // A list item that is private within a teams region must not appear in a
7482 // firstprivate clause on a distribute construct if any of the distribute
7483 // regions arising from the distribute construct ever bind to any of the
7484 // teams regions arising from the teams construct.
7485 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7486 // A list item that appears in a reduction clause of a teams construct
7487 // must not appear in a firstprivate clause on a distribute construct if
7488 // any of the distribute regions arising from the distribute construct
7489 // ever bind to any of the teams regions arising from the teams construct.
7490 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7491 // A list item may appear in a firstprivate or lastprivate clause but not
7492 // both.
7493 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007494 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007495 [](OpenMPDirectiveKind K) -> bool {
7496 return isOpenMPTeamsDirective(K);
7497 },
7498 false);
7499 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7500 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007501 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007502 continue;
7503 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007504 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007505 [](OpenMPDirectiveKind K) -> bool {
7506 return isOpenMPTeamsDirective(K);
7507 },
7508 false);
7509 if (DVar.CKind == OMPC_reduction &&
7510 isOpenMPTeamsDirective(DVar.DKind)) {
7511 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007512 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007513 continue;
7514 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007515 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007516 if (DVar.CKind == OMPC_lastprivate) {
7517 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007518 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007519 continue;
7520 }
7521 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007522 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7523 // A list item cannot appear in both a map clause and a data-sharing
7524 // attribute clause on the same construct
7525 if (CurrDir == OMPD_target) {
7526 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7527 [&](Expr *RE) -> bool {return true;})) {
7528 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7529 << getOpenMPClauseName(OMPC_firstprivate)
7530 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7531 ReportOriginalDSA(*this, DSAStack, D, DVar);
7532 continue;
7533 }
7534 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007535 }
7536
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007537 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007538 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007539 DSAStack->getCurrentDirective() == OMPD_task) {
7540 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7541 << getOpenMPClauseName(OMPC_firstprivate) << Type
7542 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7543 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007544 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007545 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007546 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007547 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007548 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007549 continue;
7550 }
7551
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007552 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007553 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7554 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007555 // Generate helper private variable and initialize it with the value of the
7556 // original variable. The address of the original variable is replaced by
7557 // the address of the new private variable in the CodeGen. This new variable
7558 // is not added to IdResolver, so the code in the OpenMP region uses
7559 // original variable for proper diagnostics and variable capturing.
7560 Expr *VDInitRefExpr = nullptr;
7561 // For arrays generate initializer for single element and replace it by the
7562 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007563 if (Type->isArrayType()) {
7564 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007565 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007566 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007567 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007568 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007569 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007570 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007571 InitializedEntity Entity =
7572 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007573 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7574
7575 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7576 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7577 if (Result.isInvalid())
7578 VDPrivate->setInvalidDecl();
7579 else
7580 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007581 // Remove temp variable declaration.
7582 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007583 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007584 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7585 ".firstprivate.temp");
7586 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7587 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007588 AddInitializerToDecl(VDPrivate,
7589 DefaultLvalueConversion(VDInitRefExpr).get(),
7590 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007591 }
7592 if (VDPrivate->isInvalidDecl()) {
7593 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007594 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007595 diag::note_omp_task_predetermined_firstprivate_here);
7596 }
7597 continue;
7598 }
7599 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007600 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007601 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7602 RefExpr->getExprLoc());
7603 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007604 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007605 if (TopDVar.CKind == OMPC_lastprivate)
7606 Ref = TopDVar.PrivateCopy;
7607 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007608 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007609 if (!IsOpenMPCapturedDecl(D))
7610 ExprCaptures.push_back(Ref->getDecl());
7611 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007612 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007613 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7614 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007615 PrivateCopies.push_back(VDPrivateRefExpr);
7616 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007617 }
7618
Alexey Bataeved09d242014-05-28 05:53:51 +00007619 if (Vars.empty())
7620 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007621
7622 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007623 Vars, PrivateCopies, Inits,
7624 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007625}
7626
Alexander Musman1bb328c2014-06-04 13:06:39 +00007627OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7628 SourceLocation StartLoc,
7629 SourceLocation LParenLoc,
7630 SourceLocation EndLoc) {
7631 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007632 SmallVector<Expr *, 8> SrcExprs;
7633 SmallVector<Expr *, 8> DstExprs;
7634 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007635 SmallVector<Decl *, 4> ExprCaptures;
7636 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007637 for (auto &RefExpr : VarList) {
7638 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007639 SourceLocation ELoc;
7640 SourceRange ERange;
7641 Expr *SimpleRefExpr = RefExpr;
7642 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007643 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007644 // It will be analyzed later.
7645 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007646 SrcExprs.push_back(nullptr);
7647 DstExprs.push_back(nullptr);
7648 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007649 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007650 ValueDecl *D = Res.first;
7651 if (!D)
7652 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007653
Alexey Bataev74caaf22016-02-20 04:09:36 +00007654 QualType Type = D->getType();
7655 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007656
7657 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7658 // A variable that appears in a lastprivate clause must not have an
7659 // incomplete type or a reference type.
7660 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007661 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007662 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007663 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007664
7665 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7666 // in a Construct]
7667 // Variables with the predetermined data-sharing attributes may not be
7668 // listed in data-sharing attributes clauses, except for the cases
7669 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007670 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007671 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7672 DVar.CKind != OMPC_firstprivate &&
7673 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7674 Diag(ELoc, diag::err_omp_wrong_dsa)
7675 << getOpenMPClauseName(DVar.CKind)
7676 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007677 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007678 continue;
7679 }
7680
Alexey Bataevf29276e2014-06-18 04:14:57 +00007681 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7682 // OpenMP [2.14.3.5, Restrictions, p.2]
7683 // A list item that is private within a parallel region, or that appears in
7684 // the reduction clause of a parallel construct, must not appear in a
7685 // lastprivate clause on a worksharing construct if any of the corresponding
7686 // worksharing regions ever binds to any of the corresponding parallel
7687 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007688 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007689 if (isOpenMPWorksharingDirective(CurrDir) &&
7690 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007691 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007692 if (DVar.CKind != OMPC_shared) {
7693 Diag(ELoc, diag::err_omp_required_access)
7694 << getOpenMPClauseName(OMPC_lastprivate)
7695 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007696 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007697 continue;
7698 }
7699 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007700
7701 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7702 // A list item may appear in a firstprivate or lastprivate clause but not
7703 // both.
7704 if (CurrDir == OMPD_distribute) {
7705 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7706 if (DVar.CKind == OMPC_firstprivate) {
7707 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7708 ReportOriginalDSA(*this, DSAStack, D, DVar);
7709 continue;
7710 }
7711 }
7712
Alexander Musman1bb328c2014-06-04 13:06:39 +00007713 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007714 // A variable of class type (or array thereof) that appears in a
7715 // lastprivate clause requires an accessible, unambiguous default
7716 // constructor for the class type, unless the list item is also specified
7717 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007718 // A variable of class type (or array thereof) that appears in a
7719 // lastprivate clause requires an accessible, unambiguous copy assignment
7720 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007721 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007722 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007723 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007724 D->hasAttrs() ? &D->getAttrs() : nullptr);
7725 auto *PseudoSrcExpr =
7726 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007727 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007728 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007729 D->hasAttrs() ? &D->getAttrs() : nullptr);
7730 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007731 // For arrays generate assignment operation for single element and replace
7732 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007733 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007734 PseudoDstExpr, PseudoSrcExpr);
7735 if (AssignmentOp.isInvalid())
7736 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007737 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007738 /*DiscardedValue=*/true);
7739 if (AssignmentOp.isInvalid())
7740 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007741
Alexey Bataev74caaf22016-02-20 04:09:36 +00007742 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007743 if (!VD) {
7744 if (TopDVar.CKind == OMPC_firstprivate)
7745 Ref = TopDVar.PrivateCopy;
7746 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007747 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007748 if (!IsOpenMPCapturedDecl(D))
7749 ExprCaptures.push_back(Ref->getDecl());
7750 }
7751 if (TopDVar.CKind == OMPC_firstprivate ||
7752 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00007753 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007754 ExprResult RefRes = DefaultLvalueConversion(Ref);
7755 if (!RefRes.isUsable())
7756 continue;
7757 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007758 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
7759 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007760 if (!PostUpdateRes.isUsable())
7761 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00007762 ExprPostUpdates.push_back(
7763 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007764 }
7765 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00007766 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00007767 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
7768 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00007769 SrcExprs.push_back(PseudoSrcExpr);
7770 DstExprs.push_back(PseudoDstExpr);
7771 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007772 }
7773
7774 if (Vars.empty())
7775 return nullptr;
7776
7777 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00007778 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007779 buildPreInits(Context, ExprCaptures),
7780 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00007781}
7782
Alexey Bataev758e55e2013-09-06 18:03:48 +00007783OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7784 SourceLocation StartLoc,
7785 SourceLocation LParenLoc,
7786 SourceLocation EndLoc) {
7787 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007788 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007789 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007790 SourceLocation ELoc;
7791 SourceRange ERange;
7792 Expr *SimpleRefExpr = RefExpr;
7793 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007794 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007795 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007796 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007797 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007798 ValueDecl *D = Res.first;
7799 if (!D)
7800 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007801
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007802 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007803 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7804 // in a Construct]
7805 // Variables with the predetermined data-sharing attributes may not be
7806 // listed in data-sharing attributes clauses, except for the cases
7807 // listed below. For these exceptions only, listing a predetermined
7808 // variable in a data-sharing attribute clause is allowed and overrides
7809 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007810 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007811 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7812 DVar.RefExpr) {
7813 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7814 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007815 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007816 continue;
7817 }
7818
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007819 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00007820 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00007821 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007822 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00007823 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007824 }
7825
Alexey Bataeved09d242014-05-28 05:53:51 +00007826 if (Vars.empty())
7827 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007828
7829 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7830}
7831
Alexey Bataevc5e02582014-06-16 07:08:35 +00007832namespace {
7833class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7834 DSAStackTy *Stack;
7835
7836public:
7837 bool VisitDeclRefExpr(DeclRefExpr *E) {
7838 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007839 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007840 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7841 return false;
7842 if (DVar.CKind != OMPC_unknown)
7843 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007844 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007845 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007846 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007847 return true;
7848 return false;
7849 }
7850 return false;
7851 }
7852 bool VisitStmt(Stmt *S) {
7853 for (auto Child : S->children()) {
7854 if (Child && Visit(Child))
7855 return true;
7856 }
7857 return false;
7858 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007859 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007860};
Alexey Bataev23b69422014-06-18 07:08:49 +00007861} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007862
Alexey Bataev60da77e2016-02-29 05:54:20 +00007863namespace {
7864// Transform MemberExpression for specified FieldDecl of current class to
7865// DeclRefExpr to specified OMPCapturedExprDecl.
7866class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
7867 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
7868 ValueDecl *Field;
7869 DeclRefExpr *CapturedExpr;
7870
7871public:
7872 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
7873 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
7874
7875 ExprResult TransformMemberExpr(MemberExpr *E) {
7876 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
7877 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00007878 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00007879 return CapturedExpr;
7880 }
7881 return BaseTransform::TransformMemberExpr(E);
7882 }
7883 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
7884};
7885} // namespace
7886
Alexey Bataeva839ddd2016-03-17 10:19:46 +00007887template <typename T>
7888static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
7889 const llvm::function_ref<T(ValueDecl *)> &Gen) {
7890 for (auto &Set : Lookups) {
7891 for (auto *D : Set) {
7892 if (auto Res = Gen(cast<ValueDecl>(D)))
7893 return Res;
7894 }
7895 }
7896 return T();
7897}
7898
7899static ExprResult
7900buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
7901 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
7902 const DeclarationNameInfo &ReductionId, QualType Ty,
7903 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
7904 if (ReductionIdScopeSpec.isInvalid())
7905 return ExprError();
7906 SmallVector<UnresolvedSet<8>, 4> Lookups;
7907 if (S) {
7908 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
7909 Lookup.suppressDiagnostics();
7910 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
7911 auto *D = Lookup.getRepresentativeDecl();
7912 do {
7913 S = S->getParent();
7914 } while (S && !S->isDeclScope(D));
7915 if (S)
7916 S = S->getParent();
7917 Lookups.push_back(UnresolvedSet<8>());
7918 Lookups.back().append(Lookup.begin(), Lookup.end());
7919 Lookup.clear();
7920 }
7921 } else if (auto *ULE =
7922 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
7923 Lookups.push_back(UnresolvedSet<8>());
7924 Decl *PrevD = nullptr;
7925 for(auto *D : ULE->decls()) {
7926 if (D == PrevD)
7927 Lookups.push_back(UnresolvedSet<8>());
7928 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
7929 Lookups.back().addDecl(DRD);
7930 PrevD = D;
7931 }
7932 }
7933 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
7934 Ty->containsUnexpandedParameterPack() ||
7935 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
7936 return !D->isInvalidDecl() &&
7937 (D->getType()->isDependentType() ||
7938 D->getType()->isInstantiationDependentType() ||
7939 D->getType()->containsUnexpandedParameterPack());
7940 })) {
7941 UnresolvedSet<8> ResSet;
7942 for (auto &Set : Lookups) {
7943 ResSet.append(Set.begin(), Set.end());
7944 // The last item marks the end of all declarations at the specified scope.
7945 ResSet.addDecl(Set[Set.size() - 1]);
7946 }
7947 return UnresolvedLookupExpr::Create(
7948 SemaRef.Context, /*NamingClass=*/nullptr,
7949 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
7950 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
7951 }
7952 if (auto *VD = filterLookupForUDR<ValueDecl *>(
7953 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
7954 if (!D->isInvalidDecl() &&
7955 SemaRef.Context.hasSameType(D->getType(), Ty))
7956 return D;
7957 return nullptr;
7958 }))
7959 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
7960 if (auto *VD = filterLookupForUDR<ValueDecl *>(
7961 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
7962 if (!D->isInvalidDecl() &&
7963 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
7964 !Ty.isMoreQualifiedThan(D->getType()))
7965 return D;
7966 return nullptr;
7967 })) {
7968 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
7969 /*DetectVirtual=*/false);
7970 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
7971 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
7972 VD->getType().getUnqualifiedType()))) {
7973 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
7974 /*DiagID=*/0) !=
7975 Sema::AR_inaccessible) {
7976 SemaRef.BuildBasePathArray(Paths, BasePath);
7977 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
7978 }
7979 }
7980 }
7981 }
7982 if (ReductionIdScopeSpec.isSet()) {
7983 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
7984 return ExprError();
7985 }
7986 return ExprEmpty();
7987}
7988
Alexey Bataevc5e02582014-06-16 07:08:35 +00007989OMPClause *Sema::ActOnOpenMPReductionClause(
7990 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7991 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00007992 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
7993 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00007994 auto DN = ReductionId.getName();
7995 auto OOK = DN.getCXXOverloadedOperator();
7996 BinaryOperatorKind BOK = BO_Comma;
7997
7998 // OpenMP [2.14.3.6, reduction clause]
7999 // C
8000 // reduction-identifier is either an identifier or one of the following
8001 // operators: +, -, *, &, |, ^, && and ||
8002 // C++
8003 // reduction-identifier is either an id-expression or one of the following
8004 // operators: +, -, *, &, |, ^, && and ||
8005 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8006 switch (OOK) {
8007 case OO_Plus:
8008 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008009 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008010 break;
8011 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008012 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008013 break;
8014 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008015 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008016 break;
8017 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008018 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008019 break;
8020 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008021 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008022 break;
8023 case OO_AmpAmp:
8024 BOK = BO_LAnd;
8025 break;
8026 case OO_PipePipe:
8027 BOK = BO_LOr;
8028 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008029 case OO_New:
8030 case OO_Delete:
8031 case OO_Array_New:
8032 case OO_Array_Delete:
8033 case OO_Slash:
8034 case OO_Percent:
8035 case OO_Tilde:
8036 case OO_Exclaim:
8037 case OO_Equal:
8038 case OO_Less:
8039 case OO_Greater:
8040 case OO_LessEqual:
8041 case OO_GreaterEqual:
8042 case OO_PlusEqual:
8043 case OO_MinusEqual:
8044 case OO_StarEqual:
8045 case OO_SlashEqual:
8046 case OO_PercentEqual:
8047 case OO_CaretEqual:
8048 case OO_AmpEqual:
8049 case OO_PipeEqual:
8050 case OO_LessLess:
8051 case OO_GreaterGreater:
8052 case OO_LessLessEqual:
8053 case OO_GreaterGreaterEqual:
8054 case OO_EqualEqual:
8055 case OO_ExclaimEqual:
8056 case OO_PlusPlus:
8057 case OO_MinusMinus:
8058 case OO_Comma:
8059 case OO_ArrowStar:
8060 case OO_Arrow:
8061 case OO_Call:
8062 case OO_Subscript:
8063 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008064 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008065 case NUM_OVERLOADED_OPERATORS:
8066 llvm_unreachable("Unexpected reduction identifier");
8067 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008068 if (auto II = DN.getAsIdentifierInfo()) {
8069 if (II->isStr("max"))
8070 BOK = BO_GT;
8071 else if (II->isStr("min"))
8072 BOK = BO_LT;
8073 }
8074 break;
8075 }
8076 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008077 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008078 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008079 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008080
8081 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008082 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008083 SmallVector<Expr *, 8> LHSs;
8084 SmallVector<Expr *, 8> RHSs;
8085 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008086 SmallVector<Decl *, 4> ExprCaptures;
8087 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008088 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8089 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008090 for (auto RefExpr : VarList) {
8091 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008092 // OpenMP [2.1, C/C++]
8093 // A list item is a variable or array section, subject to the restrictions
8094 // specified in Section 2.4 on page 42 and in each of the sections
8095 // describing clauses and directives for which a list appears.
8096 // OpenMP [2.14.3.3, Restrictions, p.1]
8097 // A variable that is part of another variable (as an array or
8098 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008099 if (!FirstIter && IR != ER)
8100 ++IR;
8101 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008102 SourceLocation ELoc;
8103 SourceRange ERange;
8104 Expr *SimpleRefExpr = RefExpr;
8105 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8106 /*AllowArraySection=*/true);
8107 if (Res.second) {
8108 // It will be analyzed later.
8109 Vars.push_back(RefExpr);
8110 Privates.push_back(nullptr);
8111 LHSs.push_back(nullptr);
8112 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008113 // Try to find 'declare reduction' corresponding construct before using
8114 // builtin/overloaded operators.
8115 QualType Type = Context.DependentTy;
8116 CXXCastPath BasePath;
8117 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8118 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8119 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8120 if (CurContext->isDependentContext() &&
8121 (DeclareReductionRef.isUnset() ||
8122 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8123 ReductionOps.push_back(DeclareReductionRef.get());
8124 else
8125 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008126 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008127 ValueDecl *D = Res.first;
8128 if (!D)
8129 continue;
8130
Alexey Bataeva1764212015-09-30 09:22:36 +00008131 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008132 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8133 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8134 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008135 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008136 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008137 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8138 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8139 Type = ATy->getElementType();
8140 else
8141 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008142 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008143 } else
8144 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8145 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008146
Alexey Bataevc5e02582014-06-16 07:08:35 +00008147 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8148 // A variable that appears in a private clause must not have an incomplete
8149 // type or a reference type.
8150 if (RequireCompleteType(ELoc, Type,
8151 diag::err_omp_reduction_incomplete_type))
8152 continue;
8153 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008154 // A list item that appears in a reduction clause must not be
8155 // const-qualified.
8156 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008157 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008158 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008159 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008160 bool IsDecl = !VD ||
8161 VD->isThisDeclarationADefinition(Context) ==
8162 VarDecl::DeclarationOnly;
8163 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008164 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008165 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008166 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008167 continue;
8168 }
8169 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8170 // If a list-item is a reference type then it must bind to the same object
8171 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008172 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008173 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008174 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008175 DSARefChecker Check(DSAStack);
8176 if (Check.Visit(VDDef->getInit())) {
8177 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8178 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8179 continue;
8180 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008181 }
8182 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008183
Alexey Bataevc5e02582014-06-16 07:08:35 +00008184 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8185 // in a Construct]
8186 // Variables with the predetermined data-sharing attributes may not be
8187 // listed in data-sharing attributes clauses, except for the cases
8188 // listed below. For these exceptions only, listing a predetermined
8189 // variable in a data-sharing attribute clause is allowed and overrides
8190 // the variable's predetermined data-sharing attributes.
8191 // OpenMP [2.14.3.6, Restrictions, p.3]
8192 // Any number of reduction clauses can be specified on the directive,
8193 // but a list item can appear only once in the reduction clauses for that
8194 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008195 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008196 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008197 if (DVar.CKind == OMPC_reduction) {
8198 Diag(ELoc, diag::err_omp_once_referenced)
8199 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008200 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008201 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008202 } else if (DVar.CKind != OMPC_unknown) {
8203 Diag(ELoc, diag::err_omp_wrong_dsa)
8204 << getOpenMPClauseName(DVar.CKind)
8205 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008206 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008207 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008208 }
8209
8210 // OpenMP [2.14.3.6, Restrictions, p.1]
8211 // A list item that appears in a reduction clause of a worksharing
8212 // construct must be shared in the parallel regions to which any of the
8213 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008214 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8215 if (isOpenMPWorksharingDirective(CurrDir) &&
8216 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008217 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008218 if (DVar.CKind != OMPC_shared) {
8219 Diag(ELoc, diag::err_omp_required_access)
8220 << getOpenMPClauseName(OMPC_reduction)
8221 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008222 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008223 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008224 }
8225 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008226
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008227 // Try to find 'declare reduction' corresponding construct before using
8228 // builtin/overloaded operators.
8229 CXXCastPath BasePath;
8230 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8231 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8232 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8233 if (DeclareReductionRef.isInvalid())
8234 continue;
8235 if (CurContext->isDependentContext() &&
8236 (DeclareReductionRef.isUnset() ||
8237 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8238 Vars.push_back(RefExpr);
8239 Privates.push_back(nullptr);
8240 LHSs.push_back(nullptr);
8241 RHSs.push_back(nullptr);
8242 ReductionOps.push_back(DeclareReductionRef.get());
8243 continue;
8244 }
8245 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8246 // Not allowed reduction identifier is found.
8247 Diag(ReductionId.getLocStart(),
8248 diag::err_omp_unknown_reduction_identifier)
8249 << Type << ReductionIdRange;
8250 continue;
8251 }
8252
8253 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8254 // The type of a list item that appears in a reduction clause must be valid
8255 // for the reduction-identifier. For a max or min reduction in C, the type
8256 // of the list item must be an allowed arithmetic data type: char, int,
8257 // float, double, or _Bool, possibly modified with long, short, signed, or
8258 // unsigned. For a max or min reduction in C++, the type of the list item
8259 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8260 // double, or bool, possibly modified with long, short, signed, or unsigned.
8261 if (DeclareReductionRef.isUnset()) {
8262 if ((BOK == BO_GT || BOK == BO_LT) &&
8263 !(Type->isScalarType() ||
8264 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8265 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8266 << getLangOpts().CPlusPlus;
8267 if (!ASE && !OASE) {
8268 bool IsDecl = !VD ||
8269 VD->isThisDeclarationADefinition(Context) ==
8270 VarDecl::DeclarationOnly;
8271 Diag(D->getLocation(),
8272 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8273 << D;
8274 }
8275 continue;
8276 }
8277 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8278 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8279 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8280 if (!ASE && !OASE) {
8281 bool IsDecl = !VD ||
8282 VD->isThisDeclarationADefinition(Context) ==
8283 VarDecl::DeclarationOnly;
8284 Diag(D->getLocation(),
8285 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8286 << D;
8287 }
8288 continue;
8289 }
8290 }
8291
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008292 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008293 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008294 D->hasAttrs() ? &D->getAttrs() : nullptr);
8295 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8296 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008297 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008298 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008299 (!ASE &&
8300 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008301 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008302 // Create pseudo array type for private copy. The size for this array will
8303 // be generated during codegen.
8304 // For array subscripts or single variables Private Ty is the same as Type
8305 // (type of the variable or single array element).
8306 PrivateTy = Context.getVariableArrayType(
8307 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8308 Context.getSizeType(), VK_RValue),
8309 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008310 } else if (!ASE && !OASE &&
8311 Context.getAsArrayType(D->getType().getNonReferenceType()))
8312 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008313 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008314 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8315 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008316 // Add initializer for private variable.
8317 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008318 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8319 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8320 if (DeclareReductionRef.isUsable()) {
8321 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8322 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8323 if (DRD->getInitializer()) {
8324 Init = DRDRef;
8325 RHSVD->setInit(DRDRef);
8326 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008327 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008328 } else {
8329 switch (BOK) {
8330 case BO_Add:
8331 case BO_Xor:
8332 case BO_Or:
8333 case BO_LOr:
8334 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8335 if (Type->isScalarType() || Type->isAnyComplexType())
8336 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8337 break;
8338 case BO_Mul:
8339 case BO_LAnd:
8340 if (Type->isScalarType() || Type->isAnyComplexType()) {
8341 // '*' and '&&' reduction ops - initializer is '1'.
8342 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008343 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008344 break;
8345 case BO_And: {
8346 // '&' reduction op - initializer is '~0'.
8347 QualType OrigType = Type;
8348 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8349 Type = ComplexTy->getElementType();
8350 if (Type->isRealFloatingType()) {
8351 llvm::APFloat InitValue =
8352 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8353 /*isIEEE=*/true);
8354 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8355 Type, ELoc);
8356 } else if (Type->isScalarType()) {
8357 auto Size = Context.getTypeSize(Type);
8358 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8359 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8360 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8361 }
8362 if (Init && OrigType->isAnyComplexType()) {
8363 // Init = 0xFFFF + 0xFFFFi;
8364 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8365 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8366 }
8367 Type = OrigType;
8368 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008369 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008370 case BO_LT:
8371 case BO_GT: {
8372 // 'min' reduction op - initializer is 'Largest representable number in
8373 // the reduction list item type'.
8374 // 'max' reduction op - initializer is 'Least representable number in
8375 // the reduction list item type'.
8376 if (Type->isIntegerType() || Type->isPointerType()) {
8377 bool IsSigned = Type->hasSignedIntegerRepresentation();
8378 auto Size = Context.getTypeSize(Type);
8379 QualType IntTy =
8380 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8381 llvm::APInt InitValue =
8382 (BOK != BO_LT)
8383 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8384 : llvm::APInt::getMinValue(Size)
8385 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8386 : llvm::APInt::getMaxValue(Size);
8387 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8388 if (Type->isPointerType()) {
8389 // Cast to pointer type.
8390 auto CastExpr = BuildCStyleCastExpr(
8391 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8392 SourceLocation(), Init);
8393 if (CastExpr.isInvalid())
8394 continue;
8395 Init = CastExpr.get();
8396 }
8397 } else if (Type->isRealFloatingType()) {
8398 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8399 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8400 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8401 Type, ELoc);
8402 }
8403 break;
8404 }
8405 case BO_PtrMemD:
8406 case BO_PtrMemI:
8407 case BO_MulAssign:
8408 case BO_Div:
8409 case BO_Rem:
8410 case BO_Sub:
8411 case BO_Shl:
8412 case BO_Shr:
8413 case BO_LE:
8414 case BO_GE:
8415 case BO_EQ:
8416 case BO_NE:
8417 case BO_AndAssign:
8418 case BO_XorAssign:
8419 case BO_OrAssign:
8420 case BO_Assign:
8421 case BO_AddAssign:
8422 case BO_SubAssign:
8423 case BO_DivAssign:
8424 case BO_RemAssign:
8425 case BO_ShlAssign:
8426 case BO_ShrAssign:
8427 case BO_Comma:
8428 llvm_unreachable("Unexpected reduction operation");
8429 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008430 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008431 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008432 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8433 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008434 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008435 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008436 if (RHSVD->isInvalidDecl())
8437 continue;
8438 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008439 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8440 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008441 bool IsDecl =
8442 !VD ||
8443 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8444 Diag(D->getLocation(),
8445 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8446 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008447 continue;
8448 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008449 // Store initializer for single element in private copy. Will be used during
8450 // codegen.
8451 PrivateVD->setInit(RHSVD->getInit());
8452 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008453 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008454 ExprResult ReductionOp;
8455 if (DeclareReductionRef.isUsable()) {
8456 QualType RedTy = DeclareReductionRef.get()->getType();
8457 QualType PtrRedTy = Context.getPointerType(RedTy);
8458 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8459 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8460 if (!BasePath.empty()) {
8461 LHS = DefaultLvalueConversion(LHS.get());
8462 RHS = DefaultLvalueConversion(RHS.get());
8463 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8464 CK_UncheckedDerivedToBase, LHS.get(),
8465 &BasePath, LHS.get()->getValueKind());
8466 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8467 CK_UncheckedDerivedToBase, RHS.get(),
8468 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008469 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008470 FunctionProtoType::ExtProtoInfo EPI;
8471 QualType Params[] = {PtrRedTy, PtrRedTy};
8472 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8473 auto *OVE = new (Context) OpaqueValueExpr(
8474 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8475 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8476 Expr *Args[] = {LHS.get(), RHS.get()};
8477 ReductionOp = new (Context)
8478 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8479 } else {
8480 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8481 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8482 if (ReductionOp.isUsable()) {
8483 if (BOK != BO_LT && BOK != BO_GT) {
8484 ReductionOp =
8485 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8486 BO_Assign, LHSDRE, ReductionOp.get());
8487 } else {
8488 auto *ConditionalOp = new (Context) ConditionalOperator(
8489 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8490 RHSDRE, Type, VK_LValue, OK_Ordinary);
8491 ReductionOp =
8492 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8493 BO_Assign, LHSDRE, ConditionalOp);
8494 }
8495 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8496 }
8497 if (ReductionOp.isInvalid())
8498 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008499 }
8500
Alexey Bataev60da77e2016-02-29 05:54:20 +00008501 DeclRefExpr *Ref = nullptr;
8502 Expr *VarsExpr = RefExpr->IgnoreParens();
8503 if (!VD) {
8504 if (ASE || OASE) {
8505 TransformExprToCaptures RebuildToCapture(*this, D);
8506 VarsExpr =
8507 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8508 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008509 } else {
8510 VarsExpr = Ref =
8511 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008512 }
8513 if (!IsOpenMPCapturedDecl(D)) {
8514 ExprCaptures.push_back(Ref->getDecl());
8515 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8516 ExprResult RefRes = DefaultLvalueConversion(Ref);
8517 if (!RefRes.isUsable())
8518 continue;
8519 ExprResult PostUpdateRes =
8520 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8521 SimpleRefExpr, RefRes.get());
8522 if (!PostUpdateRes.isUsable())
8523 continue;
8524 ExprPostUpdates.push_back(
8525 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008526 }
8527 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008528 }
8529 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8530 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008531 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008532 LHSs.push_back(LHSDRE);
8533 RHSs.push_back(RHSDRE);
8534 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008535 }
8536
8537 if (Vars.empty())
8538 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008539
Alexey Bataevc5e02582014-06-16 07:08:35 +00008540 return OMPReductionClause::Create(
8541 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008542 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008543 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8544 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008545}
8546
Alexey Bataev182227b2015-08-20 10:54:39 +00008547OMPClause *Sema::ActOnOpenMPLinearClause(
8548 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8549 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8550 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008551 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008552 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008553 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008554 SmallVector<Decl *, 4> ExprCaptures;
8555 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataev182227b2015-08-20 10:54:39 +00008556 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8557 LinKind == OMPC_LINEAR_unknown) {
8558 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8559 LinKind = OMPC_LINEAR_val;
8560 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008561 for (auto &RefExpr : VarList) {
8562 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008563 SourceLocation ELoc;
8564 SourceRange ERange;
8565 Expr *SimpleRefExpr = RefExpr;
8566 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8567 /*AllowArraySection=*/false);
8568 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008569 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008570 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008571 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008572 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008573 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008574 ValueDecl *D = Res.first;
8575 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008576 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008577
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008578 QualType Type = D->getType();
8579 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008580
8581 // OpenMP [2.14.3.7, linear clause]
8582 // A list-item cannot appear in more than one linear clause.
8583 // A list-item that appears in a linear clause cannot appear in any
8584 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008585 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008586 if (DVar.RefExpr) {
8587 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8588 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008589 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008590 continue;
8591 }
8592
8593 // A variable must not have an incomplete type or a reference type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008594 if (RequireCompleteType(ELoc, Type,
8595 diag::err_omp_linear_incomplete_type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008596 continue;
Alexey Bataev1185e192015-08-20 12:15:57 +00008597 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008598 !Type->isReferenceType()) {
Alexey Bataev1185e192015-08-20 12:15:57 +00008599 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008600 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
Alexey Bataev1185e192015-08-20 12:15:57 +00008601 continue;
8602 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008603 Type = Type.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008604
8605 // A list item must not be const-qualified.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008606 if (Type.isConstant(Context)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008607 Diag(ELoc, diag::err_omp_const_variable)
8608 << getOpenMPClauseName(OMPC_linear);
8609 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008610 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008611 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008612 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008613 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008614 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008615 continue;
8616 }
8617
8618 // A list item must be of integral or pointer type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008619 Type = Type.getUnqualifiedType().getCanonicalType();
8620 const auto *Ty = Type.getTypePtrOrNull();
Alexander Musman8dba6642014-04-22 13:09:42 +00008621 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8622 !Ty->isPointerType())) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008623 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
Alexander Musman8dba6642014-04-22 13:09:42 +00008624 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008625 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008626 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008627 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008628 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008629 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008630 continue;
8631 }
8632
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008633 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008634 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8635 D->hasAttrs() ? &D->getAttrs() : nullptr);
8636 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008637 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008638 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008639 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008640 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008641 if (!VD) {
8642 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8643 if (!IsOpenMPCapturedDecl(D)) {
8644 ExprCaptures.push_back(Ref->getDecl());
8645 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8646 ExprResult RefRes = DefaultLvalueConversion(Ref);
8647 if (!RefRes.isUsable())
8648 continue;
8649 ExprResult PostUpdateRes =
8650 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8651 SimpleRefExpr, RefRes.get());
8652 if (!PostUpdateRes.isUsable())
8653 continue;
8654 ExprPostUpdates.push_back(
8655 IgnoredValueConversions(PostUpdateRes.get()).get());
8656 }
8657 }
8658 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008659 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008660 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008661 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008662 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008663 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008664 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8665 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8666
8667 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
8668 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008669 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008670 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008671 }
8672
8673 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008674 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008675
8676 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008677 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008678 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8679 !Step->isInstantiationDependent() &&
8680 !Step->containsUnexpandedParameterPack()) {
8681 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008682 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008683 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008684 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008685 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008686
Alexander Musman3276a272015-03-21 10:12:56 +00008687 // Build var to save the step value.
8688 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008689 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008690 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008691 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008692 ExprResult CalcStep =
8693 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008694 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008695
Alexander Musman8dba6642014-04-22 13:09:42 +00008696 // Warn about zero linear step (it would be probably better specified as
8697 // making corresponding variables 'const').
8698 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008699 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8700 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008701 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8702 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008703 if (!IsConstant && CalcStep.isUsable()) {
8704 // Calculate the step beforehand instead of doing this on each iteration.
8705 // (This is not used if the number of iterations may be kfold-ed).
8706 CalcStepExpr = CalcStep.get();
8707 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008708 }
8709
Alexey Bataev182227b2015-08-20 10:54:39 +00008710 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8711 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008712 StepExpr, CalcStepExpr,
8713 buildPreInits(Context, ExprCaptures),
8714 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00008715}
8716
Alexey Bataev5a3af132016-03-29 08:58:54 +00008717static bool
8718FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8719 Expr *NumIterations, Sema &SemaRef, Scope *S) {
Alexander Musman3276a272015-03-21 10:12:56 +00008720 // Walk the vars and build update/final expressions for the CodeGen.
8721 SmallVector<Expr *, 8> Updates;
8722 SmallVector<Expr *, 8> Finals;
8723 Expr *Step = Clause.getStep();
8724 Expr *CalcStep = Clause.getCalcStep();
8725 // OpenMP [2.14.3.7, linear clause]
8726 // If linear-step is not specified it is assumed to be 1.
8727 if (Step == nullptr)
8728 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008729 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00008730 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008731 }
Alexander Musman3276a272015-03-21 10:12:56 +00008732 bool HasErrors = false;
8733 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008734 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008735 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008736 for (auto &RefExpr : Clause.varlists()) {
8737 Expr *InitExpr = *CurInit;
8738
8739 // Build privatized reference to the current linear var.
8740 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008741 Expr *CapturedRef;
8742 if (LinKind == OMPC_LINEAR_uval)
8743 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8744 else
8745 CapturedRef =
8746 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8747 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8748 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008749
8750 // Build update: Var = InitExpr + IV * Step
8751 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008752 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008753 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008754 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8755 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008756
8757 // Build final: Var = InitExpr + NumIterations * Step
8758 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008759 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008760 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008761 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8762 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008763 if (!Update.isUsable() || !Final.isUsable()) {
8764 Updates.push_back(nullptr);
8765 Finals.push_back(nullptr);
8766 HasErrors = true;
8767 } else {
8768 Updates.push_back(Update.get());
8769 Finals.push_back(Final.get());
8770 }
Richard Trieucc3949d2016-02-18 22:34:54 +00008771 ++CurInit;
8772 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008773 }
8774 Clause.setUpdates(Updates);
8775 Clause.setFinals(Finals);
8776 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008777}
8778
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008779OMPClause *Sema::ActOnOpenMPAlignedClause(
8780 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8781 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8782
8783 SmallVector<Expr *, 8> Vars;
8784 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00008785 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8786 SourceLocation ELoc;
8787 SourceRange ERange;
8788 Expr *SimpleRefExpr = RefExpr;
8789 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8790 /*AllowArraySection=*/false);
8791 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008792 // It will be analyzed later.
8793 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008794 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00008795 ValueDecl *D = Res.first;
8796 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008797 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008798
Alexey Bataev1efd1662016-03-29 10:59:56 +00008799 QualType QType = D->getType();
8800 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008801
8802 // OpenMP [2.8.1, simd construct, Restrictions]
8803 // The type of list items appearing in the aligned clause must be
8804 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008805 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008806 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00008807 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008808 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00008809 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008810 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00008811 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008812 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008813 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008814 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00008815 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008816 continue;
8817 }
8818
8819 // OpenMP [2.8.1, simd construct, Restrictions]
8820 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00008821 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
8822 Diag(ELoc, diag::err_omp_aligned_twice) << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008823 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8824 << getOpenMPClauseName(OMPC_aligned);
8825 continue;
8826 }
8827
Alexey Bataev1efd1662016-03-29 10:59:56 +00008828 DeclRefExpr *Ref = nullptr;
8829 if (!VD && IsOpenMPCapturedDecl(D))
8830 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8831 Vars.push_back(DefaultFunctionArrayConversion(
8832 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
8833 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008834 }
8835
8836 // OpenMP [2.8.1, simd construct, Description]
8837 // The parameter of the aligned clause, alignment, must be a constant
8838 // positive integer expression.
8839 // If no optional parameter is specified, implementation-defined default
8840 // alignments for SIMD instructions on the target platforms are assumed.
8841 if (Alignment != nullptr) {
8842 ExprResult AlignResult =
8843 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8844 if (AlignResult.isInvalid())
8845 return nullptr;
8846 Alignment = AlignResult.get();
8847 }
8848 if (Vars.empty())
8849 return nullptr;
8850
8851 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8852 EndLoc, Vars, Alignment);
8853}
8854
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008855OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8856 SourceLocation StartLoc,
8857 SourceLocation LParenLoc,
8858 SourceLocation EndLoc) {
8859 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008860 SmallVector<Expr *, 8> SrcExprs;
8861 SmallVector<Expr *, 8> DstExprs;
8862 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008863 for (auto &RefExpr : VarList) {
8864 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8865 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008866 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008867 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008868 SrcExprs.push_back(nullptr);
8869 DstExprs.push_back(nullptr);
8870 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008871 continue;
8872 }
8873
Alexey Bataeved09d242014-05-28 05:53:51 +00008874 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008875 // OpenMP [2.1, C/C++]
8876 // A list item is a variable name.
8877 // OpenMP [2.14.4.1, Restrictions, p.1]
8878 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008879 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008880 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008881 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8882 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008883 continue;
8884 }
8885
8886 Decl *D = DE->getDecl();
8887 VarDecl *VD = cast<VarDecl>(D);
8888
8889 QualType Type = VD->getType();
8890 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8891 // It will be analyzed later.
8892 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008893 SrcExprs.push_back(nullptr);
8894 DstExprs.push_back(nullptr);
8895 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008896 continue;
8897 }
8898
8899 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8900 // A list item that appears in a copyin clause must be threadprivate.
8901 if (!DSAStack->isThreadPrivate(VD)) {
8902 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008903 << getOpenMPClauseName(OMPC_copyin)
8904 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008905 continue;
8906 }
8907
8908 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8909 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008910 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008911 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008912 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008913 auto *SrcVD =
8914 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8915 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008916 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008917 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8918 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008919 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8920 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008921 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008922 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008923 // For arrays generate assignment operation for single element and replace
8924 // it by the original array element in CodeGen.
8925 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8926 PseudoDstExpr, PseudoSrcExpr);
8927 if (AssignmentOp.isInvalid())
8928 continue;
8929 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8930 /*DiscardedValue=*/true);
8931 if (AssignmentOp.isInvalid())
8932 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008933
8934 DSAStack->addDSA(VD, DE, OMPC_copyin);
8935 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008936 SrcExprs.push_back(PseudoSrcExpr);
8937 DstExprs.push_back(PseudoDstExpr);
8938 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008939 }
8940
Alexey Bataeved09d242014-05-28 05:53:51 +00008941 if (Vars.empty())
8942 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008943
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008944 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8945 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008946}
8947
Alexey Bataevbae9a792014-06-27 10:37:06 +00008948OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8949 SourceLocation StartLoc,
8950 SourceLocation LParenLoc,
8951 SourceLocation EndLoc) {
8952 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008953 SmallVector<Expr *, 8> SrcExprs;
8954 SmallVector<Expr *, 8> DstExprs;
8955 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008956 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00008957 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8958 SourceLocation ELoc;
8959 SourceRange ERange;
8960 Expr *SimpleRefExpr = RefExpr;
8961 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8962 /*AllowArraySection=*/false);
8963 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008964 // It will be analyzed later.
8965 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008966 SrcExprs.push_back(nullptr);
8967 DstExprs.push_back(nullptr);
8968 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008969 }
Alexey Bataeve122da12016-03-17 10:50:17 +00008970 ValueDecl *D = Res.first;
8971 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00008972 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008973
Alexey Bataeve122da12016-03-17 10:50:17 +00008974 QualType Type = D->getType();
8975 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008976
8977 // OpenMP [2.14.4.2, Restrictions, p.2]
8978 // A list item that appears in a copyprivate clause may not appear in a
8979 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00008980 if (!VD || !DSAStack->isThreadPrivate(VD)) {
8981 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008982 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8983 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008984 Diag(ELoc, diag::err_omp_wrong_dsa)
8985 << getOpenMPClauseName(DVar.CKind)
8986 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00008987 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008988 continue;
8989 }
8990
8991 // OpenMP [2.11.4.2, Restrictions, p.1]
8992 // All list items that appear in a copyprivate clause must be either
8993 // threadprivate or private in the enclosing context.
8994 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00008995 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008996 if (DVar.CKind == OMPC_shared) {
8997 Diag(ELoc, diag::err_omp_required_access)
8998 << getOpenMPClauseName(OMPC_copyprivate)
8999 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009000 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009001 continue;
9002 }
9003 }
9004 }
9005
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009006 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009007 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009008 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009009 << getOpenMPClauseName(OMPC_copyprivate) << Type
9010 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009011 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009012 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009013 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009014 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009015 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009016 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009017 continue;
9018 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009019
Alexey Bataevbae9a792014-06-27 10:37:06 +00009020 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9021 // A variable of class type (or array thereof) that appears in a
9022 // copyin clause requires an accessible, unambiguous copy assignment
9023 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009024 Type = Context.getBaseElementType(Type.getNonReferenceType())
9025 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009026 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009027 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9028 D->hasAttrs() ? &D->getAttrs() : nullptr);
9029 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009030 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009031 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9032 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009033 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009034 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9035 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009036 PseudoDstExpr, PseudoSrcExpr);
9037 if (AssignmentOp.isInvalid())
9038 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009039 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009040 /*DiscardedValue=*/true);
9041 if (AssignmentOp.isInvalid())
9042 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009043
9044 // No need to mark vars as copyprivate, they are already threadprivate or
9045 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009046 assert(VD || IsOpenMPCapturedDecl(D));
9047 Vars.push_back(
9048 VD ? RefExpr->IgnoreParens()
9049 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009050 SrcExprs.push_back(PseudoSrcExpr);
9051 DstExprs.push_back(PseudoDstExpr);
9052 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009053 }
9054
9055 if (Vars.empty())
9056 return nullptr;
9057
Alexey Bataeva63048e2015-03-23 06:18:07 +00009058 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9059 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009060}
9061
Alexey Bataev6125da92014-07-21 11:26:11 +00009062OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9063 SourceLocation StartLoc,
9064 SourceLocation LParenLoc,
9065 SourceLocation EndLoc) {
9066 if (VarList.empty())
9067 return nullptr;
9068
9069 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9070}
Alexey Bataevdea47612014-07-23 07:46:59 +00009071
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009072OMPClause *
9073Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9074 SourceLocation DepLoc, SourceLocation ColonLoc,
9075 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9076 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009077 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009078 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009079 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009080 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009081 return nullptr;
9082 }
9083 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009084 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9085 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009086 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009087 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009088 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9089 /*Last=*/OMPC_DEPEND_unknown, Except)
9090 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009091 return nullptr;
9092 }
9093 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009094 llvm::APSInt DepCounter(/*BitWidth=*/32);
9095 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9096 if (DepKind == OMPC_DEPEND_sink) {
9097 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9098 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9099 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009100 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009101 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009102 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9103 DSAStack->getParentOrderedRegionParam()) {
9104 for (auto &RefExpr : VarList) {
9105 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9106 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9107 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9108 // It will be analyzed later.
9109 Vars.push_back(RefExpr);
9110 continue;
9111 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009112
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009113 SourceLocation ELoc = RefExpr->getExprLoc();
9114 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9115 if (DepKind == OMPC_DEPEND_sink) {
9116 if (DepCounter >= TotalDepCount) {
9117 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9118 continue;
9119 }
9120 ++DepCounter;
9121 // OpenMP [2.13.9, Summary]
9122 // depend(dependence-type : vec), where dependence-type is:
9123 // 'sink' and where vec is the iteration vector, which has the form:
9124 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9125 // where n is the value specified by the ordered clause in the loop
9126 // directive, xi denotes the loop iteration variable of the i-th nested
9127 // loop associated with the loop directive, and di is a constant
9128 // non-negative integer.
9129 SimpleExpr = SimpleExpr->IgnoreImplicit();
9130 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9131 if (!DE) {
9132 OverloadedOperatorKind OOK = OO_None;
9133 SourceLocation OOLoc;
9134 Expr *LHS, *RHS;
9135 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9136 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9137 OOLoc = BO->getOperatorLoc();
9138 LHS = BO->getLHS()->IgnoreParenImpCasts();
9139 RHS = BO->getRHS()->IgnoreParenImpCasts();
9140 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9141 OOK = OCE->getOperator();
9142 OOLoc = OCE->getOperatorLoc();
9143 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9144 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9145 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9146 OOK = MCE->getMethodDecl()
9147 ->getNameInfo()
9148 .getName()
9149 .getCXXOverloadedOperator();
9150 OOLoc = MCE->getCallee()->getExprLoc();
9151 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9152 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9153 } else {
9154 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9155 continue;
9156 }
9157 DE = dyn_cast<DeclRefExpr>(LHS);
9158 if (!DE) {
9159 Diag(LHS->getExprLoc(),
9160 diag::err_omp_depend_sink_expected_loop_iteration)
9161 << DSAStack->getParentLoopControlVariable(
9162 DepCounter.getZExtValue());
9163 continue;
9164 }
9165 if (OOK != OO_Plus && OOK != OO_Minus) {
9166 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9167 continue;
9168 }
9169 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9170 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9171 if (Res.isInvalid())
9172 continue;
9173 }
9174 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9175 if (!CurContext->isDependentContext() &&
9176 DSAStack->getParentOrderedRegionParam() &&
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00009177 (!VD ||
9178 DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009179 Diag(DE->getExprLoc(),
9180 diag::err_omp_depend_sink_expected_loop_iteration)
9181 << DSAStack->getParentLoopControlVariable(
9182 DepCounter.getZExtValue());
9183 continue;
9184 }
9185 } else {
9186 // OpenMP [2.11.1.1, Restrictions, p.3]
9187 // A variable that is part of another variable (such as a field of a
9188 // structure) but is not an array element or an array section cannot
9189 // appear in a depend clause.
9190 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9191 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9192 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9193 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9194 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009195 (ASE &&
9196 !ASE->getBase()
9197 ->getType()
9198 .getNonReferenceType()
9199 ->isPointerType() &&
9200 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009201 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9202 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009203 continue;
9204 }
9205 }
9206
9207 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9208 }
9209
9210 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9211 TotalDepCount > VarList.size() &&
9212 DSAStack->getParentOrderedRegionParam()) {
9213 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9214 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9215 }
9216 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9217 Vars.empty())
9218 return nullptr;
9219 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009220
9221 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9222 DepLoc, ColonLoc, Vars);
9223}
Michael Wonge710d542015-08-07 16:16:36 +00009224
9225OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9226 SourceLocation LParenLoc,
9227 SourceLocation EndLoc) {
9228 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009229
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009230 // OpenMP [2.9.1, Restrictions]
9231 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009232 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9233 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009234 return nullptr;
9235
Michael Wonge710d542015-08-07 16:16:36 +00009236 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9237}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009238
9239static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9240 DSAStackTy *Stack, CXXRecordDecl *RD) {
9241 if (!RD || RD->isInvalidDecl())
9242 return true;
9243
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009244 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9245 if (auto *CTD = CTSD->getSpecializedTemplate())
9246 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009247 auto QTy = SemaRef.Context.getRecordType(RD);
9248 if (RD->isDynamicClass()) {
9249 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9250 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9251 return false;
9252 }
9253 auto *DC = RD;
9254 bool IsCorrect = true;
9255 for (auto *I : DC->decls()) {
9256 if (I) {
9257 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9258 if (MD->isStatic()) {
9259 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9260 SemaRef.Diag(MD->getLocation(),
9261 diag::note_omp_static_member_in_target);
9262 IsCorrect = false;
9263 }
9264 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9265 if (VD->isStaticDataMember()) {
9266 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9267 SemaRef.Diag(VD->getLocation(),
9268 diag::note_omp_static_member_in_target);
9269 IsCorrect = false;
9270 }
9271 }
9272 }
9273 }
9274
9275 for (auto &I : RD->bases()) {
9276 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9277 I.getType()->getAsCXXRecordDecl()))
9278 IsCorrect = false;
9279 }
9280 return IsCorrect;
9281}
9282
9283static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9284 DSAStackTy *Stack, QualType QTy) {
9285 NamedDecl *ND;
9286 if (QTy->isIncompleteType(&ND)) {
9287 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9288 return false;
9289 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9290 if (!RD->isInvalidDecl() &&
9291 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9292 return false;
9293 }
9294 return true;
9295}
9296
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009297/// \brief Return true if it can be proven that the provided array expression
9298/// (array section or array subscript) does NOT specify the whole size of the
9299/// array whose base type is \a BaseQTy.
9300static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9301 const Expr *E,
9302 QualType BaseQTy) {
9303 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9304
9305 // If this is an array subscript, it refers to the whole size if the size of
9306 // the dimension is constant and equals 1. Also, an array section assumes the
9307 // format of an array subscript if no colon is used.
9308 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9309 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9310 return ATy->getSize().getSExtValue() != 1;
9311 // Size can't be evaluated statically.
9312 return false;
9313 }
9314
9315 assert(OASE && "Expecting array section if not an array subscript.");
9316 auto *LowerBound = OASE->getLowerBound();
9317 auto *Length = OASE->getLength();
9318
9319 // If there is a lower bound that does not evaluates to zero, we are not
9320 // convering the whole dimension.
9321 if (LowerBound) {
9322 llvm::APSInt ConstLowerBound;
9323 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9324 return false; // Can't get the integer value as a constant.
9325 if (ConstLowerBound.getSExtValue())
9326 return true;
9327 }
9328
9329 // If we don't have a length we covering the whole dimension.
9330 if (!Length)
9331 return false;
9332
9333 // If the base is a pointer, we don't have a way to get the size of the
9334 // pointee.
9335 if (BaseQTy->isPointerType())
9336 return false;
9337
9338 // We can only check if the length is the same as the size of the dimension
9339 // if we have a constant array.
9340 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9341 if (!CATy)
9342 return false;
9343
9344 llvm::APSInt ConstLength;
9345 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9346 return false; // Can't get the integer value as a constant.
9347
9348 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9349}
9350
9351// Return true if it can be proven that the provided array expression (array
9352// section or array subscript) does NOT specify a single element of the array
9353// whose base type is \a BaseQTy.
9354static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9355 const Expr *E,
9356 QualType BaseQTy) {
9357 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9358
9359 // An array subscript always refer to a single element. Also, an array section
9360 // assumes the format of an array subscript if no colon is used.
9361 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9362 return false;
9363
9364 assert(OASE && "Expecting array section if not an array subscript.");
9365 auto *Length = OASE->getLength();
9366
9367 // If we don't have a length we have to check if the array has unitary size
9368 // for this dimension. Also, we should always expect a length if the base type
9369 // is pointer.
9370 if (!Length) {
9371 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9372 return ATy->getSize().getSExtValue() != 1;
9373 // We cannot assume anything.
9374 return false;
9375 }
9376
9377 // Check if the length evaluates to 1.
9378 llvm::APSInt ConstLength;
9379 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9380 return false; // Can't get the integer value as a constant.
9381
9382 return ConstLength.getSExtValue() != 1;
9383}
9384
Samuel Antao5de996e2016-01-22 20:21:36 +00009385// Return the expression of the base of the map clause or null if it cannot
9386// be determined and do all the necessary checks to see if the expression is
9387// valid as a standalone map clause expression.
9388static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
9389 SourceLocation ELoc = E->getExprLoc();
9390 SourceRange ERange = E->getSourceRange();
9391
9392 // The base of elements of list in a map clause have to be either:
9393 // - a reference to variable or field.
9394 // - a member expression.
9395 // - an array expression.
9396 //
9397 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9398 // reference to 'r'.
9399 //
9400 // If we have:
9401 //
9402 // struct SS {
9403 // Bla S;
9404 // foo() {
9405 // #pragma omp target map (S.Arr[:12]);
9406 // }
9407 // }
9408 //
9409 // We want to retrieve the member expression 'this->S';
9410
9411 Expr *RelevantExpr = nullptr;
9412
Samuel Antao5de996e2016-01-22 20:21:36 +00009413 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9414 // If a list item is an array section, it must specify contiguous storage.
9415 //
9416 // For this restriction it is sufficient that we make sure only references
9417 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009418 // exist except in the rightmost expression (unless they cover the whole
9419 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009420 //
9421 // r.ArrS[3:5].Arr[6:7]
9422 //
9423 // r.ArrS[3:5].x
9424 //
9425 // but these would be valid:
9426 // r.ArrS[3].Arr[6:7]
9427 //
9428 // r.ArrS[3].x
9429
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009430 bool AllowUnitySizeArraySection = true;
9431 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009432
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009433 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009434 E = E->IgnoreParenImpCasts();
9435
9436 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9437 if (!isa<VarDecl>(CurE->getDecl()))
9438 break;
9439
9440 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009441
9442 // If we got a reference to a declaration, we should not expect any array
9443 // section before that.
9444 AllowUnitySizeArraySection = false;
9445 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009446 continue;
9447 }
9448
9449 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9450 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9451
9452 if (isa<CXXThisExpr>(BaseE))
9453 // We found a base expression: this->Val.
9454 RelevantExpr = CurE;
9455 else
9456 E = BaseE;
9457
9458 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9459 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9460 << CurE->getSourceRange();
9461 break;
9462 }
9463
9464 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9465
9466 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9467 // A bit-field cannot appear in a map clause.
9468 //
9469 if (FD->isBitField()) {
9470 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9471 << CurE->getSourceRange();
9472 break;
9473 }
9474
9475 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9476 // If the type of a list item is a reference to a type T then the type
9477 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009478 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009479
9480 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9481 // A list item cannot be a variable that is a member of a structure with
9482 // a union type.
9483 //
9484 if (auto *RT = CurType->getAs<RecordType>())
9485 if (RT->isUnionType()) {
9486 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9487 << CurE->getSourceRange();
9488 break;
9489 }
9490
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009491 // If we got a member expression, we should not expect any array section
9492 // before that:
9493 //
9494 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9495 // If a list item is an element of a structure, only the rightmost symbol
9496 // of the variable reference can be an array section.
9497 //
9498 AllowUnitySizeArraySection = false;
9499 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009500 continue;
9501 }
9502
9503 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9504 E = CurE->getBase()->IgnoreParenImpCasts();
9505
9506 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9507 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9508 << 0 << CurE->getSourceRange();
9509 break;
9510 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009511
9512 // If we got an array subscript that express the whole dimension we
9513 // can have any array expressions before. If it only expressing part of
9514 // the dimension, we can only have unitary-size array expressions.
9515 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9516 E->getType()))
9517 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009518 continue;
9519 }
9520
9521 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009522 E = CurE->getBase()->IgnoreParenImpCasts();
9523
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009524 auto CurType =
9525 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9526
Samuel Antao5de996e2016-01-22 20:21:36 +00009527 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9528 // If the type of a list item is a reference to a type T then the type
9529 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009530 if (CurType->isReferenceType())
9531 CurType = CurType->getPointeeType();
9532
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009533 bool IsPointer = CurType->isAnyPointerType();
9534
9535 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009536 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9537 << 0 << CurE->getSourceRange();
9538 break;
9539 }
9540
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009541 bool NotWhole =
9542 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9543 bool NotUnity =
9544 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9545
9546 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9547 // Any array section is currently allowed.
9548 //
9549 // If this array section refers to the whole dimension we can still
9550 // accept other array sections before this one, except if the base is a
9551 // pointer. Otherwise, only unitary sections are accepted.
9552 if (NotWhole || IsPointer)
9553 AllowWholeSizeArraySection = false;
9554 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9555 (AllowWholeSizeArraySection && NotWhole)) {
9556 // A unity or whole array section is not allowed and that is not
9557 // compatible with the properties of the current array section.
9558 SemaRef.Diag(
9559 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9560 << CurE->getSourceRange();
9561 break;
9562 }
Samuel Antao5de996e2016-01-22 20:21:36 +00009563 continue;
9564 }
9565
9566 // If nothing else worked, this is not a valid map clause expression.
9567 SemaRef.Diag(ELoc,
9568 diag::err_omp_expected_named_var_member_or_array_expression)
9569 << ERange;
9570 break;
9571 }
9572
9573 return RelevantExpr;
9574}
9575
9576// Return true if expression E associated with value VD has conflicts with other
9577// map information.
9578static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9579 Expr *E, bool CurrentRegionOnly) {
9580 assert(VD && E);
9581
9582 // Types used to organize the components of a valid map clause.
9583 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9584 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9585
9586 // Helper to extract the components in the map clause expression E and store
9587 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9588 // it has already passed the single clause checks.
9589 auto ExtractMapExpressionComponents = [](Expr *TE,
9590 MapExpressionComponents &MEC) {
9591 while (true) {
9592 TE = TE->IgnoreParenImpCasts();
9593
9594 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9595 MEC.push_back(
9596 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9597 break;
9598 }
9599
9600 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9601 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9602
9603 MEC.push_back(MapExpressionComponent(
9604 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9605 if (isa<CXXThisExpr>(BaseE))
9606 break;
9607
9608 TE = BaseE;
9609 continue;
9610 }
9611
9612 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9613 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9614 TE = CurE->getBase()->IgnoreParenImpCasts();
9615 continue;
9616 }
9617
9618 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9619 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9620 TE = CurE->getBase()->IgnoreParenImpCasts();
9621 continue;
9622 }
9623
9624 llvm_unreachable(
9625 "Expecting only valid map clause expressions at this point!");
9626 }
9627 };
9628
9629 SourceLocation ELoc = E->getExprLoc();
9630 SourceRange ERange = E->getSourceRange();
9631
9632 // In order to easily check the conflicts we need to match each component of
9633 // the expression under test with the components of the expressions that are
9634 // already in the stack.
9635
9636 MapExpressionComponents CurComponents;
9637 ExtractMapExpressionComponents(E, CurComponents);
9638
9639 assert(!CurComponents.empty() && "Map clause expression with no components!");
9640 assert(CurComponents.back().second == VD &&
9641 "Map clause expression with unexpected base!");
9642
9643 // Variables to help detecting enclosing problems in data environment nests.
9644 bool IsEnclosedByDataEnvironmentExpr = false;
9645 Expr *EnclosingExpr = nullptr;
9646
9647 bool FoundError =
9648 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9649 MapExpressionComponents StackComponents;
9650 ExtractMapExpressionComponents(RE, StackComponents);
9651 assert(!StackComponents.empty() &&
9652 "Map clause expression with no components!");
9653 assert(StackComponents.back().second == VD &&
9654 "Map clause expression with unexpected base!");
9655
9656 // Expressions must start from the same base. Here we detect at which
9657 // point both expressions diverge from each other and see if we can
9658 // detect if the memory referred to both expressions is contiguous and
9659 // do not overlap.
9660 auto CI = CurComponents.rbegin();
9661 auto CE = CurComponents.rend();
9662 auto SI = StackComponents.rbegin();
9663 auto SE = StackComponents.rend();
9664 for (; CI != CE && SI != SE; ++CI, ++SI) {
9665
9666 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9667 // At most one list item can be an array item derived from a given
9668 // variable in map clauses of the same construct.
9669 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9670 isa<OMPArraySectionExpr>(CI->first)) &&
9671 (isa<ArraySubscriptExpr>(SI->first) ||
9672 isa<OMPArraySectionExpr>(SI->first))) {
9673 SemaRef.Diag(CI->first->getExprLoc(),
9674 diag::err_omp_multiple_array_items_in_map_clause)
9675 << CI->first->getSourceRange();
9676 ;
9677 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9678 << SI->first->getSourceRange();
9679 return true;
9680 }
9681
9682 // Do both expressions have the same kind?
9683 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9684 break;
9685
9686 // Are we dealing with different variables/fields?
9687 if (CI->second != SI->second)
9688 break;
9689 }
9690
9691 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9692 // List items of map clauses in the same construct must not share
9693 // original storage.
9694 //
9695 // If the expressions are exactly the same or one is a subset of the
9696 // other, it means they are sharing storage.
9697 if (CI == CE && SI == SE) {
9698 if (CurrentRegionOnly) {
9699 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9700 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9701 << RE->getSourceRange();
9702 return true;
9703 } else {
9704 // If we find the same expression in the enclosing data environment,
9705 // that is legal.
9706 IsEnclosedByDataEnvironmentExpr = true;
9707 return false;
9708 }
9709 }
9710
9711 QualType DerivedType = std::prev(CI)->first->getType();
9712 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9713
9714 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9715 // If the type of a list item is a reference to a type T then the type
9716 // will be considered to be T for all purposes of this clause.
9717 if (DerivedType->isReferenceType())
9718 DerivedType = DerivedType->getPointeeType();
9719
9720 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9721 // A variable for which the type is pointer and an array section
9722 // derived from that variable must not appear as list items of map
9723 // clauses of the same construct.
9724 //
9725 // Also, cover one of the cases in:
9726 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9727 // If any part of the original storage of a list item has corresponding
9728 // storage in the device data environment, all of the original storage
9729 // must have corresponding storage in the device data environment.
9730 //
9731 if (DerivedType->isAnyPointerType()) {
9732 if (CI == CE || SI == SE) {
9733 SemaRef.Diag(
9734 DerivedLoc,
9735 diag::err_omp_pointer_mapped_along_with_derived_section)
9736 << DerivedLoc;
9737 } else {
9738 assert(CI != CE && SI != SE);
9739 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9740 << DerivedLoc;
9741 }
9742 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9743 << RE->getSourceRange();
9744 return true;
9745 }
9746
9747 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9748 // List items of map clauses in the same construct must not share
9749 // original storage.
9750 //
9751 // An expression is a subset of the other.
9752 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9753 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9754 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9755 << RE->getSourceRange();
9756 return true;
9757 }
9758
9759 // The current expression uses the same base as other expression in the
9760 // data environment but does not contain it completelly.
9761 if (!CurrentRegionOnly && SI != SE)
9762 EnclosingExpr = RE;
9763
9764 // The current expression is a subset of the expression in the data
9765 // environment.
9766 IsEnclosedByDataEnvironmentExpr |=
9767 (!CurrentRegionOnly && CI != CE && SI == SE);
9768
9769 return false;
9770 });
9771
9772 if (CurrentRegionOnly)
9773 return FoundError;
9774
9775 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9776 // If any part of the original storage of a list item has corresponding
9777 // storage in the device data environment, all of the original storage must
9778 // have corresponding storage in the device data environment.
9779 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9780 // If a list item is an element of a structure, and a different element of
9781 // the structure has a corresponding list item in the device data environment
9782 // prior to a task encountering the construct associated with the map clause,
9783 // then the list item must also have a correspnding list item in the device
9784 // data environment prior to the task encountering the construct.
9785 //
9786 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9787 SemaRef.Diag(ELoc,
9788 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9789 << ERange;
9790 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9791 << EnclosingExpr->getSourceRange();
9792 return true;
9793 }
9794
9795 return FoundError;
9796}
9797
Samuel Antao23abd722016-01-19 20:40:49 +00009798OMPClause *
9799Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9800 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9801 SourceLocation MapLoc, SourceLocation ColonLoc,
9802 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9803 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009804 SmallVector<Expr *, 4> Vars;
9805
9806 for (auto &RE : VarList) {
9807 assert(RE && "Null expr in omp map");
9808 if (isa<DependentScopeDeclRefExpr>(RE)) {
9809 // It will be analyzed later.
9810 Vars.push_back(RE);
9811 continue;
9812 }
9813 SourceLocation ELoc = RE->getExprLoc();
9814
Kelvin Li0bff7af2015-11-23 05:32:03 +00009815 auto *VE = RE->IgnoreParenLValueCasts();
9816
9817 if (VE->isValueDependent() || VE->isTypeDependent() ||
9818 VE->isInstantiationDependent() ||
9819 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009820 // We can only analyze this information once the missing information is
9821 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009822 Vars.push_back(RE);
9823 continue;
9824 }
9825
9826 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009827
Samuel Antao5de996e2016-01-22 20:21:36 +00009828 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9829 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9830 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009831 continue;
9832 }
9833
Samuel Antao5de996e2016-01-22 20:21:36 +00009834 // Obtain the array or member expression bases if required.
9835 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9836 if (!BE)
9837 continue;
9838
9839 // If the base is a reference to a variable, we rely on that variable for
9840 // the following checks. If it is a 'this' expression we rely on the field.
9841 ValueDecl *D = nullptr;
9842 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9843 D = DRE->getDecl();
9844 } else {
9845 auto *ME = cast<MemberExpr>(BE);
9846 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9847 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009848 }
9849 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009850
Samuel Antao5de996e2016-01-22 20:21:36 +00009851 auto *VD = dyn_cast<VarDecl>(D);
9852 auto *FD = dyn_cast<FieldDecl>(D);
9853
9854 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009855 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009856
9857 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9858 // threadprivate variables cannot appear in a map clause.
9859 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009860 auto DVar = DSAStack->getTopDSA(VD, false);
9861 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9862 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9863 continue;
9864 }
9865
Samuel Antao5de996e2016-01-22 20:21:36 +00009866 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9867 // A list item cannot appear in both a map clause and a data-sharing
9868 // attribute clause on the same construct.
9869 //
9870 // TODO: Implement this check - it cannot currently be tested because of
9871 // missing implementation of the other data sharing clauses in target
9872 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009873
Samuel Antao5de996e2016-01-22 20:21:36 +00009874 // Check conflicts with other map clause expressions. We check the conflicts
9875 // with the current construct separately from the enclosing data
9876 // environment, because the restrictions are different.
9877 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9878 /*CurrentRegionOnly=*/true))
9879 break;
9880 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9881 /*CurrentRegionOnly=*/false))
9882 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009883
Samuel Antao5de996e2016-01-22 20:21:36 +00009884 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9885 // If the type of a list item is a reference to a type T then the type will
9886 // be considered to be T for all purposes of this clause.
9887 QualType Type = D->getType();
9888 if (Type->isReferenceType())
9889 Type = Type->getPointeeType();
9890
9891 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009892 // A list item must have a mappable type.
9893 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9894 DSAStack, Type))
9895 continue;
9896
Samuel Antaodf67fc42016-01-19 19:15:56 +00009897 // target enter data
9898 // OpenMP [2.10.2, Restrictions, p. 99]
9899 // A map-type must be specified in all map clauses and must be either
9900 // to or alloc.
9901 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9902 if (DKind == OMPD_target_enter_data &&
9903 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9904 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009905 << (IsMapTypeImplicit ? 1 : 0)
9906 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009907 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009908 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009909 }
9910
Samuel Antao72590762016-01-19 20:04:50 +00009911 // target exit_data
9912 // OpenMP [2.10.3, Restrictions, p. 102]
9913 // A map-type must be specified in all map clauses and must be either
9914 // from, release, or delete.
9915 DKind = DSAStack->getCurrentDirective();
9916 if (DKind == OMPD_target_exit_data &&
9917 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9918 MapType == OMPC_MAP_delete)) {
9919 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009920 << (IsMapTypeImplicit ? 1 : 0)
9921 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009922 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009923 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009924 }
9925
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009926 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9927 // A list item cannot appear in both a map clause and a data-sharing
9928 // attribute clause on the same construct
9929 if (DKind == OMPD_target && VD) {
9930 auto DVar = DSAStack->getTopDSA(VD, false);
9931 if (isOpenMPPrivate(DVar.CKind)) {
9932 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
9933 << getOpenMPClauseName(DVar.CKind)
9934 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9935 ReportOriginalDSA(*this, DSAStack, D, DVar);
9936 continue;
9937 }
9938 }
9939
Kelvin Li0bff7af2015-11-23 05:32:03 +00009940 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009941 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009942 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009943
Samuel Antao5de996e2016-01-22 20:21:36 +00009944 // We need to produce a map clause even if we don't have variables so that
9945 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009946 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009947 MapTypeModifier, MapType, IsMapTypeImplicit,
9948 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009949}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009950
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00009951QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
9952 TypeResult ParsedType) {
9953 assert(ParsedType.isUsable());
9954
9955 QualType ReductionType = GetTypeFromParser(ParsedType.get());
9956 if (ReductionType.isNull())
9957 return QualType();
9958
9959 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
9960 // A type name in a declare reduction directive cannot be a function type, an
9961 // array type, a reference type, or a type qualified with const, volatile or
9962 // restrict.
9963 if (ReductionType.hasQualifiers()) {
9964 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
9965 return QualType();
9966 }
9967
9968 if (ReductionType->isFunctionType()) {
9969 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
9970 return QualType();
9971 }
9972 if (ReductionType->isReferenceType()) {
9973 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
9974 return QualType();
9975 }
9976 if (ReductionType->isArrayType()) {
9977 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
9978 return QualType();
9979 }
9980 return ReductionType;
9981}
9982
9983Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
9984 Scope *S, DeclContext *DC, DeclarationName Name,
9985 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
9986 AccessSpecifier AS, Decl *PrevDeclInScope) {
9987 SmallVector<Decl *, 8> Decls;
9988 Decls.reserve(ReductionTypes.size());
9989
9990 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
9991 ForRedeclaration);
9992 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
9993 // A reduction-identifier may not be re-declared in the current scope for the
9994 // same type or for a type that is compatible according to the base language
9995 // rules.
9996 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
9997 OMPDeclareReductionDecl *PrevDRD = nullptr;
9998 bool InCompoundScope = true;
9999 if (S != nullptr) {
10000 // Find previous declaration with the same name not referenced in other
10001 // declarations.
10002 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10003 InCompoundScope =
10004 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10005 LookupName(Lookup, S);
10006 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10007 /*AllowInlineNamespace=*/false);
10008 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10009 auto Filter = Lookup.makeFilter();
10010 while (Filter.hasNext()) {
10011 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10012 if (InCompoundScope) {
10013 auto I = UsedAsPrevious.find(PrevDecl);
10014 if (I == UsedAsPrevious.end())
10015 UsedAsPrevious[PrevDecl] = false;
10016 if (auto *D = PrevDecl->getPrevDeclInScope())
10017 UsedAsPrevious[D] = true;
10018 }
10019 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10020 PrevDecl->getLocation();
10021 }
10022 Filter.done();
10023 if (InCompoundScope) {
10024 for (auto &PrevData : UsedAsPrevious) {
10025 if (!PrevData.second) {
10026 PrevDRD = PrevData.first;
10027 break;
10028 }
10029 }
10030 }
10031 } else if (PrevDeclInScope != nullptr) {
10032 auto *PrevDRDInScope = PrevDRD =
10033 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10034 do {
10035 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10036 PrevDRDInScope->getLocation();
10037 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10038 } while (PrevDRDInScope != nullptr);
10039 }
10040 for (auto &TyData : ReductionTypes) {
10041 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10042 bool Invalid = false;
10043 if (I != PreviousRedeclTypes.end()) {
10044 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10045 << TyData.first;
10046 Diag(I->second, diag::note_previous_definition);
10047 Invalid = true;
10048 }
10049 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10050 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10051 Name, TyData.first, PrevDRD);
10052 DC->addDecl(DRD);
10053 DRD->setAccess(AS);
10054 Decls.push_back(DRD);
10055 if (Invalid)
10056 DRD->setInvalidDecl();
10057 else
10058 PrevDRD = DRD;
10059 }
10060
10061 return DeclGroupPtrTy::make(
10062 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10063}
10064
10065void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10066 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10067
10068 // Enter new function scope.
10069 PushFunctionScope();
10070 getCurFunction()->setHasBranchProtectedScope();
10071 getCurFunction()->setHasOMPDeclareReductionCombiner();
10072
10073 if (S != nullptr)
10074 PushDeclContext(S, DRD);
10075 else
10076 CurContext = DRD;
10077
10078 PushExpressionEvaluationContext(PotentiallyEvaluated);
10079
10080 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010081 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10082 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10083 // uses semantics of argument handles by value, but it should be passed by
10084 // reference. C lang does not support references, so pass all parameters as
10085 // pointers.
10086 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010087 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010088 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010089 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10090 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10091 // uses semantics of argument handles by value, but it should be passed by
10092 // reference. C lang does not support references, so pass all parameters as
10093 // pointers.
10094 // Create 'T omp_out;' variable.
10095 auto *OmpOutParm =
10096 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10097 if (S != nullptr) {
10098 PushOnScopeChains(OmpInParm, S);
10099 PushOnScopeChains(OmpOutParm, S);
10100 } else {
10101 DRD->addDecl(OmpInParm);
10102 DRD->addDecl(OmpOutParm);
10103 }
10104}
10105
10106void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10107 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10108 DiscardCleanupsInEvaluationContext();
10109 PopExpressionEvaluationContext();
10110
10111 PopDeclContext();
10112 PopFunctionScopeInfo();
10113
10114 if (Combiner != nullptr)
10115 DRD->setCombiner(Combiner);
10116 else
10117 DRD->setInvalidDecl();
10118}
10119
10120void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10121 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10122
10123 // Enter new function scope.
10124 PushFunctionScope();
10125 getCurFunction()->setHasBranchProtectedScope();
10126
10127 if (S != nullptr)
10128 PushDeclContext(S, DRD);
10129 else
10130 CurContext = DRD;
10131
10132 PushExpressionEvaluationContext(PotentiallyEvaluated);
10133
10134 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010135 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10136 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10137 // uses semantics of argument handles by value, but it should be passed by
10138 // reference. C lang does not support references, so pass all parameters as
10139 // pointers.
10140 // Create 'T omp_priv;' variable.
10141 auto *OmpPrivParm =
10142 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010143 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10144 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10145 // uses semantics of argument handles by value, but it should be passed by
10146 // reference. C lang does not support references, so pass all parameters as
10147 // pointers.
10148 // Create 'T omp_orig;' variable.
10149 auto *OmpOrigParm =
10150 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010151 if (S != nullptr) {
10152 PushOnScopeChains(OmpPrivParm, S);
10153 PushOnScopeChains(OmpOrigParm, S);
10154 } else {
10155 DRD->addDecl(OmpPrivParm);
10156 DRD->addDecl(OmpOrigParm);
10157 }
10158}
10159
10160void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10161 Expr *Initializer) {
10162 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10163 DiscardCleanupsInEvaluationContext();
10164 PopExpressionEvaluationContext();
10165
10166 PopDeclContext();
10167 PopFunctionScopeInfo();
10168
10169 if (Initializer != nullptr)
10170 DRD->setInitializer(Initializer);
10171 else
10172 DRD->setInvalidDecl();
10173}
10174
10175Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10176 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10177 for (auto *D : DeclReductions.get()) {
10178 if (IsValid) {
10179 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10180 if (S != nullptr)
10181 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10182 } else
10183 D->setInvalidDecl();
10184 }
10185 return DeclReductions;
10186}
10187
Kelvin Li099bb8c2015-11-24 20:50:12 +000010188OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10189 SourceLocation StartLoc,
10190 SourceLocation LParenLoc,
10191 SourceLocation EndLoc) {
10192 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010193
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010194 // OpenMP [teams Constrcut, Restrictions]
10195 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010196 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10197 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010198 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010199
10200 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10201}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010202
10203OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10204 SourceLocation StartLoc,
10205 SourceLocation LParenLoc,
10206 SourceLocation EndLoc) {
10207 Expr *ValExpr = ThreadLimit;
10208
10209 // OpenMP [teams Constrcut, Restrictions]
10210 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010211 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10212 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010213 return nullptr;
10214
10215 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10216 EndLoc);
10217}
Alexey Bataeva0569352015-12-01 10:17:31 +000010218
10219OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10220 SourceLocation StartLoc,
10221 SourceLocation LParenLoc,
10222 SourceLocation EndLoc) {
10223 Expr *ValExpr = Priority;
10224
10225 // OpenMP [2.9.1, task Constrcut]
10226 // The priority-value is a non-negative numerical scalar expression.
10227 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10228 /*StrictlyPositive=*/false))
10229 return nullptr;
10230
10231 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10232}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010233
10234OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10235 SourceLocation StartLoc,
10236 SourceLocation LParenLoc,
10237 SourceLocation EndLoc) {
10238 Expr *ValExpr = Grainsize;
10239
10240 // OpenMP [2.9.2, taskloop Constrcut]
10241 // The parameter of the grainsize clause must be a positive integer
10242 // expression.
10243 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10244 /*StrictlyPositive=*/true))
10245 return nullptr;
10246
10247 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10248}
Alexey Bataev382967a2015-12-08 12:06:20 +000010249
10250OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10251 SourceLocation StartLoc,
10252 SourceLocation LParenLoc,
10253 SourceLocation EndLoc) {
10254 Expr *ValExpr = NumTasks;
10255
10256 // OpenMP [2.9.2, taskloop Constrcut]
10257 // The parameter of the num_tasks clause must be a positive integer
10258 // expression.
10259 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10260 /*StrictlyPositive=*/true))
10261 return nullptr;
10262
10263 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10264}
10265
Alexey Bataev28c75412015-12-15 08:19:24 +000010266OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10267 SourceLocation LParenLoc,
10268 SourceLocation EndLoc) {
10269 // OpenMP [2.13.2, critical construct, Description]
10270 // ... where hint-expression is an integer constant expression that evaluates
10271 // to a valid lock hint.
10272 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10273 if (HintExpr.isInvalid())
10274 return nullptr;
10275 return new (Context)
10276 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10277}
10278
Carlo Bertollib4adf552016-01-15 18:50:31 +000010279OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10280 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10281 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10282 SourceLocation EndLoc) {
10283 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10284 std::string Values;
10285 Values += "'";
10286 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10287 Values += "'";
10288 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10289 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10290 return nullptr;
10291 }
10292 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010293 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010294 if (ChunkSize) {
10295 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10296 !ChunkSize->isInstantiationDependent() &&
10297 !ChunkSize->containsUnexpandedParameterPack()) {
10298 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10299 ExprResult Val =
10300 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10301 if (Val.isInvalid())
10302 return nullptr;
10303
10304 ValExpr = Val.get();
10305
10306 // OpenMP [2.7.1, Restrictions]
10307 // chunk_size must be a loop invariant integer expression with a positive
10308 // value.
10309 llvm::APSInt Result;
10310 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10311 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10312 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10313 << "dist_schedule" << ChunkSize->getSourceRange();
10314 return nullptr;
10315 }
10316 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010317 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10318 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10319 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010320 }
10321 }
10322 }
10323
10324 return new (Context)
10325 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010326 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010327}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010328
10329OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10330 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10331 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10332 SourceLocation KindLoc, SourceLocation EndLoc) {
10333 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10334 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10335 Kind != OMPC_DEFAULTMAP_scalar) {
10336 std::string Value;
10337 SourceLocation Loc;
10338 Value += "'";
10339 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10340 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10341 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10342 Loc = MLoc;
10343 } else {
10344 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10345 OMPC_DEFAULTMAP_scalar);
10346 Loc = KindLoc;
10347 }
10348 Value += "'";
10349 Diag(Loc, diag::err_omp_unexpected_clause_value)
10350 << Value << getOpenMPClauseName(OMPC_defaultmap);
10351 return nullptr;
10352 }
10353
10354 return new (Context)
10355 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10356}