blob: 5d919403d160159a834dc29fcc4b337c334c2176 [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 Bataev758e55e2013-09-06 18:03:48 +000048/// \brief Stack for tracking declarations used in OpenMP directives and
49/// clauses and their data-sharing attributes.
Alexey Bataev7ace49d2016-05-17 08:55:33 +000050class DSAStackTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000051public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000052 struct DSAVarData final {
53 OpenMPDirectiveKind DKind = OMPD_unknown;
54 OpenMPClauseKind CKind = OMPC_unknown;
55 Expr *RefExpr = nullptr;
56 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000057 SourceLocation ImplicitDSALoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000058 DSAVarData() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000059 };
Alexey Bataev8b427062016-05-25 12:36:08 +000060 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
61 OperatorOffsetTy;
Alexey Bataeved09d242014-05-28 05:53:51 +000062
Alexey Bataev758e55e2013-09-06 18:03:48 +000063private:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000064 struct DSAInfo final {
65 OpenMPClauseKind Attributes = OMPC_unknown;
66 /// Pointer to a reference expression and a flag which shows that the
67 /// variable is marked as lastprivate(true) or not (false).
68 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000070 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000071 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000073 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Samuel Antao90927002016-04-26 14:54:23 +000075 typedef llvm::DenseMap<
76 ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists>
77 MappedExprComponentsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000078 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
79 CriticalsWithHintsTy;
Alexey Bataev8b427062016-05-25 12:36:08 +000080 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
81 DoacrossDependMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000082
Alexey Bataev7ace49d2016-05-17 08:55:33 +000083 struct SharingMapTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000085 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +000086 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000087 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000088 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000090 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000092 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +000094 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
95 /// get the data (loop counters etc.) about enclosing loop-based construct.
96 /// This data is required during codegen.
97 DoacrossDependMapTy DoacrossDepends;
Alexey Bataev346265e2015-09-25 10:37:12 +000098 /// \brief first argument (Expr *) contains optional argument of the
99 /// 'ordered' clause, the second one is true if the regions has 'ordered'
100 /// clause, false otherwise.
101 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000102 bool NowaitRegion = false;
103 bool CancelRegion = false;
104 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000105 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000106 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000108 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
109 ConstructLoc(Loc) {}
110 SharingMapTy() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000111 };
112
Axel Naumann323862e2016-02-03 10:45:22 +0000113 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114
115 /// \brief Stack of used declaration and their data-sharing attributes.
116 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000117 /// \brief true, if check for DSA must be from parent directive, false, if
118 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000119 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000120 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000121 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000122 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123
124 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
125
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000126 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000127
128 /// \brief Checks if the variable is a local for OpenMP region.
129 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000130
Alexey Bataev758e55e2013-09-06 18:03:48 +0000131public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000132 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
135 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000137 bool isForceVarCapturing() const { return ForceCapturing; }
138 void setForceVarCapturing(bool V) { ForceCapturing = V; }
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000141 Scope *CurScope, SourceLocation Loc) {
142 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
143 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 }
145
146 void pop() {
147 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
148 Stack.pop_back();
149 }
150
Alexey Bataev28c75412015-12-15 08:19:24 +0000151 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
152 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
153 }
154 const std::pair<OMPCriticalDirective *, llvm::APSInt>
155 getCriticalWithHint(const DeclarationNameInfo &Name) const {
156 auto I = Criticals.find(Name.getAsString());
157 if (I != Criticals.end())
158 return I->second;
159 return std::make_pair(nullptr, llvm::APSInt());
160 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000161 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000162 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000163 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000164 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000165
Alexey Bataev9c821032015-04-30 04:23:23 +0000166 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000167 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000168 /// \brief Check if the specified variable is a loop control variable for
169 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000170 /// \return The index of the loop control variable in the list of associated
171 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000172 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000173 /// \brief Check if the specified variable is a loop control variable for
174 /// parent region.
175 /// \return The index of the loop control variable in the list of associated
176 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000177 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000178 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
179 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000180 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000181
Alexey Bataev758e55e2013-09-06 18:03:48 +0000182 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000183 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
184 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000185
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186 /// \brief Returns data sharing attributes from top of the stack for the
187 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000188 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000189 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000190 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 /// \brief Checks if the specified variables has data-sharing attributes which
192 /// match specified \a CPred predicate in any directive which matches \a DPred
193 /// predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000194 DSAVarData hasDSA(ValueDecl *D,
195 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
196 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
197 bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000198 /// \brief Checks if the specified variables has data-sharing attributes which
199 /// match specified \a CPred predicate in any innermost directive which
200 /// matches \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000201 DSAVarData
202 hasInnermostDSA(ValueDecl *D,
203 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
204 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
205 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000206 /// \brief Checks if the specified variables has explicit data-sharing
207 /// attributes which match specified \a CPred predicate at the specified
208 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000209 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000210 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000211 unsigned Level, bool NotLastprivate = false);
Samuel Antao4be30e92015-10-02 17:14:03 +0000212
213 /// \brief Returns true if the directive at level \Level matches in the
214 /// specified \a DPred predicate.
215 bool hasExplicitDirective(
216 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
217 unsigned Level);
218
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000219 /// \brief Finds a directive which matches specified \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000220 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
221 const DeclarationNameInfo &,
222 SourceLocation)> &DPred,
223 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000224
Alexey Bataev758e55e2013-09-06 18:03:48 +0000225 /// \brief Returns currently analyzed directive.
226 OpenMPDirectiveKind getCurrentDirective() const {
227 return Stack.back().Directive;
228 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000229 /// \brief Returns parent directive.
230 OpenMPDirectiveKind getParentDirective() const {
231 if (Stack.size() > 2)
232 return Stack[Stack.size() - 2].Directive;
233 return OMPD_unknown;
234 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000235
236 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000237 void setDefaultDSANone(SourceLocation Loc) {
238 Stack.back().DefaultAttr = DSA_none;
239 Stack.back().DefaultAttrLoc = Loc;
240 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000241 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 void setDefaultDSAShared(SourceLocation Loc) {
243 Stack.back().DefaultAttr = DSA_shared;
244 Stack.back().DefaultAttrLoc = Loc;
245 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246
247 DefaultDataSharingAttributes getDefaultDSA() const {
248 return Stack.back().DefaultAttr;
249 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000250 SourceLocation getDefaultDSALocation() const {
251 return Stack.back().DefaultAttrLoc;
252 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000253
Alexey Bataevf29276e2014-06-18 04:14:57 +0000254 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000255 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000256 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000257 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000258 }
259
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000260 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000261 void setOrderedRegion(bool IsOrdered, Expr *Param) {
262 Stack.back().OrderedRegion.setInt(IsOrdered);
263 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000264 }
265 /// \brief Returns true, if parent region is ordered (has associated
266 /// 'ordered' clause), false - otherwise.
267 bool isParentOrderedRegion() const {
268 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000269 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000270 return false;
271 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000272 /// \brief Returns optional parameter for the ordered region.
273 Expr *getParentOrderedRegionParam() const {
274 if (Stack.size() > 2)
275 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
276 return nullptr;
277 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000278 /// \brief Marks current region as nowait (it has a 'nowait' clause).
279 void setNowaitRegion(bool IsNowait = true) {
280 Stack.back().NowaitRegion = IsNowait;
281 }
282 /// \brief Returns true, if parent region is nowait (has associated
283 /// 'nowait' clause), false - otherwise.
284 bool isParentNowaitRegion() const {
285 if (Stack.size() > 2)
286 return Stack[Stack.size() - 2].NowaitRegion;
287 return false;
288 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000289 /// \brief Marks parent region as cancel region.
290 void setParentCancelRegion(bool Cancel = true) {
291 if (Stack.size() > 2)
292 Stack[Stack.size() - 2].CancelRegion =
293 Stack[Stack.size() - 2].CancelRegion || Cancel;
294 }
295 /// \brief Return true if current region has inner cancel construct.
296 bool isCancelRegion() const {
297 return Stack.back().CancelRegion;
298 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000299
Alexey Bataev9c821032015-04-30 04:23:23 +0000300 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000301 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000302 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000303 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000304
Alexey Bataev13314bf2014-10-09 04:18:56 +0000305 /// \brief Marks current target region as one with closely nested teams
306 /// region.
307 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
308 if (Stack.size() > 2)
309 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
310 }
311 /// \brief Returns true, if current region has closely nested teams region.
312 bool hasInnerTeamsRegion() const {
313 return getInnerTeamsRegionLoc().isValid();
314 }
315 /// \brief Returns location of the nested teams region (if any).
316 SourceLocation getInnerTeamsRegionLoc() const {
317 if (Stack.size() > 1)
318 return Stack.back().InnerTeamsRegionLoc;
319 return SourceLocation();
320 }
321
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000322 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000324 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000325
Samuel Antao90927002016-04-26 14:54:23 +0000326 // Do the check specified in \a Check to all component lists and return true
327 // if any issue is found.
328 bool checkMappableExprComponentListsForDecl(
329 ValueDecl *VD, bool CurrentRegionOnly,
330 const llvm::function_ref<bool(
331 OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000332 auto SI = Stack.rbegin();
333 auto SE = Stack.rend();
334
335 if (SI == SE)
336 return false;
337
338 if (CurrentRegionOnly) {
339 SE = std::next(SI);
340 } else {
341 ++SI;
342 }
343
344 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000345 auto MI = SI->MappedExprComponents.find(VD);
346 if (MI != SI->MappedExprComponents.end())
347 for (auto &L : MI->second)
348 if (Check(L))
Samuel Antao5de996e2016-01-22 20:21:36 +0000349 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000350 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000351 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000352 }
353
Samuel Antao90927002016-04-26 14:54:23 +0000354 // Create a new mappable expression component list associated with a given
355 // declaration and initialize it with the provided list of components.
356 void addMappableExpressionComponents(
357 ValueDecl *VD,
358 OMPClauseMappableExprCommon::MappableExprComponentListRef Components) {
359 assert(Stack.size() > 1 &&
360 "Not expecting to retrieve components from a empty stack!");
361 auto &MEC = Stack.back().MappedExprComponents[VD];
362 // Create new entry and append the new components there.
363 MEC.resize(MEC.size() + 1);
364 MEC.back().append(Components.begin(), Components.end());
Kelvin Li0bff7af2015-11-23 05:32:03 +0000365 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000366
367 unsigned getNestingLevel() const {
368 assert(Stack.size() > 1);
369 return Stack.size() - 2;
370 }
Alexey Bataev8b427062016-05-25 12:36:08 +0000371 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
372 assert(Stack.size() > 2);
373 assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive));
374 Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
375 }
376 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
377 getDoacrossDependClauses() const {
378 assert(Stack.size() > 1);
379 if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
380 auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
381 return llvm::make_range(Ref.begin(), Ref.end());
382 }
383 return llvm::make_range(Stack[0].DoacrossDepends.end(),
384 Stack[0].DoacrossDepends.end());
385 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000387bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000388 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
389 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000390}
Alexey Bataeved09d242014-05-28 05:53:51 +0000391} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000393static ValueDecl *getCanonicalDecl(ValueDecl *D) {
394 auto *VD = dyn_cast<VarDecl>(D);
395 auto *FD = dyn_cast<FieldDecl>(D);
396 if (VD != nullptr) {
397 VD = VD->getCanonicalDecl();
398 D = VD;
399 } else {
400 assert(FD);
401 FD = FD->getCanonicalDecl();
402 D = FD;
403 }
404 return D;
405}
406
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000407DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000408 ValueDecl *D) {
409 D = getCanonicalDecl(D);
410 auto *VD = dyn_cast<VarDecl>(D);
411 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000412 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000413 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000414 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
415 // in a region but not in construct]
416 // File-scope or namespace-scope variables referenced in called routines
417 // in the region are shared unless they appear in a threadprivate
418 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000419 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 DVar.CKind = OMPC_shared;
421
422 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
423 // in a region but not in construct]
424 // Variables with static storage duration that are declared in called
425 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000426 if (VD && VD->hasGlobalStorage())
427 DVar.CKind = OMPC_shared;
428
429 // Non-static data members are shared by default.
430 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000431 DVar.CKind = OMPC_shared;
432
Alexey Bataev758e55e2013-09-06 18:03:48 +0000433 return DVar;
434 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000437 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
438 // in a Construct, C/C++, predetermined, p.1]
439 // Variables with automatic storage duration that are declared in a scope
440 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000441 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
442 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000443 DVar.CKind = OMPC_private;
444 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000445 }
446
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 // Explicitly specified attributes and local variables with predetermined
448 // attributes.
449 if (Iter->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000450 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000451 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000452 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000453 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 return DVar;
455 }
456
457 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
458 // in a Construct, C/C++, implicitly determined, p.1]
459 // In a parallel or task construct, the data-sharing attributes of these
460 // variables are determined by the default clause, if present.
461 switch (Iter->DefaultAttr) {
462 case DSA_shared:
463 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000464 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 return DVar;
466 case DSA_none:
467 return DVar;
468 case DSA_unspecified:
469 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470 // in a Construct, implicitly determined, p.2]
471 // In a parallel construct, if no default clause is present, these
472 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000473 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000474 if (isOpenMPParallelDirective(DVar.DKind) ||
475 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 DVar.CKind = OMPC_shared;
477 return DVar;
478 }
479
480 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
481 // in a Construct, implicitly determined, p.4]
482 // In a task construct, if no default clause is present, a variable that in
483 // the enclosing context is determined to be shared by all implicit tasks
484 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000485 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000486 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000487 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000488 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000489 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000490 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 // In a task construct, if no default clause is present, a variable
492 // whose data-sharing attribute is not determined by the rules above is
493 // firstprivate.
494 DVarTemp = getDSA(I, D);
495 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000496 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000497 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000498 return DVar;
499 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000500 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000501 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000502 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000504 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000505 return DVar;
506 }
507 }
508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
509 // in a Construct, implicitly determined, p.3]
510 // For constructs other than task, if no default clause is present, these
511 // variables inherit their data-sharing attributes from the enclosing
512 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000513 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000514}
515
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000516Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000517 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000518 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000519 auto It = Stack.back().AlignedMap.find(D);
520 if (It == Stack.back().AlignedMap.end()) {
521 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
522 Stack.back().AlignedMap[D] = NewDE;
523 return nullptr;
524 } else {
525 assert(It->second && "Unexpected nullptr expr in the aligned map");
526 return It->second;
527 }
528 return nullptr;
529}
530
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000531void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000532 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000533 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000534 Stack.back().LCVMap.insert(
535 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000536}
537
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000538DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000539 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000540 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000541 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
542 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000543}
544
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000545DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000546 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000547 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000548 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
549 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000550 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000551}
552
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000553ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000554 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
555 if (Stack[Stack.size() - 2].LCVMap.size() < I)
556 return nullptr;
557 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000558 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000559 return Pair.first;
560 }
561 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000562}
563
Alexey Bataev90c228f2016-02-08 09:29:13 +0000564void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
565 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000566 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000567 if (A == OMPC_threadprivate) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000568 auto &Data = Stack[0].SharingMap[D];
569 Data.Attributes = A;
570 Data.RefExpr.setPointer(E);
571 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000572 } else {
573 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000574 auto &Data = Stack.back().SharingMap[D];
575 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
576 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
577 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
578 (isLoopControlVariable(D).first && A == OMPC_private));
579 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
580 Data.RefExpr.setInt(/*IntVal=*/true);
581 return;
582 }
583 const bool IsLastprivate =
584 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
585 Data.Attributes = A;
586 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
587 Data.PrivateCopy = PrivateCopy;
588 if (PrivateCopy) {
589 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
590 Data.Attributes = A;
591 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
592 Data.PrivateCopy = nullptr;
593 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594 }
595}
596
Alexey Bataeved09d242014-05-28 05:53:51 +0000597bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000598 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000599 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000600 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000601 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000602 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000603 ++I;
604 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000605 if (I == E)
606 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000607 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000608 Scope *CurScope = getCurScope();
609 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000610 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000611 }
612 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000613 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000614 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000615}
616
Alexey Bataev39f915b82015-05-08 10:41:21 +0000617/// \brief Build a variable declaration for OpenMP loop iteration variable.
618static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000619 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000620 DeclContext *DC = SemaRef.CurContext;
621 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
622 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
623 VarDecl *Decl =
624 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000625 if (Attrs) {
626 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
627 I != E; ++I)
628 Decl->addAttr(*I);
629 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000630 Decl->setImplicit();
631 return Decl;
632}
633
634static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
635 SourceLocation Loc,
636 bool RefersToCapture = false) {
637 D->setReferenced();
638 D->markUsed(S.Context);
639 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
640 SourceLocation(), D, RefersToCapture, Loc, Ty,
641 VK_LValue);
642}
643
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000644DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
645 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000646 DSAVarData DVar;
647
648 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
649 // in a Construct, C/C++, predetermined, p.1]
650 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000651 auto *VD = dyn_cast<VarDecl>(D);
652 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
653 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000654 SemaRef.getLangOpts().OpenMPUseTLS &&
655 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000656 (VD && VD->getStorageClass() == SC_Register &&
657 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
658 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000659 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000660 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000661 }
662 if (Stack[0].SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000663 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 DVar.CKind = OMPC_threadprivate;
665 return DVar;
666 }
667
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000668 if (Stack.size() == 1) {
669 // Not in OpenMP execution region and top scope was already checked.
670 return DVar;
671 }
672
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000674 // in a Construct, C/C++, predetermined, p.4]
675 // Static data members are shared.
676 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
677 // in a Construct, C/C++, predetermined, p.7]
678 // Variables with static storage duration that are declared in a scope
679 // inside the construct are shared.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000680 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000681 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000682 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000683 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000684 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000686 DVar.CKind = OMPC_shared;
687 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000688 }
689
690 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000691 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
692 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000693 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
694 // in a Construct, C/C++, predetermined, p.6]
695 // Variables with const qualified type having no mutable member are
696 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000697 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000698 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000699 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
700 if (auto *CTD = CTSD->getSpecializedTemplate())
701 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000703 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
704 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000705 // Variables with const-qualified type having no mutable member may be
706 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000707 DSAVarData DVarTemp = hasDSA(
708 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
709 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000710 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
711 return DVar;
712
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713 DVar.CKind = OMPC_shared;
714 return DVar;
715 }
716
Alexey Bataev758e55e2013-09-06 18:03:48 +0000717 // Explicitly specified attributes and local variables with predetermined
718 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000724 auto I = std::prev(StartI);
725 if (I->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000726 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000727 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000728 DVar.CKind = I->SharingMap[D].Attributes;
729 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000730 }
731
732 return DVar;
733}
734
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000735DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
736 bool FromParent) {
737 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000738 auto StartI = Stack.rbegin();
739 auto EndI = std::prev(Stack.rend());
740 if (FromParent && StartI != EndI) {
741 StartI = std::next(StartI);
742 }
743 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000744}
745
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000746DSAStackTy::DSAVarData
747DSAStackTy::hasDSA(ValueDecl *D,
748 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
749 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
750 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000751 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000752 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000753 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000754 if (FromParent && StartI != EndI) {
755 StartI = std::next(StartI);
756 }
757 for (auto I = StartI, EE = EndI; I != EE; ++I) {
758 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000759 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000760 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000761 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000762 return DVar;
763 }
764 return DSAVarData();
765}
766
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000767DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
768 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
769 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
770 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000771 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000772 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000773 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000774 if (FromParent && StartI != EndI) {
775 StartI = std::next(StartI);
776 }
777 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000778 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000779 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000780 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000781 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000782 return DVar;
783 return DSAVarData();
784 }
785 return DSAVarData();
786}
787
Alexey Bataevaac108a2015-06-23 04:51:00 +0000788bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000789 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000790 unsigned Level, bool NotLastprivate) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000791 if (CPred(ClauseKindMode))
792 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000793 D = getCanonicalDecl(D);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000794 auto StartI = std::next(Stack.begin());
795 auto EndI = Stack.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000796 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000797 return false;
798 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000799 return (StartI->SharingMap.count(D) > 0) &&
800 StartI->SharingMap[D].RefExpr.getPointer() &&
801 CPred(StartI->SharingMap[D].Attributes) &&
802 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +0000803}
804
Samuel Antao4be30e92015-10-02 17:14:03 +0000805bool DSAStackTy::hasExplicitDirective(
806 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
807 unsigned Level) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000808 auto StartI = std::next(Stack.begin());
809 auto EndI = Stack.end();
Samuel Antao4be30e92015-10-02 17:14:03 +0000810 if (std::distance(StartI, EndI) <= (int)Level)
811 return false;
812 std::advance(StartI, Level);
813 return DPred(StartI->Directive);
814}
815
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000816bool DSAStackTy::hasDirective(
817 const llvm::function_ref<bool(OpenMPDirectiveKind,
818 const DeclarationNameInfo &, SourceLocation)>
819 &DPred,
820 bool FromParent) {
Samuel Antaof0d79752016-05-27 15:21:27 +0000821 // We look only in the enclosing region.
822 if (Stack.size() < 2)
823 return false;
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000824 auto StartI = std::next(Stack.rbegin());
825 auto EndI = std::prev(Stack.rend());
826 if (FromParent && StartI != EndI) {
827 StartI = std::next(StartI);
828 }
829 for (auto I = StartI, EE = EndI; I != EE; ++I) {
830 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
831 return true;
832 }
833 return false;
834}
835
Alexey Bataev758e55e2013-09-06 18:03:48 +0000836void Sema::InitDataSharingAttributesStack() {
837 VarDataSharingAttributesStack = new DSAStackTy(*this);
838}
839
840#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
841
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000842bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000843 assert(LangOpts.OpenMP && "OpenMP is not allowed");
844
845 auto &Ctx = getASTContext();
846 bool IsByRef = true;
847
848 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000849 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000850
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000851 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000852 // This table summarizes how a given variable should be passed to the device
853 // given its type and the clauses where it appears. This table is based on
854 // the description in OpenMP 4.5 [2.10.4, target Construct] and
855 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
856 //
857 // =========================================================================
858 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
859 // | |(tofrom:scalar)| | pvt | | | |
860 // =========================================================================
861 // | scl | | | | - | | bycopy|
862 // | scl | | - | x | - | - | bycopy|
863 // | scl | | x | - | - | - | null |
864 // | scl | x | | | - | | byref |
865 // | scl | x | - | x | - | - | bycopy|
866 // | scl | x | x | - | - | - | null |
867 // | scl | | - | - | - | x | byref |
868 // | scl | x | - | - | - | x | byref |
869 //
870 // | agg | n.a. | | | - | | byref |
871 // | agg | n.a. | - | x | - | - | byref |
872 // | agg | n.a. | x | - | - | - | null |
873 // | agg | n.a. | - | - | - | x | byref |
874 // | agg | n.a. | - | - | - | x[] | byref |
875 //
876 // | ptr | n.a. | | | - | | bycopy|
877 // | ptr | n.a. | - | x | - | - | bycopy|
878 // | ptr | n.a. | x | - | - | - | null |
879 // | ptr | n.a. | - | - | - | x | byref |
880 // | ptr | n.a. | - | - | - | x[] | bycopy|
881 // | ptr | n.a. | - | - | x | | bycopy|
882 // | ptr | n.a. | - | - | x | x | bycopy|
883 // | ptr | n.a. | - | - | x | x[] | bycopy|
884 // =========================================================================
885 // Legend:
886 // scl - scalar
887 // ptr - pointer
888 // agg - aggregate
889 // x - applies
890 // - - invalid in this combination
891 // [] - mapped with an array section
892 // byref - should be mapped by reference
893 // byval - should be mapped by value
894 // null - initialize a local variable to null on the device
895 //
896 // Observations:
897 // - All scalar declarations that show up in a map clause have to be passed
898 // by reference, because they may have been mapped in the enclosing data
899 // environment.
900 // - If the scalar value does not fit the size of uintptr, it has to be
901 // passed by reference, regardless the result in the table above.
902 // - For pointers mapped by value that have either an implicit map or an
903 // array section, the runtime library may pass the NULL value to the
904 // device instead of the value passed to it by the compiler.
905
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000906
907 if (Ty->isReferenceType())
908 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000909
910 // Locate map clauses and see if the variable being captured is referred to
911 // in any of those clauses. Here we only care about variables, not fields,
912 // because fields are part of aggregates.
913 bool IsVariableUsedInMapClause = false;
914 bool IsVariableAssociatedWithSection = false;
915
916 DSAStack->checkMappableExprComponentListsForDecl(
917 D, /*CurrentRegionOnly=*/true,
918 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
919 MapExprComponents) {
920
921 auto EI = MapExprComponents.rbegin();
922 auto EE = MapExprComponents.rend();
923
924 assert(EI != EE && "Invalid map expression!");
925
926 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
927 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
928
929 ++EI;
930 if (EI == EE)
931 return false;
932
933 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
934 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
935 isa<MemberExpr>(EI->getAssociatedExpression())) {
936 IsVariableAssociatedWithSection = true;
937 // There is nothing more we need to know about this variable.
938 return true;
939 }
940
941 // Keep looking for more map info.
942 return false;
943 });
944
945 if (IsVariableUsedInMapClause) {
946 // If variable is identified in a map clause it is always captured by
947 // reference except if it is a pointer that is dereferenced somehow.
948 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
949 } else {
950 // By default, all the data that has a scalar type is mapped by copy.
951 IsByRef = !Ty->isScalarType();
952 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000953 }
954
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000955 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
956 IsByRef = !DSAStack->hasExplicitDSA(
957 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
958 Level, /*NotLastprivate=*/true);
959 }
960
Samuel Antao86ace552016-04-27 22:40:57 +0000961 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000962 // and alignment, because the runtime library only deals with uintptr types.
963 // If it does not fit the uintptr size, we need to pass the data by reference
964 // instead.
965 if (!IsByRef &&
966 (Ctx.getTypeSizeInChars(Ty) >
967 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000968 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000969 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000970 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000971
972 return IsByRef;
973}
974
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000975unsigned Sema::getOpenMPNestingLevel() const {
976 assert(getLangOpts().OpenMP);
977 return DSAStack->getNestingLevel();
978}
979
Alexey Bataev90c228f2016-02-08 09:29:13 +0000980VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000981 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000982 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000983
984 // If we are attempting to capture a global variable in a directive with
985 // 'target' we return true so that this global is also mapped to the device.
986 //
987 // FIXME: If the declaration is enclosed in a 'declare target' directive,
988 // then it should not be captured. Therefore, an extra check has to be
989 // inserted here once support for 'declare target' is added.
990 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000991 auto *VD = dyn_cast<VarDecl>(D);
992 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000993 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000994 !DSAStack->isClauseParsingMode())
995 return VD;
Samuel Antaof0d79752016-05-27 15:21:27 +0000996 if (DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000997 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
998 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000999 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +00001000 },
Alexey Bataev90c228f2016-02-08 09:29:13 +00001001 false))
1002 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +00001003 }
1004
Alexey Bataev48977c32015-08-04 08:10:48 +00001005 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1006 (!DSAStack->isClauseParsingMode() ||
1007 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001008 auto &&Info = DSAStack->isLoopControlVariable(D);
1009 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001010 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001011 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001012 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001013 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001014 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001015 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001016 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001017 DVarPrivate = DSAStack->hasDSA(
1018 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1019 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001020 if (DVarPrivate.CKind != OMPC_unknown)
1021 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001022 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001023 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001024}
1025
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001026bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001027 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1028 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001029 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001030}
1031
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001032bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001033 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1034 // Return true if the current level is no longer enclosed in a target region.
1035
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001036 auto *VD = dyn_cast<VarDecl>(D);
1037 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001038 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1039 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001040}
1041
Alexey Bataeved09d242014-05-28 05:53:51 +00001042void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001043
1044void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1045 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001046 Scope *CurScope, SourceLocation Loc) {
1047 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001048 PushExpressionEvaluationContext(PotentiallyEvaluated);
1049}
1050
Alexey Bataevaac108a2015-06-23 04:51:00 +00001051void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1052 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001053}
1054
Alexey Bataevaac108a2015-06-23 04:51:00 +00001055void Sema::EndOpenMPClause() {
1056 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001057}
1058
Alexey Bataev758e55e2013-09-06 18:03:48 +00001059void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001060 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1061 // A variable of class type (or array thereof) that appears in a lastprivate
1062 // clause requires an accessible, unambiguous default constructor for the
1063 // class type, unless the list item is also specified in a firstprivate
1064 // clause.
1065 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001066 for (auto *C : D->clauses()) {
1067 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1068 SmallVector<Expr *, 8> PrivateCopies;
1069 for (auto *DE : Clause->varlists()) {
1070 if (DE->isValueDependent() || DE->isTypeDependent()) {
1071 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001072 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001073 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001074 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001075 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1076 QualType Type = VD->getType().getNonReferenceType();
1077 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001078 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001079 // Generate helper private variable and initialize it with the
1080 // default value. The address of the original variable is replaced
1081 // by the address of the new private variable in CodeGen. This new
1082 // variable is not added to IdResolver, so the code in the OpenMP
1083 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001084 auto *VDPrivate = buildVarDecl(
1085 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001086 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001087 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1088 if (VDPrivate->isInvalidDecl())
1089 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001090 PrivateCopies.push_back(buildDeclRefExpr(
1091 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001092 } else {
1093 // The variable is also a firstprivate, so initialization sequence
1094 // for private copy is generated already.
1095 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001096 }
1097 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001098 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001099 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001100 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001101 }
1102 }
1103 }
1104
Alexey Bataev758e55e2013-09-06 18:03:48 +00001105 DSAStack->pop();
1106 DiscardCleanupsInEvaluationContext();
1107 PopExpressionEvaluationContext();
1108}
1109
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001110static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1111 Expr *NumIterations, Sema &SemaRef,
1112 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001113
Alexey Bataeva769e072013-03-22 06:34:35 +00001114namespace {
1115
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001116class VarDeclFilterCCC : public CorrectionCandidateCallback {
1117private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001118 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001119
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001120public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001121 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001122 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001123 NamedDecl *ND = Candidate.getCorrectionDecl();
1124 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1125 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001126 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1127 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001128 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001129 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001130 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001131};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001132
1133class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1134private:
1135 Sema &SemaRef;
1136
1137public:
1138 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1139 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1140 NamedDecl *ND = Candidate.getCorrectionDecl();
1141 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1142 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1143 SemaRef.getCurScope());
1144 }
1145 return false;
1146 }
1147};
1148
Alexey Bataeved09d242014-05-28 05:53:51 +00001149} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001150
1151ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1152 CXXScopeSpec &ScopeSpec,
1153 const DeclarationNameInfo &Id) {
1154 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1155 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1156
1157 if (Lookup.isAmbiguous())
1158 return ExprError();
1159
1160 VarDecl *VD;
1161 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001162 if (TypoCorrection Corrected = CorrectTypo(
1163 Id, LookupOrdinaryName, CurScope, nullptr,
1164 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001165 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001166 PDiag(Lookup.empty()
1167 ? diag::err_undeclared_var_use_suggest
1168 : diag::err_omp_expected_var_arg_suggest)
1169 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001170 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001172 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1173 : diag::err_omp_expected_var_arg)
1174 << Id.getName();
1175 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001177 } else {
1178 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001179 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1181 return ExprError();
1182 }
1183 }
1184 Lookup.suppressDiagnostics();
1185
1186 // OpenMP [2.9.2, Syntax, C/C++]
1187 // Variables must be file-scope, namespace-scope, or static block-scope.
1188 if (!VD->hasGlobalStorage()) {
1189 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001190 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1191 bool IsDecl =
1192 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1195 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001196 return ExprError();
1197 }
1198
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001199 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1200 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001201 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1202 // A threadprivate directive for file-scope variables must appear outside
1203 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001204 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1205 !getCurLexicalContext()->isTranslationUnit()) {
1206 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001207 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1208 bool IsDecl =
1209 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1210 Diag(VD->getLocation(),
1211 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1212 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001213 return ExprError();
1214 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001215 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1216 // A threadprivate directive for static class member variables must appear
1217 // in the class definition, in the same scope in which the member
1218 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001219 if (CanonicalVD->isStaticDataMember() &&
1220 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1221 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001222 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1223 bool IsDecl =
1224 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1225 Diag(VD->getLocation(),
1226 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1227 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001228 return ExprError();
1229 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1231 // A threadprivate directive for namespace-scope variables must appear
1232 // outside any definition or declaration other than the namespace
1233 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001234 if (CanonicalVD->getDeclContext()->isNamespace() &&
1235 (!getCurLexicalContext()->isFileContext() ||
1236 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1237 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001238 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1239 bool IsDecl =
1240 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1241 Diag(VD->getLocation(),
1242 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1243 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001244 return ExprError();
1245 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001246 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1247 // A threadprivate directive for static block-scope variables must appear
1248 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001249 if (CanonicalVD->isStaticLocal() && CurScope &&
1250 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001251 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001252 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1253 bool IsDecl =
1254 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1255 Diag(VD->getLocation(),
1256 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1257 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001258 return ExprError();
1259 }
1260
1261 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1262 // A threadprivate directive must lexically precede all references to any
1263 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001264 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001265 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001266 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001267 return ExprError();
1268 }
1269
1270 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001271 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1272 SourceLocation(), VD,
1273 /*RefersToEnclosingVariableOrCapture=*/false,
1274 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001275}
1276
Alexey Bataeved09d242014-05-28 05:53:51 +00001277Sema::DeclGroupPtrTy
1278Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1279 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001280 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001281 CurContext->addDecl(D);
1282 return DeclGroupPtrTy::make(DeclGroupRef(D));
1283 }
David Blaikie0403cb12016-01-15 23:43:25 +00001284 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001285}
1286
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001287namespace {
1288class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1289 Sema &SemaRef;
1290
1291public:
1292 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1293 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1294 if (VD->hasLocalStorage()) {
1295 SemaRef.Diag(E->getLocStart(),
1296 diag::err_omp_local_var_in_threadprivate_init)
1297 << E->getSourceRange();
1298 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1299 << VD << VD->getSourceRange();
1300 return true;
1301 }
1302 }
1303 return false;
1304 }
1305 bool VisitStmt(const Stmt *S) {
1306 for (auto Child : S->children()) {
1307 if (Child && Visit(Child))
1308 return true;
1309 }
1310 return false;
1311 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001312 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001313};
1314} // namespace
1315
Alexey Bataeved09d242014-05-28 05:53:51 +00001316OMPThreadPrivateDecl *
1317Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001318 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001319 for (auto &RefExpr : VarList) {
1320 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001321 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1322 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001323
Alexey Bataev376b4a42016-02-09 09:41:09 +00001324 // Mark variable as used.
1325 VD->setReferenced();
1326 VD->markUsed(Context);
1327
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001328 QualType QType = VD->getType();
1329 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1330 // It will be analyzed later.
1331 Vars.push_back(DE);
1332 continue;
1333 }
1334
Alexey Bataeva769e072013-03-22 06:34:35 +00001335 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1336 // A threadprivate variable must not have an incomplete type.
1337 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001338 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001339 continue;
1340 }
1341
1342 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1343 // A threadprivate variable must not have a reference type.
1344 if (VD->getType()->isReferenceType()) {
1345 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001346 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1347 bool IsDecl =
1348 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1349 Diag(VD->getLocation(),
1350 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1351 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001352 continue;
1353 }
1354
Samuel Antaof8b50122015-07-13 22:54:53 +00001355 // Check if this is a TLS variable. If TLS is not being supported, produce
1356 // the corresponding diagnostic.
1357 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1358 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1359 getLangOpts().OpenMPUseTLS &&
1360 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001361 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1362 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001363 Diag(ILoc, diag::err_omp_var_thread_local)
1364 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001365 bool IsDecl =
1366 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1367 Diag(VD->getLocation(),
1368 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1369 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001370 continue;
1371 }
1372
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001373 // Check if initial value of threadprivate variable reference variable with
1374 // local storage (it is not supported by runtime).
1375 if (auto Init = VD->getAnyInitializer()) {
1376 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001377 if (Checker.Visit(Init))
1378 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001379 }
1380
Alexey Bataeved09d242014-05-28 05:53:51 +00001381 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001382 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001383 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1384 Context, SourceRange(Loc, Loc)));
1385 if (auto *ML = Context.getASTMutationListener())
1386 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001387 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001388 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001389 if (!Vars.empty()) {
1390 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1391 Vars);
1392 D->setAccess(AS_public);
1393 }
1394 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001395}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001396
Alexey Bataev7ff55242014-06-19 09:13:45 +00001397static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001398 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001399 bool IsLoopIterVar = false) {
1400 if (DVar.RefExpr) {
1401 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1402 << getOpenMPClauseName(DVar.CKind);
1403 return;
1404 }
1405 enum {
1406 PDSA_StaticMemberShared,
1407 PDSA_StaticLocalVarShared,
1408 PDSA_LoopIterVarPrivate,
1409 PDSA_LoopIterVarLinear,
1410 PDSA_LoopIterVarLastprivate,
1411 PDSA_ConstVarShared,
1412 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001413 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001414 PDSA_LocalVarPrivate,
1415 PDSA_Implicit
1416 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001417 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001418 auto ReportLoc = D->getLocation();
1419 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001420 if (IsLoopIterVar) {
1421 if (DVar.CKind == OMPC_private)
1422 Reason = PDSA_LoopIterVarPrivate;
1423 else if (DVar.CKind == OMPC_lastprivate)
1424 Reason = PDSA_LoopIterVarLastprivate;
1425 else
1426 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001427 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1428 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001429 Reason = PDSA_TaskVarFirstprivate;
1430 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001431 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001432 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001433 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001434 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001435 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001436 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001437 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001438 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001439 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001440 ReportHint = true;
1441 Reason = PDSA_LocalVarPrivate;
1442 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001443 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001444 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001445 << Reason << ReportHint
1446 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1447 } else if (DVar.ImplicitDSALoc.isValid()) {
1448 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1449 << getOpenMPClauseName(DVar.CKind);
1450 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001451}
1452
Alexey Bataev758e55e2013-09-06 18:03:48 +00001453namespace {
1454class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1455 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001456 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001457 bool ErrorFound;
1458 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001459 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001460 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001461
Alexey Bataev758e55e2013-09-06 18:03:48 +00001462public:
1463 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001464 if (E->isTypeDependent() || E->isValueDependent() ||
1465 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1466 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001467 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001469 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1470 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001471
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001472 auto DVar = Stack->getTopDSA(VD, false);
1473 // Check if the variable has explicit DSA set and stop analysis if it so.
1474 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001476 auto ELoc = E->getExprLoc();
1477 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001478 // The default(none) clause requires that each variable that is referenced
1479 // in the construct, and does not have a predetermined data-sharing
1480 // attribute, must have its data-sharing attribute explicitly determined
1481 // by being listed in a data-sharing attribute clause.
1482 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001483 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001484 VarsWithInheritedDSA.count(VD) == 0) {
1485 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486 return;
1487 }
1488
1489 // OpenMP [2.9.3.6, Restrictions, p.2]
1490 // A list item that appears in a reduction clause of the innermost
1491 // enclosing worksharing or parallel construct may not be accessed in an
1492 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001493 DVar = Stack->hasInnermostDSA(
1494 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1495 [](OpenMPDirectiveKind K) -> bool {
1496 return isOpenMPParallelDirective(K) ||
1497 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1498 },
1499 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001500 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001501 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001502 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1503 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001504 return;
1505 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001506
1507 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001508 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001509 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1510 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001511 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001512 }
1513 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001514 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001515 if (E->isTypeDependent() || E->isValueDependent() ||
1516 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1517 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001518 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1519 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1520 auto DVar = Stack->getTopDSA(FD, false);
1521 // Check if the variable has explicit DSA set and stop analysis if it
1522 // so.
1523 if (DVar.RefExpr)
1524 return;
1525
1526 auto ELoc = E->getExprLoc();
1527 auto DKind = Stack->getCurrentDirective();
1528 // OpenMP [2.9.3.6, Restrictions, p.2]
1529 // A list item that appears in a reduction clause of the innermost
1530 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001531 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001532 DVar = Stack->hasInnermostDSA(
1533 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1534 [](OpenMPDirectiveKind K) -> bool {
1535 return isOpenMPParallelDirective(K) ||
1536 isOpenMPWorksharingDirective(K) ||
1537 isOpenMPTeamsDirective(K);
1538 },
1539 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001540 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001541 ErrorFound = true;
1542 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1543 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1544 return;
1545 }
1546
1547 // Define implicit data-sharing attributes for task.
1548 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001549 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1550 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001551 ImplicitFirstprivate.push_back(E);
1552 }
1553 }
1554 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001555 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001556 for (auto *C : S->clauses()) {
1557 // Skip analysis of arguments of implicitly defined firstprivate clause
1558 // for task directives.
1559 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1560 for (auto *CC : C->children()) {
1561 if (CC)
1562 Visit(CC);
1563 }
1564 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001565 }
1566 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001567 for (auto *C : S->children()) {
1568 if (C && !isa<OMPExecutableDirective>(C))
1569 Visit(C);
1570 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001571 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001572
1573 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001574 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001575 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001576 return VarsWithInheritedDSA;
1577 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001578
Alexey Bataev7ff55242014-06-19 09:13:45 +00001579 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1580 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001581};
Alexey Bataeved09d242014-05-28 05:53:51 +00001582} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001583
Alexey Bataevbae9a792014-06-27 10:37:06 +00001584void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001585 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00001586 case OMPD_parallel:
1587 case OMPD_parallel_for:
1588 case OMPD_parallel_for_simd:
1589 case OMPD_parallel_sections:
1590 case OMPD_teams: {
Alexey Bataev9959db52014-05-06 10:08:46 +00001591 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001592 QualType KmpInt32PtrTy =
1593 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001594 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001595 std::make_pair(".global_tid.", KmpInt32PtrTy),
1596 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1597 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001598 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001599 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1600 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001601 break;
1602 }
Kelvin Li70a12c52016-07-13 21:51:49 +00001603 case OMPD_simd:
1604 case OMPD_for:
1605 case OMPD_for_simd:
1606 case OMPD_sections:
1607 case OMPD_section:
1608 case OMPD_single:
1609 case OMPD_master:
1610 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00001611 case OMPD_taskgroup:
1612 case OMPD_distribute:
Kelvin Li70a12c52016-07-13 21:51:49 +00001613 case OMPD_ordered:
1614 case OMPD_atomic:
1615 case OMPD_target_data:
1616 case OMPD_target:
1617 case OMPD_target_parallel:
1618 case OMPD_target_parallel_for:
Kelvin Lia579b912016-07-14 02:54:56 +00001619 case OMPD_target_parallel_for_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001620 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001621 std::make_pair(StringRef(), QualType()) // __context with shared vars
1622 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001623 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1624 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001625 break;
1626 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001627 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001628 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001629 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1630 FunctionProtoType::ExtProtoInfo EPI;
1631 EPI.Variadic = true;
1632 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001633 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001634 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001635 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1636 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1637 std::make_pair(".copy_fn.",
1638 Context.getPointerType(CopyFnType).withConst()),
1639 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001640 std::make_pair(StringRef(), QualType()) // __context with shared vars
1641 };
1642 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1643 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001644 // Mark this captured region as inlined, because we don't use outlined
1645 // function directly.
1646 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1647 AlwaysInlineAttr::CreateImplicit(
1648 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001649 break;
1650 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001651 case OMPD_taskloop:
1652 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001653 QualType KmpInt32Ty =
1654 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1655 QualType KmpUInt64Ty =
1656 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1657 QualType KmpInt64Ty =
1658 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1659 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1660 FunctionProtoType::ExtProtoInfo EPI;
1661 EPI.Variadic = true;
1662 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001663 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001664 std::make_pair(".global_tid.", KmpInt32Ty),
1665 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1666 std::make_pair(".privates.",
1667 Context.VoidPtrTy.withConst().withRestrict()),
1668 std::make_pair(
1669 ".copy_fn.",
1670 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1671 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1672 std::make_pair(".lb.", KmpUInt64Ty),
1673 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1674 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001675 std::make_pair(StringRef(), QualType()) // __context with shared vars
1676 };
1677 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1678 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001679 // Mark this captured region as inlined, because we don't use outlined
1680 // function directly.
1681 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1682 AlwaysInlineAttr::CreateImplicit(
1683 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001684 break;
1685 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001686 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001687 case OMPD_distribute_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001688 case OMPD_distribute_parallel_for: {
1689 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1690 QualType KmpInt32PtrTy =
1691 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1692 Sema::CapturedParamNameType Params[] = {
1693 std::make_pair(".global_tid.", KmpInt32PtrTy),
1694 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1695 std::make_pair(".previous.lb.", Context.getSizeType()),
1696 std::make_pair(".previous.ub.", Context.getSizeType()),
1697 std::make_pair(StringRef(), QualType()) // __context with shared vars
1698 };
1699 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1700 Params);
1701 break;
1702 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001703 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001704 case OMPD_taskyield:
1705 case OMPD_barrier:
1706 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001707 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001708 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001709 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001710 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001711 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001712 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001713 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001714 case OMPD_declare_target:
1715 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001716 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001717 llvm_unreachable("OpenMP Directive is not allowed");
1718 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001719 llvm_unreachable("Unknown OpenMP directive");
1720 }
1721}
1722
Alexey Bataev3392d762016-02-16 11:18:12 +00001723static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001724 Expr *CaptureExpr, bool WithInit,
1725 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001726 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001727 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001728 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001729 QualType Ty = Init->getType();
1730 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1731 if (S.getLangOpts().CPlusPlus)
1732 Ty = C.getLValueReferenceType(Ty);
1733 else {
1734 Ty = C.getPointerType(Ty);
1735 ExprResult Res =
1736 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1737 if (!Res.isUsable())
1738 return nullptr;
1739 Init = Res.get();
1740 }
Alexey Bataev61205072016-03-02 04:57:40 +00001741 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001742 }
1743 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001744 if (!WithInit)
1745 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001746 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001747 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1748 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001749 return CED;
1750}
1751
Alexey Bataev61205072016-03-02 04:57:40 +00001752static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1753 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001754 OMPCapturedExprDecl *CD;
1755 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1756 CD = cast<OMPCapturedExprDecl>(VD);
1757 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001758 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1759 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001760 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001761 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001762}
1763
Alexey Bataev5a3af132016-03-29 08:58:54 +00001764static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1765 if (!Ref) {
1766 auto *CD =
1767 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1768 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1769 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1770 CaptureExpr->getExprLoc());
1771 }
1772 ExprResult Res = Ref;
1773 if (!S.getLangOpts().CPlusPlus &&
1774 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1775 Ref->getType()->isPointerType())
1776 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1777 if (!Res.isUsable())
1778 return ExprError();
1779 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001780}
1781
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001782StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1783 ArrayRef<OMPClause *> Clauses) {
1784 if (!S.isUsable()) {
1785 ActOnCapturedRegionError();
1786 return StmtError();
1787 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001788
1789 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001790 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001791 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001792 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001793 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001794 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001795 Clause->getClauseKind() == OMPC_copyprivate ||
1796 (getLangOpts().OpenMPUseTLS &&
1797 getASTContext().getTargetInfo().isTLSSupported() &&
1798 Clause->getClauseKind() == OMPC_copyin)) {
1799 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001800 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001801 for (auto *VarRef : Clause->children()) {
1802 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001803 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001804 }
1805 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001806 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001807 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001808 // Mark all variables in private list clauses as used in inner region.
1809 // Required for proper codegen of combined directives.
1810 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001811 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001812 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1813 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001814 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1815 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001816 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001817 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1818 if (auto *E = C->getPostUpdateExpr())
1819 MarkDeclarationsReferencedInExpr(E);
1820 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001821 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001822 if (Clause->getClauseKind() == OMPC_schedule)
1823 SC = cast<OMPScheduleClause>(Clause);
1824 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001825 OC = cast<OMPOrderedClause>(Clause);
1826 else if (Clause->getClauseKind() == OMPC_linear)
1827 LCs.push_back(cast<OMPLinearClause>(Clause));
1828 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001829 bool ErrorFound = false;
1830 // OpenMP, 2.7.1 Loop Construct, Restrictions
1831 // The nonmonotonic modifier cannot be specified if an ordered clause is
1832 // specified.
1833 if (SC &&
1834 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1835 SC->getSecondScheduleModifier() ==
1836 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1837 OC) {
1838 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1839 ? SC->getFirstScheduleModifierLoc()
1840 : SC->getSecondScheduleModifierLoc(),
1841 diag::err_omp_schedule_nonmonotonic_ordered)
1842 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1843 ErrorFound = true;
1844 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001845 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1846 for (auto *C : LCs) {
1847 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1848 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1849 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001850 ErrorFound = true;
1851 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001852 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1853 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1854 OC->getNumForLoops()) {
1855 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1856 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1857 ErrorFound = true;
1858 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001859 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001860 ActOnCapturedRegionError();
1861 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001862 }
1863 return ActOnCapturedRegionEnd(S.get());
1864}
1865
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001866static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1867 OpenMPDirectiveKind CurrentRegion,
1868 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001869 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001870 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001871 // Allowed nesting of constructs
1872 // +------------------+-----------------+------------------------------------+
1873 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1874 // +------------------+-----------------+------------------------------------+
1875 // | parallel | parallel | * |
1876 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001877 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001878 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001879 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001880 // | parallel | simd | * |
1881 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001882 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001883 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001884 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001885 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001886 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001887 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001888 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001889 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001890 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001891 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001892 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001893 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001894 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001895 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001896 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001897 // | parallel | target parallel | * |
1898 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001899 // | parallel | target enter | * |
1900 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001901 // | parallel | target exit | * |
1902 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001903 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001904 // | parallel | cancellation | |
1905 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001906 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001907 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001908 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001909 // | parallel | distribute | + |
1910 // | parallel | distribute | + |
1911 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001912 // | parallel | distribute | + |
1913 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001914 // | parallel | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001915 // +------------------+-----------------+------------------------------------+
1916 // | for | parallel | * |
1917 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001918 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001919 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001920 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001921 // | for | simd | * |
1922 // | for | sections | + |
1923 // | for | section | + |
1924 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001925 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001926 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001927 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001928 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001929 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001930 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001931 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001932 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001933 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001934 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001935 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001936 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001937 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001938 // | for | target parallel | * |
1939 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001940 // | for | target enter | * |
1941 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001942 // | for | target exit | * |
1943 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001944 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001945 // | for | cancellation | |
1946 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001947 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001948 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001949 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001950 // | for | distribute | + |
1951 // | for | distribute | + |
1952 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001953 // | for | distribute | + |
1954 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001955 // | for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001956 // | for | target parallel | + |
1957 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001958 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001959 // | master | parallel | * |
1960 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001961 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001962 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001963 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001964 // | master | simd | * |
1965 // | master | sections | + |
1966 // | master | section | + |
1967 // | master | single | + |
1968 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001969 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001970 // | master |parallel sections| * |
1971 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001972 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001973 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001974 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001975 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001976 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001977 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001978 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001979 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001980 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001981 // | master | target parallel | * |
1982 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001983 // | master | target enter | * |
1984 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001985 // | master | target exit | * |
1986 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001987 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001988 // | master | cancellation | |
1989 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001990 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001991 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001992 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001993 // | master | distribute | + |
1994 // | master | distribute | + |
1995 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001996 // | master | distribute | + |
1997 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001998 // | master | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001999 // | master | target parallel | + |
2000 // | | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002001 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002002 // | critical | parallel | * |
2003 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002004 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002005 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002006 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002007 // | critical | simd | * |
2008 // | critical | sections | + |
2009 // | critical | section | + |
2010 // | critical | single | + |
2011 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002012 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002013 // | critical |parallel sections| * |
2014 // | critical | task | * |
2015 // | critical | taskyield | * |
2016 // | critical | barrier | + |
2017 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002018 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002019 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002020 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002021 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002022 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002023 // | critical | target parallel | * |
2024 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002025 // | critical | target enter | * |
2026 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002027 // | critical | target exit | * |
2028 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002029 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002030 // | critical | cancellation | |
2031 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002032 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002033 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002034 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002035 // | critical | distribute | + |
2036 // | critical | distribute | + |
2037 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002038 // | critical | distribute | + |
2039 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002040 // | critical | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002041 // | critical | target parallel | + |
2042 // | | for simd | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002043 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002044 // | simd | parallel | |
2045 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002046 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002047 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002048 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002049 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002050 // | simd | sections | |
2051 // | simd | section | |
2052 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002053 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002054 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002055 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002056 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002057 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002058 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002059 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002060 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002061 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002062 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002063 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002064 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002065 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002066 // | simd | target parallel | |
2067 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002068 // | simd | target enter | |
2069 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002070 // | simd | target exit | |
2071 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002072 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002073 // | simd | cancellation | |
2074 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002075 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002076 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002077 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002078 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002079 // | simd | distribute | |
2080 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002081 // | simd | distribute | |
2082 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002083 // | simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002084 // | simd | target parallel | |
2085 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002086 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002087 // | for simd | parallel | |
2088 // | for simd | for | |
2089 // | for simd | for simd | |
2090 // | for simd | master | |
2091 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002092 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002093 // | for simd | sections | |
2094 // | for simd | section | |
2095 // | for simd | single | |
2096 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002097 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002098 // | for simd |parallel sections| |
2099 // | for simd | task | |
2100 // | for simd | taskyield | |
2101 // | for simd | barrier | |
2102 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002103 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002104 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002105 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002106 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002107 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002108 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002109 // | for simd | target parallel | |
2110 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002111 // | for simd | target enter | |
2112 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002113 // | for simd | target exit | |
2114 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002115 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002116 // | for simd | cancellation | |
2117 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002118 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002119 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002120 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002121 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002122 // | for simd | distribute | |
2123 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002124 // | for simd | distribute | |
2125 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002126 // | for simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002127 // | for simd | target parallel | |
2128 // | | for simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002129 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002130 // | parallel for simd| parallel | |
2131 // | parallel for simd| for | |
2132 // | parallel for simd| for simd | |
2133 // | parallel for simd| master | |
2134 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002135 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002136 // | parallel for simd| sections | |
2137 // | parallel for simd| section | |
2138 // | parallel for simd| single | |
2139 // | parallel for simd| parallel for | |
2140 // | parallel for simd|parallel for simd| |
2141 // | parallel for simd|parallel sections| |
2142 // | parallel for simd| task | |
2143 // | parallel for simd| taskyield | |
2144 // | parallel for simd| barrier | |
2145 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002146 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002147 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002148 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002149 // | parallel for simd| atomic | |
2150 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002151 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002152 // | parallel for simd| target parallel | |
2153 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002154 // | parallel for simd| target enter | |
2155 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002156 // | parallel for simd| target exit | |
2157 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002158 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002159 // | parallel for simd| cancellation | |
2160 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002161 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002162 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002163 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002164 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002165 // | parallel for simd| distribute | |
2166 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002167 // | parallel for simd| distribute | |
2168 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002169 // | parallel for simd| distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002170 // | | for simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002171 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002172 // | sections | parallel | * |
2173 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002174 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002175 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002176 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002177 // | sections | simd | * |
2178 // | sections | sections | + |
2179 // | sections | section | * |
2180 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002181 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002182 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002183 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002184 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002185 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002186 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002187 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002188 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002189 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002190 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002191 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002192 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002193 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002194 // | sections | target parallel | * |
2195 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002196 // | sections | target enter | * |
2197 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002198 // | sections | target exit | * |
2199 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002200 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002201 // | sections | cancellation | |
2202 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002203 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002204 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002205 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002206 // | sections | distribute | + |
2207 // | sections | distribute | + |
2208 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002209 // | sections | distribute | + |
2210 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002211 // | sections | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002212 // | sections | target parallel | + |
2213 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002214 // +------------------+-----------------+------------------------------------+
2215 // | section | parallel | * |
2216 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002217 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002218 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002219 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002220 // | section | simd | * |
2221 // | section | sections | + |
2222 // | section | section | + |
2223 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002224 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002225 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002226 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002227 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002228 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002229 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002230 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002231 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002232 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002233 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002234 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002235 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002236 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002237 // | section | target parallel | * |
2238 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002239 // | section | target enter | * |
2240 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002241 // | section | target exit | * |
2242 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002243 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002244 // | section | cancellation | |
2245 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002246 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002247 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002248 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002249 // | section | distribute | + |
2250 // | section | distribute | + |
2251 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002252 // | section | distribute | + |
2253 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002254 // | section | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002255 // | section | target parallel | + |
2256 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002257 // +------------------+-----------------+------------------------------------+
2258 // | single | parallel | * |
2259 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002260 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002261 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002262 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002263 // | single | simd | * |
2264 // | single | sections | + |
2265 // | single | section | + |
2266 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002267 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002268 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002269 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002270 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002271 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002272 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002273 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002274 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002275 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002276 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002277 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002278 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002279 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002280 // | single | target parallel | * |
2281 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002282 // | single | target enter | * |
2283 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002284 // | single | target exit | * |
2285 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002286 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002287 // | single | cancellation | |
2288 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002289 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002290 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002291 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002292 // | single | distribute | + |
2293 // | single | distribute | + |
2294 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002295 // | single | distribute | + |
2296 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002297 // | single | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002298 // | single | target parallel | + |
2299 // | | for simd | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002300 // +------------------+-----------------+------------------------------------+
2301 // | parallel for | parallel | * |
2302 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002303 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002304 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002305 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002306 // | parallel for | simd | * |
2307 // | parallel for | sections | + |
2308 // | parallel for | section | + |
2309 // | parallel for | single | + |
2310 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002311 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002312 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002313 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002314 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002315 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002316 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002317 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002318 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002319 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002320 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002321 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002322 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002323 // | parallel for | target parallel | * |
2324 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002325 // | parallel for | target enter | * |
2326 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002327 // | parallel for | target exit | * |
2328 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002329 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002330 // | parallel for | cancellation | |
2331 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002332 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002333 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002334 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002335 // | parallel for | distribute | + |
2336 // | parallel for | distribute | + |
2337 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002338 // | parallel for | distribute | + |
2339 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002340 // | parallel for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002341 // | parallel for | target parallel | + |
2342 // | | for simd | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002343 // +------------------+-----------------+------------------------------------+
2344 // | parallel sections| parallel | * |
2345 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002346 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002347 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002348 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002349 // | parallel sections| simd | * |
2350 // | parallel sections| sections | + |
2351 // | parallel sections| section | * |
2352 // | parallel sections| single | + |
2353 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002354 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002355 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002356 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002357 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002358 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002359 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002360 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002361 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002362 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002363 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002364 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002365 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002366 // | parallel sections| target parallel | * |
2367 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002368 // | parallel sections| target enter | * |
2369 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002370 // | parallel sections| target exit | * |
2371 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002372 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002373 // | parallel sections| cancellation | |
2374 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002375 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002376 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002377 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002378 // | parallel sections| distribute | + |
2379 // | parallel sections| distribute | + |
2380 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002381 // | parallel sections| distribute | + |
2382 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002383 // | parallel sections| distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002384 // | parallel sections| target parallel | + |
2385 // | | for simd | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002386 // +------------------+-----------------+------------------------------------+
2387 // | task | parallel | * |
2388 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002389 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002390 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002391 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002392 // | task | simd | * |
2393 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002394 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002395 // | task | single | + |
2396 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002397 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002398 // | task |parallel sections| * |
2399 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002400 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002401 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002402 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002403 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002404 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002405 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002406 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002407 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002408 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002409 // | task | target parallel | * |
2410 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002411 // | task | target enter | * |
2412 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002413 // | task | target exit | * |
2414 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002415 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002416 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002417 // | | point | ! |
2418 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002419 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002420 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002421 // | task | distribute | + |
2422 // | task | distribute | + |
2423 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002424 // | task | distribute | + |
2425 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002426 // | task | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002427 // | task | target parallel | + |
2428 // | | for simd | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002429 // +------------------+-----------------+------------------------------------+
2430 // | ordered | parallel | * |
2431 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002432 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002433 // | ordered | master | * |
2434 // | ordered | critical | * |
2435 // | ordered | simd | * |
2436 // | ordered | sections | + |
2437 // | ordered | section | + |
2438 // | ordered | single | + |
2439 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002440 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002441 // | ordered |parallel sections| * |
2442 // | ordered | task | * |
2443 // | ordered | taskyield | * |
2444 // | ordered | barrier | + |
2445 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002446 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002447 // | ordered | flush | * |
2448 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002449 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002450 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002451 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002452 // | ordered | target parallel | * |
2453 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002454 // | ordered | target enter | * |
2455 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002456 // | ordered | target exit | * |
2457 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002458 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002459 // | ordered | cancellation | |
2460 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002461 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002462 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002463 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002464 // | ordered | distribute | + |
2465 // | ordered | distribute | + |
2466 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002467 // | ordered | distribute | + |
2468 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002469 // | ordered | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002470 // | ordered | target parallel | + |
2471 // | | for simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002472 // +------------------+-----------------+------------------------------------+
2473 // | atomic | parallel | |
2474 // | atomic | for | |
2475 // | atomic | for simd | |
2476 // | atomic | master | |
2477 // | atomic | critical | |
2478 // | atomic | simd | |
2479 // | atomic | sections | |
2480 // | atomic | section | |
2481 // | atomic | single | |
2482 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002483 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002484 // | atomic |parallel sections| |
2485 // | atomic | task | |
2486 // | atomic | taskyield | |
2487 // | atomic | barrier | |
2488 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002489 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002490 // | atomic | flush | |
2491 // | atomic | ordered | |
2492 // | atomic | atomic | |
2493 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002494 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002495 // | atomic | target parallel | |
2496 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002497 // | atomic | target enter | |
2498 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002499 // | atomic | target exit | |
2500 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002501 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002502 // | atomic | cancellation | |
2503 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002504 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002505 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002506 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002507 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002508 // | atomic | distribute | |
2509 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002510 // | atomic | distribute | |
2511 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002512 // | atomic | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002513 // | atomic | target parallel | |
2514 // | | for simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002515 // +------------------+-----------------+------------------------------------+
2516 // | target | parallel | * |
2517 // | target | for | * |
2518 // | target | for simd | * |
2519 // | target | master | * |
2520 // | target | critical | * |
2521 // | target | simd | * |
2522 // | target | sections | * |
2523 // | target | section | * |
2524 // | target | single | * |
2525 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002526 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002527 // | target |parallel sections| * |
2528 // | target | task | * |
2529 // | target | taskyield | * |
2530 // | target | barrier | * |
2531 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002532 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002533 // | target | flush | * |
2534 // | target | ordered | * |
2535 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002536 // | target | target | |
2537 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002538 // | target | target parallel | |
2539 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002540 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002541 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002542 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002543 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002544 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002545 // | target | cancellation | |
2546 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002547 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002548 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002549 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002550 // | target | distribute | + |
2551 // | target | distribute | + |
2552 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002553 // | target | distribute | + |
2554 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002555 // | target | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002556 // | target | target parallel | |
2557 // | | for simd | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002558 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002559 // | target parallel | parallel | * |
2560 // | target parallel | for | * |
2561 // | target parallel | for simd | * |
2562 // | target parallel | master | * |
2563 // | target parallel | critical | * |
2564 // | target parallel | simd | * |
2565 // | target parallel | sections | * |
2566 // | target parallel | section | * |
2567 // | target parallel | single | * |
2568 // | target parallel | parallel for | * |
2569 // | target parallel |parallel for simd| * |
2570 // | target parallel |parallel sections| * |
2571 // | target parallel | task | * |
2572 // | target parallel | taskyield | * |
2573 // | target parallel | barrier | * |
2574 // | target parallel | taskwait | * |
2575 // | target parallel | taskgroup | * |
2576 // | target parallel | flush | * |
2577 // | target parallel | ordered | * |
2578 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002579 // | target parallel | target | |
2580 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002581 // | target parallel | target parallel | |
2582 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002583 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002584 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002585 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002586 // | | data | |
2587 // | target parallel | teams | |
2588 // | target parallel | cancellation | |
2589 // | | point | ! |
2590 // | target parallel | cancel | ! |
2591 // | target parallel | taskloop | * |
2592 // | target parallel | taskloop simd | * |
2593 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002594 // | target parallel | distribute | |
2595 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002596 // | target parallel | distribute | |
2597 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002598 // | target parallel | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002599 // | target parallel | target parallel | |
2600 // | | for simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002601 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002602 // | target parallel | parallel | * |
2603 // | for | | |
2604 // | target parallel | for | * |
2605 // | for | | |
2606 // | target parallel | for simd | * |
2607 // | for | | |
2608 // | target parallel | master | * |
2609 // | for | | |
2610 // | target parallel | critical | * |
2611 // | for | | |
2612 // | target parallel | simd | * |
2613 // | for | | |
2614 // | target parallel | sections | * |
2615 // | for | | |
2616 // | target parallel | section | * |
2617 // | for | | |
2618 // | target parallel | single | * |
2619 // | for | | |
2620 // | target parallel | parallel for | * |
2621 // | for | | |
2622 // | target parallel |parallel for simd| * |
2623 // | for | | |
2624 // | target parallel |parallel sections| * |
2625 // | for | | |
2626 // | target parallel | task | * |
2627 // | for | | |
2628 // | target parallel | taskyield | * |
2629 // | for | | |
2630 // | target parallel | barrier | * |
2631 // | for | | |
2632 // | target parallel | taskwait | * |
2633 // | for | | |
2634 // | target parallel | taskgroup | * |
2635 // | for | | |
2636 // | target parallel | flush | * |
2637 // | for | | |
2638 // | target parallel | ordered | * |
2639 // | for | | |
2640 // | target parallel | atomic | * |
2641 // | for | | |
2642 // | target parallel | target | |
2643 // | for | | |
2644 // | target parallel | target parallel | |
2645 // | for | | |
2646 // | target parallel | target parallel | |
2647 // | for | for | |
2648 // | target parallel | target enter | |
2649 // | for | data | |
2650 // | target parallel | target exit | |
2651 // | for | data | |
2652 // | target parallel | teams | |
2653 // | for | | |
2654 // | target parallel | cancellation | |
2655 // | for | point | ! |
2656 // | target parallel | cancel | ! |
2657 // | for | | |
2658 // | target parallel | taskloop | * |
2659 // | for | | |
2660 // | target parallel | taskloop simd | * |
2661 // | for | | |
2662 // | target parallel | distribute | |
2663 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002664 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002665 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002666 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002667 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002668 // | target parallel | distribute simd | |
2669 // | for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002670 // | target parallel | target parallel | |
2671 // | for | for simd | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002672 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002673 // | teams | parallel | * |
2674 // | teams | for | + |
2675 // | teams | for simd | + |
2676 // | teams | master | + |
2677 // | teams | critical | + |
2678 // | teams | simd | + |
2679 // | teams | sections | + |
2680 // | teams | section | + |
2681 // | teams | single | + |
2682 // | teams | parallel for | * |
2683 // | teams |parallel for simd| * |
2684 // | teams |parallel sections| * |
2685 // | teams | task | + |
2686 // | teams | taskyield | + |
2687 // | teams | barrier | + |
2688 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002689 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002690 // | teams | flush | + |
2691 // | teams | ordered | + |
2692 // | teams | atomic | + |
2693 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002694 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002695 // | teams | target parallel | + |
2696 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002697 // | teams | target enter | + |
2698 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002699 // | teams | target exit | + |
2700 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002701 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002702 // | teams | cancellation | |
2703 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002704 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002705 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002706 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002707 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002708 // | teams | distribute | ! |
2709 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002710 // | teams | distribute | ! |
2711 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002712 // | teams | distribute simd | ! |
Kelvin Lia579b912016-07-14 02:54:56 +00002713 // | teams | target parallel | + |
2714 // | | for simd | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002715 // +------------------+-----------------+------------------------------------+
2716 // | taskloop | parallel | * |
2717 // | taskloop | for | + |
2718 // | taskloop | for simd | + |
2719 // | taskloop | master | + |
2720 // | taskloop | critical | * |
2721 // | taskloop | simd | * |
2722 // | taskloop | sections | + |
2723 // | taskloop | section | + |
2724 // | taskloop | single | + |
2725 // | taskloop | parallel for | * |
2726 // | taskloop |parallel for simd| * |
2727 // | taskloop |parallel sections| * |
2728 // | taskloop | task | * |
2729 // | taskloop | taskyield | * |
2730 // | taskloop | barrier | + |
2731 // | taskloop | taskwait | * |
2732 // | taskloop | taskgroup | * |
2733 // | taskloop | flush | * |
2734 // | taskloop | ordered | + |
2735 // | taskloop | atomic | * |
2736 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002737 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002738 // | taskloop | target parallel | * |
2739 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002740 // | taskloop | target enter | * |
2741 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002742 // | taskloop | target exit | * |
2743 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002744 // | taskloop | teams | + |
2745 // | taskloop | cancellation | |
2746 // | | point | |
2747 // | taskloop | cancel | |
2748 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002749 // | taskloop | distribute | + |
2750 // | taskloop | distribute | + |
2751 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002752 // | taskloop | distribute | + |
2753 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002754 // | taskloop | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002755 // | taskloop | target parallel | * |
2756 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002757 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002758 // | taskloop simd | parallel | |
2759 // | taskloop simd | for | |
2760 // | taskloop simd | for simd | |
2761 // | taskloop simd | master | |
2762 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002763 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002764 // | taskloop simd | sections | |
2765 // | taskloop simd | section | |
2766 // | taskloop simd | single | |
2767 // | taskloop simd | parallel for | |
2768 // | taskloop simd |parallel for simd| |
2769 // | taskloop simd |parallel sections| |
2770 // | taskloop simd | task | |
2771 // | taskloop simd | taskyield | |
2772 // | taskloop simd | barrier | |
2773 // | taskloop simd | taskwait | |
2774 // | taskloop simd | taskgroup | |
2775 // | taskloop simd | flush | |
2776 // | taskloop simd | ordered | + (with simd clause) |
2777 // | taskloop simd | atomic | |
2778 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002779 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002780 // | taskloop simd | target parallel | |
2781 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002782 // | taskloop simd | target enter | |
2783 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002784 // | taskloop simd | target exit | |
2785 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002786 // | taskloop simd | teams | |
2787 // | taskloop simd | cancellation | |
2788 // | | point | |
2789 // | taskloop simd | cancel | |
2790 // | taskloop simd | taskloop | |
2791 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002792 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002793 // | taskloop simd | distribute | |
2794 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002795 // | taskloop simd | distribute | |
2796 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002797 // | taskloop simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002798 // | taskloop simd | target parallel | |
2799 // | | for simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002800 // +------------------+-----------------+------------------------------------+
2801 // | distribute | parallel | * |
2802 // | distribute | for | * |
2803 // | distribute | for simd | * |
2804 // | distribute | master | * |
2805 // | distribute | critical | * |
2806 // | distribute | simd | * |
2807 // | distribute | sections | * |
2808 // | distribute | section | * |
2809 // | distribute | single | * |
2810 // | distribute | parallel for | * |
2811 // | distribute |parallel for simd| * |
2812 // | distribute |parallel sections| * |
2813 // | distribute | task | * |
2814 // | distribute | taskyield | * |
2815 // | distribute | barrier | * |
2816 // | distribute | taskwait | * |
2817 // | distribute | taskgroup | * |
2818 // | distribute | flush | * |
2819 // | distribute | ordered | + |
2820 // | distribute | atomic | * |
2821 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002822 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002823 // | distribute | target parallel | |
2824 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002825 // | distribute | target enter | |
2826 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002827 // | distribute | target exit | |
2828 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002829 // | distribute | teams | |
2830 // | distribute | cancellation | + |
2831 // | | point | |
2832 // | distribute | cancel | + |
2833 // | distribute | taskloop | * |
2834 // | distribute | taskloop simd | * |
2835 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002836 // | distribute | distribute | |
2837 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002838 // | distribute | distribute | |
2839 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002840 // | distribute | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002841 // | distribute | target parallel | |
2842 // | | for simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002843 // +------------------+-----------------+------------------------------------+
2844 // | distribute | parallel | * |
2845 // | parallel for | | |
2846 // | distribute | for | * |
2847 // | parallel for | | |
2848 // | distribute | for simd | * |
2849 // | parallel for | | |
2850 // | distribute | master | * |
2851 // | parallel for | | |
2852 // | distribute | critical | * |
2853 // | parallel for | | |
2854 // | distribute | simd | * |
2855 // | parallel for | | |
2856 // | distribute | sections | * |
2857 // | parallel for | | |
2858 // | distribute | section | * |
2859 // | parallel for | | |
2860 // | distribute | single | * |
2861 // | parallel for | | |
2862 // | distribute | parallel for | * |
2863 // | parallel for | | |
2864 // | distribute |parallel for simd| * |
2865 // | parallel for | | |
2866 // | distribute |parallel sections| * |
2867 // | parallel for | | |
2868 // | distribute | task | * |
2869 // | parallel for | | |
2870 // | parallel for | | |
2871 // | distribute | taskyield | * |
2872 // | parallel for | | |
2873 // | distribute | barrier | * |
2874 // | parallel for | | |
2875 // | distribute | taskwait | * |
2876 // | parallel for | | |
2877 // | distribute | taskgroup | * |
2878 // | parallel for | | |
2879 // | distribute | flush | * |
2880 // | parallel for | | |
2881 // | distribute | ordered | + |
2882 // | parallel for | | |
2883 // | distribute | atomic | * |
2884 // | parallel for | | |
2885 // | distribute | target | |
2886 // | parallel for | | |
2887 // | distribute | target parallel | |
2888 // | parallel for | | |
2889 // | distribute | target parallel | |
2890 // | parallel for | for | |
2891 // | distribute | target enter | |
2892 // | parallel for | data | |
2893 // | distribute | target exit | |
2894 // | parallel for | data | |
2895 // | distribute | teams | |
2896 // | parallel for | | |
2897 // | distribute | cancellation | + |
2898 // | parallel for | point | |
2899 // | distribute | cancel | + |
2900 // | parallel for | | |
2901 // | distribute | taskloop | * |
2902 // | parallel for | | |
2903 // | distribute | taskloop simd | * |
2904 // | parallel for | | |
2905 // | distribute | distribute | |
2906 // | parallel for | | |
2907 // | distribute | distribute | |
2908 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002909 // | distribute | distribute | |
2910 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002911 // | distribute | distribute simd | |
2912 // | parallel for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002913 // | distribute | target parallel | |
2914 // | parallel for | for simd | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002915 // +------------------+-----------------+------------------------------------+
2916 // | distribute | parallel | * |
2917 // | parallel for simd| | |
2918 // | distribute | for | * |
2919 // | parallel for simd| | |
2920 // | distribute | for simd | * |
2921 // | parallel for simd| | |
2922 // | distribute | master | * |
2923 // | parallel for simd| | |
2924 // | distribute | critical | * |
2925 // | parallel for simd| | |
2926 // | distribute | simd | * |
2927 // | parallel for simd| | |
2928 // | distribute | sections | * |
2929 // | parallel for simd| | |
2930 // | distribute | section | * |
2931 // | parallel for simd| | |
2932 // | distribute | single | * |
2933 // | parallel for simd| | |
2934 // | distribute | parallel for | * |
2935 // | parallel for simd| | |
2936 // | distribute |parallel for simd| * |
2937 // | parallel for simd| | |
2938 // | distribute |parallel sections| * |
2939 // | parallel for simd| | |
2940 // | distribute | task | * |
2941 // | parallel for simd| | |
2942 // | distribute | taskyield | * |
2943 // | parallel for simd| | |
2944 // | distribute | barrier | * |
2945 // | parallel for simd| | |
2946 // | distribute | taskwait | * |
2947 // | parallel for simd| | |
2948 // | distribute | taskgroup | * |
2949 // | parallel for simd| | |
2950 // | distribute | flush | * |
2951 // | parallel for simd| | |
2952 // | distribute | ordered | + |
2953 // | parallel for simd| | |
2954 // | distribute | atomic | * |
2955 // | parallel for simd| | |
2956 // | distribute | target | |
2957 // | parallel for simd| | |
2958 // | distribute | target parallel | |
2959 // | parallel for simd| | |
2960 // | distribute | target parallel | |
2961 // | parallel for simd| for | |
2962 // | distribute | target enter | |
2963 // | parallel for simd| data | |
2964 // | distribute | target exit | |
2965 // | parallel for simd| data | |
2966 // | distribute | teams | |
2967 // | parallel for simd| | |
2968 // | distribute | cancellation | + |
2969 // | parallel for simd| point | |
2970 // | distribute | cancel | + |
2971 // | parallel for simd| | |
2972 // | distribute | taskloop | * |
2973 // | parallel for simd| | |
2974 // | distribute | taskloop simd | * |
2975 // | parallel for simd| | |
2976 // | distribute | distribute | |
2977 // | parallel for simd| | |
2978 // | distribute | distribute | * |
2979 // | parallel for simd| parallel for | |
2980 // | distribute | distribute | * |
2981 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002982 // | distribute | distribute simd | * |
2983 // | parallel for simd| | |
Kelvin Lia579b912016-07-14 02:54:56 +00002984 // | distribute | target parallel | |
2985 // | parallel for simd| for simd | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002986 // +------------------+-----------------+------------------------------------+
2987 // | distribute simd | parallel | * |
2988 // | distribute simd | for | * |
2989 // | distribute simd | for simd | * |
2990 // | distribute simd | master | * |
2991 // | distribute simd | critical | * |
2992 // | distribute simd | simd | * |
2993 // | distribute simd | sections | * |
2994 // | distribute simd | section | * |
2995 // | distribute simd | single | * |
2996 // | distribute simd | parallel for | * |
2997 // | distribute simd |parallel for simd| * |
2998 // | distribute simd |parallel sections| * |
2999 // | distribute simd | task | * |
3000 // | distribute simd | taskyield | * |
3001 // | distribute simd | barrier | * |
3002 // | distribute simd | taskwait | * |
3003 // | distribute simd | taskgroup | * |
3004 // | distribute simd | flush | * |
3005 // | distribute simd | ordered | + |
3006 // | distribute simd | atomic | * |
3007 // | distribute simd | target | * |
3008 // | distribute simd | target parallel | * |
3009 // | distribute simd | target parallel | * |
3010 // | | for | |
3011 // | distribute simd | target enter | * |
3012 // | | data | |
3013 // | distribute simd | target exit | * |
3014 // | | data | |
3015 // | distribute simd | teams | * |
3016 // | distribute simd | cancellation | + |
3017 // | | point | |
3018 // | distribute simd | cancel | + |
3019 // | distribute simd | taskloop | * |
3020 // | distribute simd | taskloop simd | * |
3021 // | distribute simd | distribute | |
3022 // | distribute simd | distribute | * |
3023 // | | parallel for | |
3024 // | distribute simd | distribute | * |
3025 // | |parallel for simd| |
3026 // | distribute simd | distribute simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003027 // | distribute simd | target parallel | * |
3028 // | | for simd | |
3029 // +------------------+-----------------+------------------------------------+
3030 // | target parallel | parallel | * |
3031 // | for simd | | |
3032 // | target parallel | for | * |
3033 // | for simd | | |
3034 // | target parallel | for simd | * |
3035 // | for simd | | |
3036 // | target parallel | master | * |
3037 // | for simd | | |
3038 // | target parallel | critical | * |
3039 // | for simd | | |
3040 // | target parallel | simd | ! |
3041 // | for simd | | |
3042 // | target parallel | sections | * |
3043 // | for simd | | |
3044 // | target parallel | section | * |
3045 // | for simd | | |
3046 // | target parallel | single | * |
3047 // | for simd | | |
3048 // | target parallel | parallel for | * |
3049 // | for simd | | |
3050 // | target parallel |parallel for simd| * |
3051 // | for simd | | |
3052 // | target parallel |parallel sections| * |
3053 // | for simd | | |
3054 // | target parallel | task | * |
3055 // | for simd | | |
3056 // | target parallel | taskyield | * |
3057 // | for simd | | |
3058 // | target parallel | barrier | * |
3059 // | for simd | | |
3060 // | target parallel | taskwait | * |
3061 // | for simd | | |
3062 // | target parallel | taskgroup | * |
3063 // | for simd | | |
3064 // | target parallel | flush | * |
3065 // | for simd | | |
3066 // | target parallel | ordered | + (with simd clause) |
3067 // | for simd | | |
3068 // | target parallel | atomic | * |
3069 // | for simd | | |
3070 // | target parallel | target | * |
3071 // | for simd | | |
3072 // | target parallel | target parallel | * |
3073 // | for simd | | |
3074 // | target parallel | target parallel | * |
3075 // | for simd | for | |
3076 // | target parallel | target enter | * |
3077 // | for simd | data | |
3078 // | target parallel | target exit | * |
3079 // | for simd | data | |
3080 // | target parallel | teams | * |
3081 // | for simd | | |
3082 // | target parallel | cancellation | * |
3083 // | for simd | point | |
3084 // | target parallel | cancel | * |
3085 // | for simd | | |
3086 // | target parallel | taskloop | * |
3087 // | for simd | | |
3088 // | target parallel | taskloop simd | * |
3089 // | for simd | | |
3090 // | target parallel | distribute | * |
3091 // | for simd | | |
3092 // | target parallel | distribute | * |
3093 // | for simd | parallel for | |
3094 // | target parallel | distribute | * |
3095 // | for simd |parallel for simd| |
3096 // | target parallel | distribute simd | * |
3097 // | for simd | | |
3098 // | target parallel | target parallel | * |
3099 // | for simd | for simd | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003100 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003101 if (Stack->getCurScope()) {
3102 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003103 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003104 bool NestingProhibited = false;
3105 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003106 enum {
3107 NoRecommend,
3108 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003109 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003110 ShouldBeInTargetRegion,
3111 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003112 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003113 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003114 // OpenMP [2.16, Nesting of Regions]
3115 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003116 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003117 // An ordered construct with the simd clause is the only OpenMP
3118 // construct that can appear in the simd region.
3119 // Allowing a SIMD consruct nested in another SIMD construct is an
3120 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3121 // message.
3122 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3123 ? diag::err_omp_prohibited_region_simd
3124 : diag::warn_omp_nesting_simd);
3125 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003126 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003127 if (ParentRegion == OMPD_atomic) {
3128 // OpenMP [2.16, Nesting of Regions]
3129 // OpenMP constructs may not be nested inside an atomic region.
3130 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3131 return true;
3132 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003133 if (CurrentRegion == OMPD_section) {
3134 // OpenMP [2.7.2, sections Construct, Restrictions]
3135 // Orphaned section directives are prohibited. That is, the section
3136 // directives must appear within the sections construct and must not be
3137 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003138 if (ParentRegion != OMPD_sections &&
3139 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003140 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3141 << (ParentRegion != OMPD_unknown)
3142 << getOpenMPDirectiveName(ParentRegion);
3143 return true;
3144 }
3145 return false;
3146 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003147 // Allow some constructs to be orphaned (they could be used in functions,
3148 // called from OpenMP regions with the required preconditions).
3149 if (ParentRegion == OMPD_unknown)
3150 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003151 if (CurrentRegion == OMPD_cancellation_point ||
3152 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003153 // OpenMP [2.16, Nesting of Regions]
3154 // A cancellation point construct for which construct-type-clause is
3155 // taskgroup must be nested inside a task construct. A cancellation
3156 // point construct for which construct-type-clause is not taskgroup must
3157 // be closely nested inside an OpenMP construct that matches the type
3158 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003159 // A cancel construct for which construct-type-clause is taskgroup must be
3160 // nested inside a task construct. A cancel construct for which
3161 // construct-type-clause is not taskgroup must be closely nested inside an
3162 // OpenMP construct that matches the type specified in
3163 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003164 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003165 !((CancelRegion == OMPD_parallel &&
3166 (ParentRegion == OMPD_parallel ||
3167 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003168 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003169 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3170 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003171 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3172 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003173 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3174 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003175 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003176 // OpenMP [2.16, Nesting of Regions]
3177 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003178 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003179 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003180 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003181 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3182 // OpenMP [2.16, Nesting of Regions]
3183 // A critical region may not be nested (closely or otherwise) inside a
3184 // critical region with the same name. Note that this restriction is not
3185 // sufficient to prevent deadlock.
3186 SourceLocation PreviousCriticalLoc;
3187 bool DeadLock =
3188 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3189 OpenMPDirectiveKind K,
3190 const DeclarationNameInfo &DNI,
3191 SourceLocation Loc)
3192 ->bool {
3193 if (K == OMPD_critical &&
3194 DNI.getName() == CurrentName.getName()) {
3195 PreviousCriticalLoc = Loc;
3196 return true;
3197 } else
3198 return false;
3199 },
3200 false /* skip top directive */);
3201 if (DeadLock) {
3202 SemaRef.Diag(StartLoc,
3203 diag::err_omp_prohibited_region_critical_same_name)
3204 << CurrentName.getName();
3205 if (PreviousCriticalLoc.isValid())
3206 SemaRef.Diag(PreviousCriticalLoc,
3207 diag::note_omp_previous_critical_region);
3208 return true;
3209 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003210 } else if (CurrentRegion == OMPD_barrier) {
3211 // OpenMP [2.16, Nesting of Regions]
3212 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003213 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003214 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3215 isOpenMPTaskingDirective(ParentRegion) ||
3216 ParentRegion == OMPD_master ||
3217 ParentRegion == OMPD_critical ||
3218 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003219 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003220 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003221 // OpenMP [2.16, Nesting of Regions]
3222 // A worksharing region may not be closely nested inside a worksharing,
3223 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003224 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3225 isOpenMPTaskingDirective(ParentRegion) ||
3226 ParentRegion == OMPD_master ||
3227 ParentRegion == OMPD_critical ||
3228 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003229 Recommend = ShouldBeInParallelRegion;
3230 } else if (CurrentRegion == OMPD_ordered) {
3231 // OpenMP [2.16, Nesting of Regions]
3232 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003233 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003234 // An ordered region must be closely nested inside a loop region (or
3235 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003236 // OpenMP [2.8.1,simd Construct, Restrictions]
3237 // An ordered construct with the simd clause is the only OpenMP construct
3238 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003239 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003240 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003241 !(isOpenMPSimdDirective(ParentRegion) ||
3242 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003243 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003244 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3245 // OpenMP [2.16, Nesting of Regions]
3246 // If specified, a teams construct must be contained within a target
3247 // construct.
3248 NestingProhibited = ParentRegion != OMPD_target;
3249 Recommend = ShouldBeInTargetRegion;
3250 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3251 }
3252 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3253 // OpenMP [2.16, Nesting of Regions]
3254 // distribute, parallel, parallel sections, parallel workshare, and the
3255 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3256 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003257 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3258 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003259 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003260 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003261 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3262 // OpenMP 4.5 [2.17 Nesting of Regions]
3263 // The region associated with the distribute construct must be strictly
3264 // nested inside a teams region
3265 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3266 Recommend = ShouldBeInTeamsRegion;
3267 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003268 if (!NestingProhibited &&
3269 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3270 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3271 // OpenMP 4.5 [2.17 Nesting of Regions]
3272 // If a target, target update, target data, target enter data, or
3273 // target exit data construct is encountered during execution of a
3274 // target region, the behavior is unspecified.
3275 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003276 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3277 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003278 if (isOpenMPTargetExecutionDirective(K)) {
3279 OffendingRegion = K;
3280 return true;
3281 } else
3282 return false;
3283 },
3284 false /* don't skip top directive */);
3285 CloseNesting = false;
3286 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003287 if (NestingProhibited) {
3288 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003289 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3290 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00003291 return true;
3292 }
3293 }
3294 return false;
3295}
3296
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003297static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3298 ArrayRef<OMPClause *> Clauses,
3299 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3300 bool ErrorFound = false;
3301 unsigned NamedModifiersNumber = 0;
3302 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3303 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003304 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003305 for (const auto *C : Clauses) {
3306 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3307 // At most one if clause without a directive-name-modifier can appear on
3308 // the directive.
3309 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3310 if (FoundNameModifiers[CurNM]) {
3311 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3312 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3313 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3314 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003315 } else if (CurNM != OMPD_unknown) {
3316 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003317 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003318 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003319 FoundNameModifiers[CurNM] = IC;
3320 if (CurNM == OMPD_unknown)
3321 continue;
3322 // Check if the specified name modifier is allowed for the current
3323 // directive.
3324 // At most one if clause with the particular directive-name-modifier can
3325 // appear on the directive.
3326 bool MatchFound = false;
3327 for (auto NM : AllowedNameModifiers) {
3328 if (CurNM == NM) {
3329 MatchFound = true;
3330 break;
3331 }
3332 }
3333 if (!MatchFound) {
3334 S.Diag(IC->getNameModifierLoc(),
3335 diag::err_omp_wrong_if_directive_name_modifier)
3336 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3337 ErrorFound = true;
3338 }
3339 }
3340 }
3341 // If any if clause on the directive includes a directive-name-modifier then
3342 // all if clauses on the directive must include a directive-name-modifier.
3343 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3344 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3345 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3346 diag::err_omp_no_more_if_clause);
3347 } else {
3348 std::string Values;
3349 std::string Sep(", ");
3350 unsigned AllowedCnt = 0;
3351 unsigned TotalAllowedNum =
3352 AllowedNameModifiers.size() - NamedModifiersNumber;
3353 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3354 ++Cnt) {
3355 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3356 if (!FoundNameModifiers[NM]) {
3357 Values += "'";
3358 Values += getOpenMPDirectiveName(NM);
3359 Values += "'";
3360 if (AllowedCnt + 2 == TotalAllowedNum)
3361 Values += " or ";
3362 else if (AllowedCnt + 1 != TotalAllowedNum)
3363 Values += Sep;
3364 ++AllowedCnt;
3365 }
3366 }
3367 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3368 diag::err_omp_unnamed_if_clause)
3369 << (TotalAllowedNum > 1) << Values;
3370 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003371 for (auto Loc : NameModifierLoc) {
3372 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3373 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003374 ErrorFound = true;
3375 }
3376 return ErrorFound;
3377}
3378
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003379StmtResult Sema::ActOnOpenMPExecutableDirective(
3380 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3381 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3382 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003383 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003384 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3385 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003386 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003387
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003388 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003389 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003390 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003391 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003392 if (AStmt) {
3393 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3394
3395 // Check default data sharing attributes for referenced variables.
3396 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3397 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3398 if (DSAChecker.isErrorFound())
3399 return StmtError();
3400 // Generate list of implicitly defined firstprivate variables.
3401 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003402
3403 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3404 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3405 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3406 SourceLocation(), SourceLocation())) {
3407 ClausesWithImplicit.push_back(Implicit);
3408 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3409 DSAChecker.getImplicitFirstprivate().size();
3410 } else
3411 ErrorFound = true;
3412 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003413 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003414
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003415 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003416 switch (Kind) {
3417 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003418 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3419 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003420 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003421 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003422 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003423 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3424 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003425 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003426 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003427 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3428 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003429 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003430 case OMPD_for_simd:
3431 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3432 EndLoc, VarsWithInheritedDSA);
3433 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003434 case OMPD_sections:
3435 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3436 EndLoc);
3437 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003438 case OMPD_section:
3439 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003440 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003441 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3442 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003443 case OMPD_single:
3444 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3445 EndLoc);
3446 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003447 case OMPD_master:
3448 assert(ClausesWithImplicit.empty() &&
3449 "No clauses are allowed for 'omp master' directive");
3450 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3451 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003452 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003453 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3454 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003455 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003456 case OMPD_parallel_for:
3457 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3458 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003459 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003460 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003461 case OMPD_parallel_for_simd:
3462 Res = ActOnOpenMPParallelForSimdDirective(
3463 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003464 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003465 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003466 case OMPD_parallel_sections:
3467 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3468 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003469 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003470 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003471 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003472 Res =
3473 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003474 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003475 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003476 case OMPD_taskyield:
3477 assert(ClausesWithImplicit.empty() &&
3478 "No clauses are allowed for 'omp taskyield' directive");
3479 assert(AStmt == nullptr &&
3480 "No associated statement allowed for 'omp taskyield' directive");
3481 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3482 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003483 case OMPD_barrier:
3484 assert(ClausesWithImplicit.empty() &&
3485 "No clauses are allowed for 'omp barrier' directive");
3486 assert(AStmt == nullptr &&
3487 "No associated statement allowed for 'omp barrier' directive");
3488 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3489 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003490 case OMPD_taskwait:
3491 assert(ClausesWithImplicit.empty() &&
3492 "No clauses are allowed for 'omp taskwait' directive");
3493 assert(AStmt == nullptr &&
3494 "No associated statement allowed for 'omp taskwait' directive");
3495 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3496 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003497 case OMPD_taskgroup:
3498 assert(ClausesWithImplicit.empty() &&
3499 "No clauses are allowed for 'omp taskgroup' directive");
3500 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3501 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003502 case OMPD_flush:
3503 assert(AStmt == nullptr &&
3504 "No associated statement allowed for 'omp flush' directive");
3505 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3506 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003507 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003508 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3509 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003510 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003511 case OMPD_atomic:
3512 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3513 EndLoc);
3514 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003515 case OMPD_teams:
3516 Res =
3517 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3518 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003519 case OMPD_target:
3520 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3521 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003522 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003523 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003524 case OMPD_target_parallel:
3525 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3526 StartLoc, EndLoc);
3527 AllowedNameModifiers.push_back(OMPD_target);
3528 AllowedNameModifiers.push_back(OMPD_parallel);
3529 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003530 case OMPD_target_parallel_for:
3531 Res = ActOnOpenMPTargetParallelForDirective(
3532 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3533 AllowedNameModifiers.push_back(OMPD_target);
3534 AllowedNameModifiers.push_back(OMPD_parallel);
3535 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003536 case OMPD_cancellation_point:
3537 assert(ClausesWithImplicit.empty() &&
3538 "No clauses are allowed for 'omp cancellation point' directive");
3539 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3540 "cancellation point' directive");
3541 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3542 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003543 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003544 assert(AStmt == nullptr &&
3545 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003546 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3547 CancelRegion);
3548 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003549 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003550 case OMPD_target_data:
3551 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3552 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003553 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003554 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003555 case OMPD_target_enter_data:
3556 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3557 EndLoc);
3558 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3559 break;
Samuel Antao72590762016-01-19 20:04:50 +00003560 case OMPD_target_exit_data:
3561 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3562 EndLoc);
3563 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3564 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003565 case OMPD_taskloop:
3566 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3567 EndLoc, VarsWithInheritedDSA);
3568 AllowedNameModifiers.push_back(OMPD_taskloop);
3569 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003570 case OMPD_taskloop_simd:
3571 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3572 EndLoc, VarsWithInheritedDSA);
3573 AllowedNameModifiers.push_back(OMPD_taskloop);
3574 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003575 case OMPD_distribute:
3576 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3577 EndLoc, VarsWithInheritedDSA);
3578 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003579 case OMPD_target_update:
3580 assert(!AStmt && "Statement is not allowed for target update");
3581 Res =
3582 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3583 AllowedNameModifiers.push_back(OMPD_target_update);
3584 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003585 case OMPD_distribute_parallel_for:
3586 Res = ActOnOpenMPDistributeParallelForDirective(
3587 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3588 AllowedNameModifiers.push_back(OMPD_parallel);
3589 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003590 case OMPD_distribute_parallel_for_simd:
3591 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3592 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3593 AllowedNameModifiers.push_back(OMPD_parallel);
3594 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003595 case OMPD_distribute_simd:
3596 Res = ActOnOpenMPDistributeSimdDirective(
3597 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3598 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003599 case OMPD_target_parallel_for_simd:
3600 Res = ActOnOpenMPTargetParallelForSimdDirective(
3601 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3602 AllowedNameModifiers.push_back(OMPD_target);
3603 AllowedNameModifiers.push_back(OMPD_parallel);
3604 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003605 case OMPD_declare_target:
3606 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003607 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003608 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003609 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003610 llvm_unreachable("OpenMP Directive is not allowed");
3611 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003612 llvm_unreachable("Unknown OpenMP directive");
3613 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003614
Alexey Bataev4acb8592014-07-07 13:01:15 +00003615 for (auto P : VarsWithInheritedDSA) {
3616 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3617 << P.first << P.second->getSourceRange();
3618 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003619 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3620
3621 if (!AllowedNameModifiers.empty())
3622 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3623 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003624
Alexey Bataeved09d242014-05-28 05:53:51 +00003625 if (ErrorFound)
3626 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003627 return Res;
3628}
3629
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003630Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3631 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003632 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003633 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3634 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003635 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003636 assert(Linears.size() == LinModifiers.size());
3637 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003638 if (!DG || DG.get().isNull())
3639 return DeclGroupPtrTy();
3640
3641 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003642 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003643 return DG;
3644 }
3645 auto *ADecl = DG.get().getSingleDecl();
3646 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3647 ADecl = FTD->getTemplatedDecl();
3648
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003649 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3650 if (!FD) {
3651 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003652 return DeclGroupPtrTy();
3653 }
3654
Alexey Bataev2af33e32016-04-07 12:45:37 +00003655 // OpenMP [2.8.2, declare simd construct, Description]
3656 // The parameter of the simdlen clause must be a constant positive integer
3657 // expression.
3658 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003659 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003660 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003661 // OpenMP [2.8.2, declare simd construct, Description]
3662 // The special this pointer can be used as if was one of the arguments to the
3663 // function in any of the linear, aligned, or uniform clauses.
3664 // The uniform clause declares one or more arguments to have an invariant
3665 // value for all concurrent invocations of the function in the execution of a
3666 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003667 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3668 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003669 for (auto *E : Uniforms) {
3670 E = E->IgnoreParenImpCasts();
3671 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3672 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3673 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3674 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003675 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3676 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003677 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003678 }
3679 if (isa<CXXThisExpr>(E)) {
3680 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003681 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003682 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003683 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3684 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003685 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003686 // OpenMP [2.8.2, declare simd construct, Description]
3687 // The aligned clause declares that the object to which each list item points
3688 // is aligned to the number of bytes expressed in the optional parameter of
3689 // the aligned clause.
3690 // The special this pointer can be used as if was one of the arguments to the
3691 // function in any of the linear, aligned, or uniform clauses.
3692 // The type of list items appearing in the aligned clause must be array,
3693 // pointer, reference to array, or reference to pointer.
3694 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3695 Expr *AlignedThis = nullptr;
3696 for (auto *E : Aligneds) {
3697 E = E->IgnoreParenImpCasts();
3698 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3699 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3700 auto *CanonPVD = PVD->getCanonicalDecl();
3701 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3702 FD->getParamDecl(PVD->getFunctionScopeIndex())
3703 ->getCanonicalDecl() == CanonPVD) {
3704 // OpenMP [2.8.1, simd construct, Restrictions]
3705 // A list-item cannot appear in more than one aligned clause.
3706 if (AlignedArgs.count(CanonPVD) > 0) {
3707 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3708 << 1 << E->getSourceRange();
3709 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3710 diag::note_omp_explicit_dsa)
3711 << getOpenMPClauseName(OMPC_aligned);
3712 continue;
3713 }
3714 AlignedArgs[CanonPVD] = E;
3715 QualType QTy = PVD->getType()
3716 .getNonReferenceType()
3717 .getUnqualifiedType()
3718 .getCanonicalType();
3719 const Type *Ty = QTy.getTypePtrOrNull();
3720 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3721 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3722 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3723 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3724 }
3725 continue;
3726 }
3727 }
3728 if (isa<CXXThisExpr>(E)) {
3729 if (AlignedThis) {
3730 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3731 << 2 << E->getSourceRange();
3732 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3733 << getOpenMPClauseName(OMPC_aligned);
3734 }
3735 AlignedThis = E;
3736 continue;
3737 }
3738 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3739 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3740 }
3741 // The optional parameter of the aligned clause, alignment, must be a constant
3742 // positive integer expression. If no optional parameter is specified,
3743 // implementation-defined default alignments for SIMD instructions on the
3744 // target platforms are assumed.
3745 SmallVector<Expr *, 4> NewAligns;
3746 for (auto *E : Alignments) {
3747 ExprResult Align;
3748 if (E)
3749 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3750 NewAligns.push_back(Align.get());
3751 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003752 // OpenMP [2.8.2, declare simd construct, Description]
3753 // The linear clause declares one or more list items to be private to a SIMD
3754 // lane and to have a linear relationship with respect to the iteration space
3755 // of a loop.
3756 // The special this pointer can be used as if was one of the arguments to the
3757 // function in any of the linear, aligned, or uniform clauses.
3758 // When a linear-step expression is specified in a linear clause it must be
3759 // either a constant integer expression or an integer-typed parameter that is
3760 // specified in a uniform clause on the directive.
3761 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3762 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3763 auto MI = LinModifiers.begin();
3764 for (auto *E : Linears) {
3765 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3766 ++MI;
3767 E = E->IgnoreParenImpCasts();
3768 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3769 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3770 auto *CanonPVD = PVD->getCanonicalDecl();
3771 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3772 FD->getParamDecl(PVD->getFunctionScopeIndex())
3773 ->getCanonicalDecl() == CanonPVD) {
3774 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3775 // A list-item cannot appear in more than one linear clause.
3776 if (LinearArgs.count(CanonPVD) > 0) {
3777 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3778 << getOpenMPClauseName(OMPC_linear)
3779 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3780 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3781 diag::note_omp_explicit_dsa)
3782 << getOpenMPClauseName(OMPC_linear);
3783 continue;
3784 }
3785 // Each argument can appear in at most one uniform or linear clause.
3786 if (UniformedArgs.count(CanonPVD) > 0) {
3787 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3788 << getOpenMPClauseName(OMPC_linear)
3789 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3790 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3791 diag::note_omp_explicit_dsa)
3792 << getOpenMPClauseName(OMPC_uniform);
3793 continue;
3794 }
3795 LinearArgs[CanonPVD] = E;
3796 if (E->isValueDependent() || E->isTypeDependent() ||
3797 E->isInstantiationDependent() ||
3798 E->containsUnexpandedParameterPack())
3799 continue;
3800 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3801 PVD->getOriginalType());
3802 continue;
3803 }
3804 }
3805 if (isa<CXXThisExpr>(E)) {
3806 if (UniformedLinearThis) {
3807 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3808 << getOpenMPClauseName(OMPC_linear)
3809 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3810 << E->getSourceRange();
3811 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3812 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3813 : OMPC_linear);
3814 continue;
3815 }
3816 UniformedLinearThis = E;
3817 if (E->isValueDependent() || E->isTypeDependent() ||
3818 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3819 continue;
3820 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3821 E->getType());
3822 continue;
3823 }
3824 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3825 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3826 }
3827 Expr *Step = nullptr;
3828 Expr *NewStep = nullptr;
3829 SmallVector<Expr *, 4> NewSteps;
3830 for (auto *E : Steps) {
3831 // Skip the same step expression, it was checked already.
3832 if (Step == E || !E) {
3833 NewSteps.push_back(E ? NewStep : nullptr);
3834 continue;
3835 }
3836 Step = E;
3837 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3838 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3839 auto *CanonPVD = PVD->getCanonicalDecl();
3840 if (UniformedArgs.count(CanonPVD) == 0) {
3841 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3842 << Step->getSourceRange();
3843 } else if (E->isValueDependent() || E->isTypeDependent() ||
3844 E->isInstantiationDependent() ||
3845 E->containsUnexpandedParameterPack() ||
3846 CanonPVD->getType()->hasIntegerRepresentation())
3847 NewSteps.push_back(Step);
3848 else {
3849 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3850 << Step->getSourceRange();
3851 }
3852 continue;
3853 }
3854 NewStep = Step;
3855 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3856 !Step->isInstantiationDependent() &&
3857 !Step->containsUnexpandedParameterPack()) {
3858 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3859 .get();
3860 if (NewStep)
3861 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3862 }
3863 NewSteps.push_back(NewStep);
3864 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003865 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3866 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003867 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003868 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3869 const_cast<Expr **>(Linears.data()), Linears.size(),
3870 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3871 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003872 ADecl->addAttr(NewAttr);
3873 return ConvertDeclToDeclGroup(ADecl);
3874}
3875
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003876StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3877 Stmt *AStmt,
3878 SourceLocation StartLoc,
3879 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003880 if (!AStmt)
3881 return StmtError();
3882
Alexey Bataev9959db52014-05-06 10:08:46 +00003883 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3884 // 1.2.2 OpenMP Language Terminology
3885 // Structured block - An executable statement with a single entry at the
3886 // top and a single exit at the bottom.
3887 // The point of exit cannot be a branch out of the structured block.
3888 // longjmp() and throw() must not violate the entry/exit criteria.
3889 CS->getCapturedDecl()->setNothrow();
3890
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003891 getCurFunction()->setHasBranchProtectedScope();
3892
Alexey Bataev25e5b442015-09-15 12:52:43 +00003893 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3894 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003895}
3896
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003897namespace {
3898/// \brief Helper class for checking canonical form of the OpenMP loops and
3899/// extracting iteration space of each loop in the loop nest, that will be used
3900/// for IR generation.
3901class OpenMPIterationSpaceChecker {
3902 /// \brief Reference to Sema.
3903 Sema &SemaRef;
3904 /// \brief A location for diagnostics (when there is no some better location).
3905 SourceLocation DefaultLoc;
3906 /// \brief A location for diagnostics (when increment is not compatible).
3907 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003908 /// \brief A source location for referring to loop init later.
3909 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003910 /// \brief A source location for referring to condition later.
3911 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003912 /// \brief A source location for referring to increment later.
3913 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003914 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003915 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003916 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003917 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003919 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003920 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003921 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003922 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003923 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003924 /// \brief This flag is true when condition is one of:
3925 /// Var < UB
3926 /// Var <= UB
3927 /// UB > Var
3928 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003929 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003931 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003932 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003933 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003934
3935public:
3936 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003937 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 /// \brief Check init-expr for canonical loop form and save loop counter
3939 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003940 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003941 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3942 /// for less/greater and for strict/non-strict comparison.
3943 bool CheckCond(Expr *S);
3944 /// \brief Check incr-expr for canonical loop form and return true if it
3945 /// does not conform, otherwise save loop step (#Step).
3946 bool CheckInc(Expr *S);
3947 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003948 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003949 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003950 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003951 /// \brief Source range of the loop init.
3952 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3953 /// \brief Source range of the loop condition.
3954 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3955 /// \brief Source range of the loop increment.
3956 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3957 /// \brief True if the step should be subtracted.
3958 bool ShouldSubtractStep() const { return SubtractStep; }
3959 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003960 Expr *
3961 BuildNumIterations(Scope *S, const bool LimitedType,
3962 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003963 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003964 Expr *BuildPreCond(Scope *S, Expr *Cond,
3965 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003966 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003967 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3968 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003969 /// \brief Build reference expression to the private counter be used for
3970 /// codegen.
3971 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003972 /// \brief Build initization of the counter be used for codegen.
3973 Expr *BuildCounterInit() const;
3974 /// \brief Build step of the counter be used for codegen.
3975 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003976 /// \brief Return true if any expression is dependent.
3977 bool Dependent() const;
3978
3979private:
3980 /// \brief Check the right-hand side of an assignment in the increment
3981 /// expression.
3982 bool CheckIncRHS(Expr *RHS);
3983 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003984 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003985 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003986 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003987 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003988 /// \brief Helper to set loop increment.
3989 bool SetStep(Expr *NewStep, bool Subtract);
3990};
3991
3992bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003993 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003994 assert(!LB && !UB && !Step);
3995 return false;
3996 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003997 return LCDecl->getType()->isDependentType() ||
3998 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3999 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004000}
4001
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004002static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004003 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4004 E = ExprTemp->getSubExpr();
4005
4006 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4007 E = MTE->GetTemporaryExpr();
4008
4009 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4010 E = Binder->getSubExpr();
4011
4012 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4013 E = ICE->getSubExprAsWritten();
4014 return E->IgnoreParens();
4015}
4016
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004017bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4018 Expr *NewLCRefExpr,
4019 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004020 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004021 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004022 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004023 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004024 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004025 LCDecl = getCanonicalDecl(NewLCDecl);
4026 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004027 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4028 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004029 if ((Ctor->isCopyOrMoveConstructor() ||
4030 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4031 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004032 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004033 LB = NewLB;
4034 return false;
4035}
4036
4037bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004038 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004039 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004040 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4041 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004042 if (!NewUB)
4043 return true;
4044 UB = NewUB;
4045 TestIsLessOp = LessOp;
4046 TestIsStrictOp = StrictOp;
4047 ConditionSrcRange = SR;
4048 ConditionLoc = SL;
4049 return false;
4050}
4051
4052bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4053 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004054 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004055 if (!NewStep)
4056 return true;
4057 if (!NewStep->isValueDependent()) {
4058 // Check that the step is integer expression.
4059 SourceLocation StepLoc = NewStep->getLocStart();
4060 ExprResult Val =
4061 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4062 if (Val.isInvalid())
4063 return true;
4064 NewStep = Val.get();
4065
4066 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4067 // If test-expr is of form var relational-op b and relational-op is < or
4068 // <= then incr-expr must cause var to increase on each iteration of the
4069 // loop. If test-expr is of form var relational-op b and relational-op is
4070 // > or >= then incr-expr must cause var to decrease on each iteration of
4071 // the loop.
4072 // If test-expr is of form b relational-op var and relational-op is < or
4073 // <= then incr-expr must cause var to decrease on each iteration of the
4074 // loop. If test-expr is of form b relational-op var and relational-op is
4075 // > or >= then incr-expr must cause var to increase on each iteration of
4076 // the loop.
4077 llvm::APSInt Result;
4078 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4079 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4080 bool IsConstNeg =
4081 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004082 bool IsConstPos =
4083 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004084 bool IsConstZero = IsConstant && !Result.getBoolValue();
4085 if (UB && (IsConstZero ||
4086 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004087 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004088 SemaRef.Diag(NewStep->getExprLoc(),
4089 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004090 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004091 SemaRef.Diag(ConditionLoc,
4092 diag::note_omp_loop_cond_requres_compatible_incr)
4093 << TestIsLessOp << ConditionSrcRange;
4094 return true;
4095 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004096 if (TestIsLessOp == Subtract) {
4097 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4098 NewStep).get();
4099 Subtract = !Subtract;
4100 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004101 }
4102
4103 Step = NewStep;
4104 SubtractStep = Subtract;
4105 return false;
4106}
4107
Alexey Bataev9c821032015-04-30 04:23:23 +00004108bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004109 // Check init-expr for canonical loop form and save loop counter
4110 // variable - #Var and its initialization value - #LB.
4111 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4112 // var = lb
4113 // integer-type var = lb
4114 // random-access-iterator-type var = lb
4115 // pointer-type var = lb
4116 //
4117 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004118 if (EmitDiags) {
4119 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4120 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004121 return true;
4122 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004123 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4124 if (!ExprTemp->cleanupsHaveSideEffects())
4125 S = ExprTemp->getSubExpr();
4126
Alexander Musmana5f070a2014-10-01 06:03:56 +00004127 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004128 if (Expr *E = dyn_cast<Expr>(S))
4129 S = E->IgnoreParens();
4130 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004131 if (BO->getOpcode() == BO_Assign) {
4132 auto *LHS = BO->getLHS()->IgnoreParens();
4133 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4134 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4135 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4136 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4137 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4138 }
4139 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4140 if (ME->isArrow() &&
4141 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4142 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4143 }
4144 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004145 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4146 if (DS->isSingleDecl()) {
4147 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004148 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004149 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004150 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004151 SemaRef.Diag(S->getLocStart(),
4152 diag::ext_omp_loop_not_canonical_init)
4153 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004154 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004155 }
4156 }
4157 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004158 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4159 if (CE->getOperator() == OO_Equal) {
4160 auto *LHS = CE->getArg(0);
4161 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4162 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4163 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4164 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4165 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4166 }
4167 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4168 if (ME->isArrow() &&
4169 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4170 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4171 }
4172 }
4173 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004174
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004175 if (Dependent() || SemaRef.CurContext->isDependentContext())
4176 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004177 if (EmitDiags) {
4178 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4179 << S->getSourceRange();
4180 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004181 return true;
4182}
4183
Alexey Bataev23b69422014-06-18 07:08:49 +00004184/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004185/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004186static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004187 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004188 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004189 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004190 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4191 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004192 if ((Ctor->isCopyOrMoveConstructor() ||
4193 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4194 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004196 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4197 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4198 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4199 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4200 return getCanonicalDecl(ME->getMemberDecl());
4201 return getCanonicalDecl(VD);
4202 }
4203 }
4204 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4205 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4206 return getCanonicalDecl(ME->getMemberDecl());
4207 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004208}
4209
4210bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4211 // Check test-expr for canonical form, save upper-bound UB, flags for
4212 // less/greater and for strict/non-strict comparison.
4213 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4214 // var relational-op b
4215 // b relational-op var
4216 //
4217 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004218 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004219 return true;
4220 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004221 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004222 SourceLocation CondLoc = S->getLocStart();
4223 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4224 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004225 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004226 return SetUB(BO->getRHS(),
4227 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4228 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4229 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004230 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004231 return SetUB(BO->getLHS(),
4232 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4233 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4234 BO->getSourceRange(), BO->getOperatorLoc());
4235 }
4236 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4237 if (CE->getNumArgs() == 2) {
4238 auto Op = CE->getOperator();
4239 switch (Op) {
4240 case OO_Greater:
4241 case OO_GreaterEqual:
4242 case OO_Less:
4243 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004244 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004245 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4246 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4247 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004248 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004249 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4250 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4251 CE->getOperatorLoc());
4252 break;
4253 default:
4254 break;
4255 }
4256 }
4257 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004258 if (Dependent() || SemaRef.CurContext->isDependentContext())
4259 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004260 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004261 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004262 return true;
4263}
4264
4265bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4266 // RHS of canonical loop form increment can be:
4267 // var + incr
4268 // incr + var
4269 // var - incr
4270 //
4271 RHS = RHS->IgnoreParenImpCasts();
4272 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4273 if (BO->isAdditiveOp()) {
4274 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004275 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004276 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004277 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004278 return SetStep(BO->getLHS(), false);
4279 }
4280 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4281 bool IsAdd = CE->getOperator() == OO_Plus;
4282 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004283 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004284 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004285 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004286 return SetStep(CE->getArg(0), false);
4287 }
4288 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004289 if (Dependent() || SemaRef.CurContext->isDependentContext())
4290 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004291 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004292 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004293 return true;
4294}
4295
4296bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4297 // Check incr-expr for canonical loop form and return true if it
4298 // does not conform.
4299 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4300 // ++var
4301 // var++
4302 // --var
4303 // var--
4304 // var += incr
4305 // var -= incr
4306 // var = var + incr
4307 // var = incr + var
4308 // var = var - incr
4309 //
4310 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004311 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004312 return true;
4313 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004314 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4315 if (!ExprTemp->cleanupsHaveSideEffects())
4316 S = ExprTemp->getSubExpr();
4317
Alexander Musmana5f070a2014-10-01 06:03:56 +00004318 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004319 S = S->IgnoreParens();
4320 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004321 if (UO->isIncrementDecrementOp() &&
4322 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004323 return SetStep(
4324 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4325 (UO->isDecrementOp() ? -1 : 1)).get(),
4326 false);
4327 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4328 switch (BO->getOpcode()) {
4329 case BO_AddAssign:
4330 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004331 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004332 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4333 break;
4334 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004335 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004336 return CheckIncRHS(BO->getRHS());
4337 break;
4338 default:
4339 break;
4340 }
4341 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4342 switch (CE->getOperator()) {
4343 case OO_PlusPlus:
4344 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004345 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004346 return SetStep(
4347 SemaRef.ActOnIntegerConstant(
4348 CE->getLocStart(),
4349 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4350 false);
4351 break;
4352 case OO_PlusEqual:
4353 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004354 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004355 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4356 break;
4357 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004358 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004359 return CheckIncRHS(CE->getArg(1));
4360 break;
4361 default:
4362 break;
4363 }
4364 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004365 if (Dependent() || SemaRef.CurContext->isDependentContext())
4366 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004367 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004368 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004369 return true;
4370}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004371
Alexey Bataev5a3af132016-03-29 08:58:54 +00004372static ExprResult
4373tryBuildCapture(Sema &SemaRef, Expr *Capture,
4374 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004375 if (SemaRef.CurContext->isDependentContext())
4376 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004377 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4378 return SemaRef.PerformImplicitConversion(
4379 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4380 /*AllowExplicit=*/true);
4381 auto I = Captures.find(Capture);
4382 if (I != Captures.end())
4383 return buildCapture(SemaRef, Capture, I->second);
4384 DeclRefExpr *Ref = nullptr;
4385 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4386 Captures[Capture] = Ref;
4387 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004388}
4389
Alexander Musmana5f070a2014-10-01 06:03:56 +00004390/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004391Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4392 Scope *S, const bool LimitedType,
4393 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004394 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004395 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004396 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004397 SemaRef.getLangOpts().CPlusPlus) {
4398 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004399 auto *UBExpr = TestIsLessOp ? UB : LB;
4400 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004401 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4402 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004403 if (!Upper || !Lower)
4404 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004405
4406 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4407
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004408 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004409 // BuildBinOp already emitted error, this one is to point user to upper
4410 // and lower bound, and to tell what is passed to 'operator-'.
4411 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4412 << Upper->getSourceRange() << Lower->getSourceRange();
4413 return nullptr;
4414 }
4415 }
4416
4417 if (!Diff.isUsable())
4418 return nullptr;
4419
4420 // Upper - Lower [- 1]
4421 if (TestIsStrictOp)
4422 Diff = SemaRef.BuildBinOp(
4423 S, DefaultLoc, BO_Sub, Diff.get(),
4424 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4425 if (!Diff.isUsable())
4426 return nullptr;
4427
4428 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004429 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4430 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004431 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004432 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004433 if (!Diff.isUsable())
4434 return nullptr;
4435
4436 // Parentheses (for dumping/debugging purposes only).
4437 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4438 if (!Diff.isUsable())
4439 return nullptr;
4440
4441 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004442 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004443 if (!Diff.isUsable())
4444 return nullptr;
4445
Alexander Musman174b3ca2014-10-06 11:16:29 +00004446 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004447 QualType Type = Diff.get()->getType();
4448 auto &C = SemaRef.Context;
4449 bool UseVarType = VarType->hasIntegerRepresentation() &&
4450 C.getTypeSize(Type) > C.getTypeSize(VarType);
4451 if (!Type->isIntegerType() || UseVarType) {
4452 unsigned NewSize =
4453 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4454 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4455 : Type->hasSignedIntegerRepresentation();
4456 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004457 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4458 Diff = SemaRef.PerformImplicitConversion(
4459 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4460 if (!Diff.isUsable())
4461 return nullptr;
4462 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004463 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004464 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004465 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4466 if (NewSize != C.getTypeSize(Type)) {
4467 if (NewSize < C.getTypeSize(Type)) {
4468 assert(NewSize == 64 && "incorrect loop var size");
4469 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4470 << InitSrcRange << ConditionSrcRange;
4471 }
4472 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004473 NewSize, Type->hasSignedIntegerRepresentation() ||
4474 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004475 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4476 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4477 Sema::AA_Converting, true);
4478 if (!Diff.isUsable())
4479 return nullptr;
4480 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004481 }
4482 }
4483
Alexander Musmana5f070a2014-10-01 06:03:56 +00004484 return Diff.get();
4485}
4486
Alexey Bataev5a3af132016-03-29 08:58:54 +00004487Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4488 Scope *S, Expr *Cond,
4489 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004490 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4491 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4492 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004493
Alexey Bataev5a3af132016-03-29 08:58:54 +00004494 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4495 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4496 if (!NewLB.isUsable() || !NewUB.isUsable())
4497 return nullptr;
4498
Alexey Bataev62dbb972015-04-22 11:59:37 +00004499 auto CondExpr = SemaRef.BuildBinOp(
4500 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4501 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004502 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004503 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004504 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4505 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004506 CondExpr = SemaRef.PerformImplicitConversion(
4507 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4508 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004509 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004510 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4511 // Otherwise use original loop conditon and evaluate it in runtime.
4512 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4513}
4514
Alexander Musmana5f070a2014-10-01 06:03:56 +00004515/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004516DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004517 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004518 auto *VD = dyn_cast<VarDecl>(LCDecl);
4519 if (!VD) {
4520 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4521 auto *Ref = buildDeclRefExpr(
4522 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004523 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4524 // If the loop control decl is explicitly marked as private, do not mark it
4525 // as captured again.
4526 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4527 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004528 return Ref;
4529 }
4530 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004531 DefaultLoc);
4532}
4533
4534Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004535 if (LCDecl && !LCDecl->isInvalidDecl()) {
4536 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004537 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004538 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4539 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004540 if (PrivateVar->isInvalidDecl())
4541 return nullptr;
4542 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4543 }
4544 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004545}
4546
4547/// \brief Build initization of the counter be used for codegen.
4548Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4549
4550/// \brief Build step of the counter be used for codegen.
4551Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4552
4553/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004554struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004555 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004556 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004557 /// \brief This expression calculates the number of iterations in the loop.
4558 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004559 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004560 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004561 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004562 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004563 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004564 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004565 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004566 /// \brief This is step for the #CounterVar used to generate its update:
4567 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004568 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004569 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004570 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004571 /// \brief Source range of the loop init.
4572 SourceRange InitSrcRange;
4573 /// \brief Source range of the loop condition.
4574 SourceRange CondSrcRange;
4575 /// \brief Source range of the loop increment.
4576 SourceRange IncSrcRange;
4577};
4578
Alexey Bataev23b69422014-06-18 07:08:49 +00004579} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004580
Alexey Bataev9c821032015-04-30 04:23:23 +00004581void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4582 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4583 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004584 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4585 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004586 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4587 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004588 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4589 if (auto *D = ISC.GetLoopDecl()) {
4590 auto *VD = dyn_cast<VarDecl>(D);
4591 if (!VD) {
4592 if (auto *Private = IsOpenMPCapturedDecl(D))
4593 VD = Private;
4594 else {
4595 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4596 /*WithInit=*/false);
4597 VD = cast<VarDecl>(Ref->getDecl());
4598 }
4599 }
4600 DSAStack->addLoopControlVariable(D, VD);
4601 }
4602 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004603 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004604 }
4605}
4606
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004607/// \brief Called on a for stmt to check and extract its iteration space
4608/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004609static bool CheckOpenMPIterationSpace(
4610 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4611 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004612 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004613 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004614 LoopIterationSpace &ResultIterSpace,
4615 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004616 // OpenMP [2.6, Canonical Loop Form]
4617 // for (init-expr; test-expr; incr-expr) structured-block
4618 auto For = dyn_cast_or_null<ForStmt>(S);
4619 if (!For) {
4620 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004621 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4622 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4623 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4624 if (NestedLoopCount > 1) {
4625 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4626 SemaRef.Diag(DSA.getConstructLoc(),
4627 diag::note_omp_collapse_ordered_expr)
4628 << 2 << CollapseLoopCountExpr->getSourceRange()
4629 << OrderedLoopCountExpr->getSourceRange();
4630 else if (CollapseLoopCountExpr)
4631 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4632 diag::note_omp_collapse_ordered_expr)
4633 << 0 << CollapseLoopCountExpr->getSourceRange();
4634 else
4635 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4636 diag::note_omp_collapse_ordered_expr)
4637 << 1 << OrderedLoopCountExpr->getSourceRange();
4638 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004639 return true;
4640 }
4641 assert(For->getBody());
4642
4643 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4644
4645 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004646 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004647 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004648 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004649
4650 bool HasErrors = false;
4651
4652 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004653 if (auto *LCDecl = ISC.GetLoopDecl()) {
4654 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004655
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004656 // OpenMP [2.6, Canonical Loop Form]
4657 // Var is one of the following:
4658 // A variable of signed or unsigned integer type.
4659 // For C++, a variable of a random access iterator type.
4660 // For C, a variable of a pointer type.
4661 auto VarType = LCDecl->getType().getNonReferenceType();
4662 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4663 !VarType->isPointerType() &&
4664 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4665 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4666 << SemaRef.getLangOpts().CPlusPlus;
4667 HasErrors = true;
4668 }
4669
4670 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4671 // a Construct
4672 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4673 // parallel for construct is (are) private.
4674 // The loop iteration variable in the associated for-loop of a simd
4675 // construct with just one associated for-loop is linear with a
4676 // constant-linear-step that is the increment of the associated for-loop.
4677 // Exclude loop var from the list of variables with implicitly defined data
4678 // sharing attributes.
4679 VarsWithImplicitDSA.erase(LCDecl);
4680
4681 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4682 // in a Construct, C/C++].
4683 // The loop iteration variable in the associated for-loop of a simd
4684 // construct with just one associated for-loop may be listed in a linear
4685 // clause with a constant-linear-step that is the increment of the
4686 // associated for-loop.
4687 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4688 // parallel for construct may be listed in a private or lastprivate clause.
4689 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4690 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4691 // declared in the loop and it is predetermined as a private.
4692 auto PredeterminedCKind =
4693 isOpenMPSimdDirective(DKind)
4694 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4695 : OMPC_private;
4696 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4697 DVar.CKind != PredeterminedCKind) ||
4698 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4699 isOpenMPDistributeDirective(DKind)) &&
4700 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4701 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4702 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4703 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4704 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4705 << getOpenMPClauseName(PredeterminedCKind);
4706 if (DVar.RefExpr == nullptr)
4707 DVar.CKind = PredeterminedCKind;
4708 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4709 HasErrors = true;
4710 } else if (LoopDeclRefExpr != nullptr) {
4711 // Make the loop iteration variable private (for worksharing constructs),
4712 // linear (for simd directives with the only one associated loop) or
4713 // lastprivate (for simd directives with several collapsed or ordered
4714 // loops).
4715 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004716 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4717 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004718 /*FromParent=*/false);
4719 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4720 }
4721
4722 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4723
4724 // Check test-expr.
4725 HasErrors |= ISC.CheckCond(For->getCond());
4726
4727 // Check incr-expr.
4728 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004729 }
4730
Alexander Musmana5f070a2014-10-01 06:03:56 +00004731 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004732 return HasErrors;
4733
Alexander Musmana5f070a2014-10-01 06:03:56 +00004734 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004735 ResultIterSpace.PreCond =
4736 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004737 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004738 DSA.getCurScope(),
4739 (isOpenMPWorksharingDirective(DKind) ||
4740 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4741 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004742 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004743 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004744 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4745 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4746 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4747 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4748 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4749 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4750
Alexey Bataev62dbb972015-04-22 11:59:37 +00004751 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4752 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004753 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004754 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004755 ResultIterSpace.CounterInit == nullptr ||
4756 ResultIterSpace.CounterStep == nullptr);
4757
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004758 return HasErrors;
4759}
4760
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004761/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004762static ExprResult
4763BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4764 ExprResult Start,
4765 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004766 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004767 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4768 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004769 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004770 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004771 VarRef.get()->getType())) {
4772 NewStart = SemaRef.PerformImplicitConversion(
4773 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4774 /*AllowExplicit=*/true);
4775 if (!NewStart.isUsable())
4776 return ExprError();
4777 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004778
4779 auto Init =
4780 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4781 return Init;
4782}
4783
Alexander Musmana5f070a2014-10-01 06:03:56 +00004784/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004785static ExprResult
4786BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4787 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4788 ExprResult Step, bool Subtract,
4789 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004790 // Add parentheses (for debugging purposes only).
4791 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4792 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4793 !Step.isUsable())
4794 return ExprError();
4795
Alexey Bataev5a3af132016-03-29 08:58:54 +00004796 ExprResult NewStep = Step;
4797 if (Captures)
4798 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004799 if (NewStep.isInvalid())
4800 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004801 ExprResult Update =
4802 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004803 if (!Update.isUsable())
4804 return ExprError();
4805
Alexey Bataevc0214e02016-02-16 12:13:49 +00004806 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4807 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004808 ExprResult NewStart = Start;
4809 if (Captures)
4810 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004811 if (NewStart.isInvalid())
4812 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004813
Alexey Bataevc0214e02016-02-16 12:13:49 +00004814 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4815 ExprResult SavedUpdate = Update;
4816 ExprResult UpdateVal;
4817 if (VarRef.get()->getType()->isOverloadableType() ||
4818 NewStart.get()->getType()->isOverloadableType() ||
4819 Update.get()->getType()->isOverloadableType()) {
4820 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4821 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4822 Update =
4823 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4824 if (Update.isUsable()) {
4825 UpdateVal =
4826 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4827 VarRef.get(), SavedUpdate.get());
4828 if (UpdateVal.isUsable()) {
4829 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4830 UpdateVal.get());
4831 }
4832 }
4833 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4834 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004835
Alexey Bataevc0214e02016-02-16 12:13:49 +00004836 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4837 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4838 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4839 NewStart.get(), SavedUpdate.get());
4840 if (!Update.isUsable())
4841 return ExprError();
4842
Alexey Bataev11481f52016-02-17 10:29:05 +00004843 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4844 VarRef.get()->getType())) {
4845 Update = SemaRef.PerformImplicitConversion(
4846 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4847 if (!Update.isUsable())
4848 return ExprError();
4849 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004850
4851 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4852 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004853 return Update;
4854}
4855
4856/// \brief Convert integer expression \a E to make it have at least \a Bits
4857/// bits.
4858static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4859 Sema &SemaRef) {
4860 if (E == nullptr)
4861 return ExprError();
4862 auto &C = SemaRef.Context;
4863 QualType OldType = E->getType();
4864 unsigned HasBits = C.getTypeSize(OldType);
4865 if (HasBits >= Bits)
4866 return ExprResult(E);
4867 // OK to convert to signed, because new type has more bits than old.
4868 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4869 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4870 true);
4871}
4872
4873/// \brief Check if the given expression \a E is a constant integer that fits
4874/// into \a Bits bits.
4875static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4876 if (E == nullptr)
4877 return false;
4878 llvm::APSInt Result;
4879 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4880 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4881 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004882}
4883
Alexey Bataev5a3af132016-03-29 08:58:54 +00004884/// Build preinits statement for the given declarations.
4885static Stmt *buildPreInits(ASTContext &Context,
4886 SmallVectorImpl<Decl *> &PreInits) {
4887 if (!PreInits.empty()) {
4888 return new (Context) DeclStmt(
4889 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4890 SourceLocation(), SourceLocation());
4891 }
4892 return nullptr;
4893}
4894
4895/// Build preinits statement for the given declarations.
4896static Stmt *buildPreInits(ASTContext &Context,
4897 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4898 if (!Captures.empty()) {
4899 SmallVector<Decl *, 16> PreInits;
4900 for (auto &Pair : Captures)
4901 PreInits.push_back(Pair.second->getDecl());
4902 return buildPreInits(Context, PreInits);
4903 }
4904 return nullptr;
4905}
4906
4907/// Build postupdate expression for the given list of postupdates expressions.
4908static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4909 Expr *PostUpdate = nullptr;
4910 if (!PostUpdates.empty()) {
4911 for (auto *E : PostUpdates) {
4912 Expr *ConvE = S.BuildCStyleCastExpr(
4913 E->getExprLoc(),
4914 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4915 E->getExprLoc(), E)
4916 .get();
4917 PostUpdate = PostUpdate
4918 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4919 PostUpdate, ConvE)
4920 .get()
4921 : ConvE;
4922 }
4923 }
4924 return PostUpdate;
4925}
4926
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004927/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004928/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4929/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004930static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004931CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4932 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4933 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004934 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004935 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004936 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004937 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004938 // Found 'collapse' clause - calculate collapse number.
4939 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004940 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004941 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004942 }
4943 if (OrderedLoopCountExpr) {
4944 // Found 'ordered' clause - calculate collapse number.
4945 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004946 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4947 if (Result.getLimitedValue() < NestedLoopCount) {
4948 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4949 diag::err_omp_wrong_ordered_loop_count)
4950 << OrderedLoopCountExpr->getSourceRange();
4951 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4952 diag::note_collapse_loop_count)
4953 << CollapseLoopCountExpr->getSourceRange();
4954 }
4955 NestedLoopCount = Result.getLimitedValue();
4956 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004957 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004958 // This is helper routine for loop directives (e.g., 'for', 'simd',
4959 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004960 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004961 SmallVector<LoopIterationSpace, 4> IterSpaces;
4962 IterSpaces.resize(NestedLoopCount);
4963 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004964 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004965 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004966 NestedLoopCount, CollapseLoopCountExpr,
4967 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004968 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004969 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004970 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004971 // OpenMP [2.8.1, simd construct, Restrictions]
4972 // All loops associated with the construct must be perfectly nested; that
4973 // is, there must be no intervening code nor any OpenMP directive between
4974 // any two loops.
4975 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004976 }
4977
Alexander Musmana5f070a2014-10-01 06:03:56 +00004978 Built.clear(/* size */ NestedLoopCount);
4979
4980 if (SemaRef.CurContext->isDependentContext())
4981 return NestedLoopCount;
4982
4983 // An example of what is generated for the following code:
4984 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004985 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004986 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004987 // for (k = 0; k < NK; ++k)
4988 // for (j = J0; j < NJ; j+=2) {
4989 // <loop body>
4990 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004991 //
4992 // We generate the code below.
4993 // Note: the loop body may be outlined in CodeGen.
4994 // Note: some counters may be C++ classes, operator- is used to find number of
4995 // iterations and operator+= to calculate counter value.
4996 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4997 // or i64 is currently supported).
4998 //
4999 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5000 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5001 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5002 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5003 // // similar updates for vars in clauses (e.g. 'linear')
5004 // <loop body (using local i and j)>
5005 // }
5006 // i = NI; // assign final values of counters
5007 // j = NJ;
5008 //
5009
5010 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5011 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005012 // Precondition tests if there is at least one iteration (all conditions are
5013 // true).
5014 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005015 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005016 ExprResult LastIteration32 = WidenIterationCount(
5017 32 /* Bits */, SemaRef.PerformImplicitConversion(
5018 N0->IgnoreImpCasts(), N0->getType(),
5019 Sema::AA_Converting, /*AllowExplicit=*/true)
5020 .get(),
5021 SemaRef);
5022 ExprResult LastIteration64 = WidenIterationCount(
5023 64 /* Bits */, SemaRef.PerformImplicitConversion(
5024 N0->IgnoreImpCasts(), N0->getType(),
5025 Sema::AA_Converting, /*AllowExplicit=*/true)
5026 .get(),
5027 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005028
5029 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5030 return NestedLoopCount;
5031
5032 auto &C = SemaRef.Context;
5033 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5034
5035 Scope *CurScope = DSA.getCurScope();
5036 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005037 if (PreCond.isUsable()) {
5038 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5039 PreCond.get(), IterSpaces[Cnt].PreCond);
5040 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005041 auto N = IterSpaces[Cnt].NumIterations;
5042 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5043 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005044 LastIteration32 = SemaRef.BuildBinOp(
5045 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5046 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5047 Sema::AA_Converting,
5048 /*AllowExplicit=*/true)
5049 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005050 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005051 LastIteration64 = SemaRef.BuildBinOp(
5052 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5053 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5054 Sema::AA_Converting,
5055 /*AllowExplicit=*/true)
5056 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005057 }
5058
5059 // Choose either the 32-bit or 64-bit version.
5060 ExprResult LastIteration = LastIteration64;
5061 if (LastIteration32.isUsable() &&
5062 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5063 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5064 FitsInto(
5065 32 /* Bits */,
5066 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5067 LastIteration64.get(), SemaRef)))
5068 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005069 QualType VType = LastIteration.get()->getType();
5070 QualType RealVType = VType;
5071 QualType StrideVType = VType;
5072 if (isOpenMPTaskLoopDirective(DKind)) {
5073 VType =
5074 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5075 StrideVType =
5076 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5077 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005078
5079 if (!LastIteration.isUsable())
5080 return 0;
5081
5082 // Save the number of iterations.
5083 ExprResult NumIterations = LastIteration;
5084 {
5085 LastIteration = SemaRef.BuildBinOp(
5086 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5087 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5088 if (!LastIteration.isUsable())
5089 return 0;
5090 }
5091
5092 // Calculate the last iteration number beforehand instead of doing this on
5093 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5094 llvm::APSInt Result;
5095 bool IsConstant =
5096 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5097 ExprResult CalcLastIteration;
5098 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005099 ExprResult SaveRef =
5100 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005101 LastIteration = SaveRef;
5102
5103 // Prepare SaveRef + 1.
5104 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005105 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005106 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5107 if (!NumIterations.isUsable())
5108 return 0;
5109 }
5110
5111 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5112
Alexander Musmanc6388682014-12-15 07:07:06 +00005113 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005114 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005115 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5116 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005117 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005118 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5119 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005120 SemaRef.AddInitializerToDecl(
5121 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5122 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5123
5124 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005125 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5126 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005127 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5128 /*DirectInit*/ false,
5129 /*TypeMayContainAuto*/ false);
5130
5131 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5132 // This will be used to implement clause 'lastprivate'.
5133 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005134 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5135 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005136 SemaRef.AddInitializerToDecl(
5137 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5138 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5139
5140 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005141 VarDecl *STDecl =
5142 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5143 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005144 SemaRef.AddInitializerToDecl(
5145 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5146 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5147
5148 // Build expression: UB = min(UB, LastIteration)
5149 // It is nesessary for CodeGen of directives with static scheduling.
5150 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5151 UB.get(), LastIteration.get());
5152 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5153 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5154 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5155 CondOp.get());
5156 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005157
5158 // If we have a combined directive that combines 'distribute', 'for' or
5159 // 'simd' we need to be able to access the bounds of the schedule of the
5160 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5161 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5162 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5163 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5164
5165 // We expect to have at least 2 more parameters than the 'parallel'
5166 // directive does - the lower and upper bounds of the previous schedule.
5167 assert(CD->getNumParams() >= 4 &&
5168 "Unexpected number of parameters in loop combined directive");
5169
5170 // Set the proper type for the bounds given what we learned from the
5171 // enclosed loops.
5172 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5173 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5174
5175 // Previous lower and upper bounds are obtained from the region
5176 // parameters.
5177 PrevLB =
5178 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5179 PrevUB =
5180 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5181 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005182 }
5183
5184 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005185 ExprResult IV;
5186 ExprResult Init;
5187 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005188 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5189 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005190 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005191 isOpenMPTaskLoopDirective(DKind) ||
5192 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005193 ? LB.get()
5194 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5195 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5196 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005197 }
5198
Alexander Musmanc6388682014-12-15 07:07:06 +00005199 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005200 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005201 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005202 (isOpenMPWorksharingDirective(DKind) ||
5203 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005204 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5205 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5206 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005207
5208 // Loop increment (IV = IV + 1)
5209 SourceLocation IncLoc;
5210 ExprResult Inc =
5211 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5212 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5213 if (!Inc.isUsable())
5214 return 0;
5215 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005216 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5217 if (!Inc.isUsable())
5218 return 0;
5219
5220 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5221 // Used for directives with static scheduling.
5222 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005223 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5224 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005225 // LB + ST
5226 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5227 if (!NextLB.isUsable())
5228 return 0;
5229 // LB = LB + ST
5230 NextLB =
5231 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5232 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5233 if (!NextLB.isUsable())
5234 return 0;
5235 // UB + ST
5236 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5237 if (!NextUB.isUsable())
5238 return 0;
5239 // UB = UB + ST
5240 NextUB =
5241 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5242 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5243 if (!NextUB.isUsable())
5244 return 0;
5245 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005246
5247 // Build updates and final values of the loop counters.
5248 bool HasErrors = false;
5249 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005250 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005251 Built.Updates.resize(NestedLoopCount);
5252 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005253 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005254 {
5255 ExprResult Div;
5256 // Go from inner nested loop to outer.
5257 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5258 LoopIterationSpace &IS = IterSpaces[Cnt];
5259 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5260 // Build: Iter = (IV / Div) % IS.NumIters
5261 // where Div is product of previous iterations' IS.NumIters.
5262 ExprResult Iter;
5263 if (Div.isUsable()) {
5264 Iter =
5265 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5266 } else {
5267 Iter = IV;
5268 assert((Cnt == (int)NestedLoopCount - 1) &&
5269 "unusable div expected on first iteration only");
5270 }
5271
5272 if (Cnt != 0 && Iter.isUsable())
5273 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5274 IS.NumIterations);
5275 if (!Iter.isUsable()) {
5276 HasErrors = true;
5277 break;
5278 }
5279
Alexey Bataev39f915b82015-05-08 10:41:21 +00005280 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005281 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5282 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5283 IS.CounterVar->getExprLoc(),
5284 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005285 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005286 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005287 if (!Init.isUsable()) {
5288 HasErrors = true;
5289 break;
5290 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005291 ExprResult Update = BuildCounterUpdate(
5292 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5293 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005294 if (!Update.isUsable()) {
5295 HasErrors = true;
5296 break;
5297 }
5298
5299 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5300 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005301 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005302 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005303 if (!Final.isUsable()) {
5304 HasErrors = true;
5305 break;
5306 }
5307
5308 // Build Div for the next iteration: Div <- Div * IS.NumIters
5309 if (Cnt != 0) {
5310 if (Div.isUnset())
5311 Div = IS.NumIterations;
5312 else
5313 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5314 IS.NumIterations);
5315
5316 // Add parentheses (for debugging purposes only).
5317 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005318 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005319 if (!Div.isUsable()) {
5320 HasErrors = true;
5321 break;
5322 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005323 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005324 }
5325 if (!Update.isUsable() || !Final.isUsable()) {
5326 HasErrors = true;
5327 break;
5328 }
5329 // Save results
5330 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005331 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005332 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005333 Built.Updates[Cnt] = Update.get();
5334 Built.Finals[Cnt] = Final.get();
5335 }
5336 }
5337
5338 if (HasErrors)
5339 return 0;
5340
5341 // Save results
5342 Built.IterationVarRef = IV.get();
5343 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005344 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005345 Built.CalcLastIteration =
5346 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005347 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005348 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005349 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005350 Built.Init = Init.get();
5351 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005352 Built.LB = LB.get();
5353 Built.UB = UB.get();
5354 Built.IL = IL.get();
5355 Built.ST = ST.get();
5356 Built.EUB = EUB.get();
5357 Built.NLB = NextLB.get();
5358 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005359 Built.PrevLB = PrevLB.get();
5360 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005361
Alexey Bataev8b427062016-05-25 12:36:08 +00005362 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5363 // Fill data for doacross depend clauses.
5364 for (auto Pair : DSA.getDoacrossDependClauses()) {
5365 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5366 Pair.first->setCounterValue(CounterVal);
5367 else {
5368 if (NestedLoopCount != Pair.second.size() ||
5369 NestedLoopCount != LoopMultipliers.size() + 1) {
5370 // Erroneous case - clause has some problems.
5371 Pair.first->setCounterValue(CounterVal);
5372 continue;
5373 }
5374 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5375 auto I = Pair.second.rbegin();
5376 auto IS = IterSpaces.rbegin();
5377 auto ILM = LoopMultipliers.rbegin();
5378 Expr *UpCounterVal = CounterVal;
5379 Expr *Multiplier = nullptr;
5380 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5381 if (I->first) {
5382 assert(IS->CounterStep);
5383 Expr *NormalizedOffset =
5384 SemaRef
5385 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5386 I->first, IS->CounterStep)
5387 .get();
5388 if (Multiplier) {
5389 NormalizedOffset =
5390 SemaRef
5391 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5392 NormalizedOffset, Multiplier)
5393 .get();
5394 }
5395 assert(I->second == OO_Plus || I->second == OO_Minus);
5396 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5397 UpCounterVal =
5398 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5399 UpCounterVal, NormalizedOffset).get();
5400 }
5401 Multiplier = *ILM;
5402 ++I;
5403 ++IS;
5404 ++ILM;
5405 }
5406 Pair.first->setCounterValue(UpCounterVal);
5407 }
5408 }
5409
Alexey Bataevabfc0692014-06-25 06:52:00 +00005410 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005411}
5412
Alexey Bataev10e775f2015-07-30 11:36:16 +00005413static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005414 auto CollapseClauses =
5415 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5416 if (CollapseClauses.begin() != CollapseClauses.end())
5417 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005418 return nullptr;
5419}
5420
Alexey Bataev10e775f2015-07-30 11:36:16 +00005421static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005422 auto OrderedClauses =
5423 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5424 if (OrderedClauses.begin() != OrderedClauses.end())
5425 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005426 return nullptr;
5427}
5428
Alexey Bataev66b15b52015-08-21 11:14:16 +00005429static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
5430 const Expr *Safelen) {
5431 llvm::APSInt SimdlenRes, SafelenRes;
5432 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
5433 Simdlen->isInstantiationDependent() ||
5434 Simdlen->containsUnexpandedParameterPack())
5435 return false;
5436 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
5437 Safelen->isInstantiationDependent() ||
5438 Safelen->containsUnexpandedParameterPack())
5439 return false;
5440 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
5441 Safelen->EvaluateAsInt(SafelenRes, S.Context);
5442 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5443 // If both simdlen and safelen clauses are specified, the value of the simdlen
5444 // parameter must be less than or equal to the value of the safelen parameter.
5445 if (SimdlenRes > SafelenRes) {
5446 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
5447 << Simdlen->getSourceRange() << Safelen->getSourceRange();
5448 return true;
5449 }
5450 return false;
5451}
5452
Alexey Bataev4acb8592014-07-07 13:01:15 +00005453StmtResult Sema::ActOnOpenMPSimdDirective(
5454 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5455 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005456 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005457 if (!AStmt)
5458 return StmtError();
5459
5460 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005461 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005462 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5463 // define the nested loops number.
5464 unsigned NestedLoopCount = CheckOpenMPLoop(
5465 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5466 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005467 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005468 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005469
Alexander Musmana5f070a2014-10-01 06:03:56 +00005470 assert((CurContext->isDependentContext() || B.builtAll()) &&
5471 "omp simd loop exprs were not built");
5472
Alexander Musman3276a272015-03-21 10:12:56 +00005473 if (!CurContext->isDependentContext()) {
5474 // Finalize the clauses that need pre-built expressions for CodeGen.
5475 for (auto C : Clauses) {
5476 if (auto LC = dyn_cast<OMPLinearClause>(C))
5477 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005478 B.NumIterations, *this, CurScope,
5479 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005480 return StmtError();
5481 }
5482 }
5483
Alexey Bataev66b15b52015-08-21 11:14:16 +00005484 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5485 // If both simdlen and safelen clauses are specified, the value of the simdlen
5486 // parameter must be less than or equal to the value of the safelen parameter.
5487 OMPSafelenClause *Safelen = nullptr;
5488 OMPSimdlenClause *Simdlen = nullptr;
5489 for (auto *Clause : Clauses) {
5490 if (Clause->getClauseKind() == OMPC_safelen)
5491 Safelen = cast<OMPSafelenClause>(Clause);
5492 else if (Clause->getClauseKind() == OMPC_simdlen)
5493 Simdlen = cast<OMPSimdlenClause>(Clause);
5494 if (Safelen && Simdlen)
5495 break;
5496 }
5497 if (Simdlen && Safelen &&
5498 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5499 Safelen->getSafelen()))
5500 return StmtError();
5501
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005502 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005503 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5504 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005505}
5506
Alexey Bataev4acb8592014-07-07 13:01:15 +00005507StmtResult Sema::ActOnOpenMPForDirective(
5508 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5509 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005510 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005511 if (!AStmt)
5512 return StmtError();
5513
5514 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005515 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005516 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5517 // define the nested loops number.
5518 unsigned NestedLoopCount = CheckOpenMPLoop(
5519 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5520 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005521 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005522 return StmtError();
5523
Alexander Musmana5f070a2014-10-01 06:03:56 +00005524 assert((CurContext->isDependentContext() || B.builtAll()) &&
5525 "omp for loop exprs were not built");
5526
Alexey Bataev54acd402015-08-04 11:18:19 +00005527 if (!CurContext->isDependentContext()) {
5528 // Finalize the clauses that need pre-built expressions for CodeGen.
5529 for (auto C : Clauses) {
5530 if (auto LC = dyn_cast<OMPLinearClause>(C))
5531 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005532 B.NumIterations, *this, CurScope,
5533 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005534 return StmtError();
5535 }
5536 }
5537
Alexey Bataevf29276e2014-06-18 04:14:57 +00005538 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005539 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005540 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005541}
5542
Alexander Musmanf82886e2014-09-18 05:12:34 +00005543StmtResult Sema::ActOnOpenMPForSimdDirective(
5544 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5545 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005546 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005547 if (!AStmt)
5548 return StmtError();
5549
5550 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005551 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005552 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5553 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005554 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005555 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5556 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5557 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005558 if (NestedLoopCount == 0)
5559 return StmtError();
5560
Alexander Musmanc6388682014-12-15 07:07:06 +00005561 assert((CurContext->isDependentContext() || B.builtAll()) &&
5562 "omp for simd loop exprs were not built");
5563
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005564 if (!CurContext->isDependentContext()) {
5565 // Finalize the clauses that need pre-built expressions for CodeGen.
5566 for (auto C : Clauses) {
5567 if (auto LC = dyn_cast<OMPLinearClause>(C))
5568 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005569 B.NumIterations, *this, CurScope,
5570 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005571 return StmtError();
5572 }
5573 }
5574
Alexey Bataev66b15b52015-08-21 11:14:16 +00005575 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5576 // If both simdlen and safelen clauses are specified, the value of the simdlen
5577 // parameter must be less than or equal to the value of the safelen parameter.
5578 OMPSafelenClause *Safelen = nullptr;
5579 OMPSimdlenClause *Simdlen = nullptr;
5580 for (auto *Clause : Clauses) {
5581 if (Clause->getClauseKind() == OMPC_safelen)
5582 Safelen = cast<OMPSafelenClause>(Clause);
5583 else if (Clause->getClauseKind() == OMPC_simdlen)
5584 Simdlen = cast<OMPSimdlenClause>(Clause);
5585 if (Safelen && Simdlen)
5586 break;
5587 }
5588 if (Simdlen && Safelen &&
5589 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5590 Safelen->getSafelen()))
5591 return StmtError();
5592
Alexander Musmanf82886e2014-09-18 05:12:34 +00005593 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005594 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5595 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005596}
5597
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005598StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5599 Stmt *AStmt,
5600 SourceLocation StartLoc,
5601 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005602 if (!AStmt)
5603 return StmtError();
5604
5605 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005606 auto BaseStmt = AStmt;
5607 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5608 BaseStmt = CS->getCapturedStmt();
5609 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5610 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005611 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005612 return StmtError();
5613 // All associated statements must be '#pragma omp section' except for
5614 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005615 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005616 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5617 if (SectionStmt)
5618 Diag(SectionStmt->getLocStart(),
5619 diag::err_omp_sections_substmt_not_section);
5620 return StmtError();
5621 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005622 cast<OMPSectionDirective>(SectionStmt)
5623 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005624 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005625 } else {
5626 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5627 return StmtError();
5628 }
5629
5630 getCurFunction()->setHasBranchProtectedScope();
5631
Alexey Bataev25e5b442015-09-15 12:52:43 +00005632 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5633 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005634}
5635
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005636StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5637 SourceLocation StartLoc,
5638 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005639 if (!AStmt)
5640 return StmtError();
5641
5642 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005643
5644 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005645 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005646
Alexey Bataev25e5b442015-09-15 12:52:43 +00005647 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5648 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005649}
5650
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005651StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5652 Stmt *AStmt,
5653 SourceLocation StartLoc,
5654 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005655 if (!AStmt)
5656 return StmtError();
5657
5658 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005659
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005660 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005661
Alexey Bataev3255bf32015-01-19 05:20:46 +00005662 // OpenMP [2.7.3, single Construct, Restrictions]
5663 // The copyprivate clause must not be used with the nowait clause.
5664 OMPClause *Nowait = nullptr;
5665 OMPClause *Copyprivate = nullptr;
5666 for (auto *Clause : Clauses) {
5667 if (Clause->getClauseKind() == OMPC_nowait)
5668 Nowait = Clause;
5669 else if (Clause->getClauseKind() == OMPC_copyprivate)
5670 Copyprivate = Clause;
5671 if (Copyprivate && Nowait) {
5672 Diag(Copyprivate->getLocStart(),
5673 diag::err_omp_single_copyprivate_with_nowait);
5674 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5675 return StmtError();
5676 }
5677 }
5678
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005679 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5680}
5681
Alexander Musman80c22892014-07-17 08:54:58 +00005682StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5683 SourceLocation StartLoc,
5684 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005685 if (!AStmt)
5686 return StmtError();
5687
5688 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005689
5690 getCurFunction()->setHasBranchProtectedScope();
5691
5692 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5693}
5694
Alexey Bataev28c75412015-12-15 08:19:24 +00005695StmtResult Sema::ActOnOpenMPCriticalDirective(
5696 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5697 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005698 if (!AStmt)
5699 return StmtError();
5700
5701 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005702
Alexey Bataev28c75412015-12-15 08:19:24 +00005703 bool ErrorFound = false;
5704 llvm::APSInt Hint;
5705 SourceLocation HintLoc;
5706 bool DependentHint = false;
5707 for (auto *C : Clauses) {
5708 if (C->getClauseKind() == OMPC_hint) {
5709 if (!DirName.getName()) {
5710 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5711 ErrorFound = true;
5712 }
5713 Expr *E = cast<OMPHintClause>(C)->getHint();
5714 if (E->isTypeDependent() || E->isValueDependent() ||
5715 E->isInstantiationDependent())
5716 DependentHint = true;
5717 else {
5718 Hint = E->EvaluateKnownConstInt(Context);
5719 HintLoc = C->getLocStart();
5720 }
5721 }
5722 }
5723 if (ErrorFound)
5724 return StmtError();
5725 auto Pair = DSAStack->getCriticalWithHint(DirName);
5726 if (Pair.first && DirName.getName() && !DependentHint) {
5727 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5728 Diag(StartLoc, diag::err_omp_critical_with_hint);
5729 if (HintLoc.isValid()) {
5730 Diag(HintLoc, diag::note_omp_critical_hint_here)
5731 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5732 } else
5733 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5734 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5735 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5736 << 1
5737 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5738 /*Radix=*/10, /*Signed=*/false);
5739 } else
5740 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5741 }
5742 }
5743
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005744 getCurFunction()->setHasBranchProtectedScope();
5745
Alexey Bataev28c75412015-12-15 08:19:24 +00005746 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5747 Clauses, AStmt);
5748 if (!Pair.first && DirName.getName() && !DependentHint)
5749 DSAStack->addCriticalWithHint(Dir, Hint);
5750 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005751}
5752
Alexey Bataev4acb8592014-07-07 13:01:15 +00005753StmtResult Sema::ActOnOpenMPParallelForDirective(
5754 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5755 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005756 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005757 if (!AStmt)
5758 return StmtError();
5759
Alexey Bataev4acb8592014-07-07 13:01:15 +00005760 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5761 // 1.2.2 OpenMP Language Terminology
5762 // Structured block - An executable statement with a single entry at the
5763 // top and a single exit at the bottom.
5764 // The point of exit cannot be a branch out of the structured block.
5765 // longjmp() and throw() must not violate the entry/exit criteria.
5766 CS->getCapturedDecl()->setNothrow();
5767
Alexander Musmanc6388682014-12-15 07:07:06 +00005768 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005769 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5770 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005771 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005772 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5773 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5774 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005775 if (NestedLoopCount == 0)
5776 return StmtError();
5777
Alexander Musmana5f070a2014-10-01 06:03:56 +00005778 assert((CurContext->isDependentContext() || B.builtAll()) &&
5779 "omp parallel for loop exprs were not built");
5780
Alexey Bataev54acd402015-08-04 11:18:19 +00005781 if (!CurContext->isDependentContext()) {
5782 // Finalize the clauses that need pre-built expressions for CodeGen.
5783 for (auto C : Clauses) {
5784 if (auto LC = dyn_cast<OMPLinearClause>(C))
5785 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005786 B.NumIterations, *this, CurScope,
5787 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005788 return StmtError();
5789 }
5790 }
5791
Alexey Bataev4acb8592014-07-07 13:01:15 +00005792 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005793 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005794 NestedLoopCount, Clauses, AStmt, B,
5795 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005796}
5797
Alexander Musmane4e893b2014-09-23 09:33:00 +00005798StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5799 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5800 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005801 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005802 if (!AStmt)
5803 return StmtError();
5804
Alexander Musmane4e893b2014-09-23 09:33:00 +00005805 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5806 // 1.2.2 OpenMP Language Terminology
5807 // Structured block - An executable statement with a single entry at the
5808 // top and a single exit at the bottom.
5809 // The point of exit cannot be a branch out of the structured block.
5810 // longjmp() and throw() must not violate the entry/exit criteria.
5811 CS->getCapturedDecl()->setNothrow();
5812
Alexander Musmanc6388682014-12-15 07:07:06 +00005813 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005814 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5815 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005816 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005817 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5818 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5819 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005820 if (NestedLoopCount == 0)
5821 return StmtError();
5822
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005823 if (!CurContext->isDependentContext()) {
5824 // Finalize the clauses that need pre-built expressions for CodeGen.
5825 for (auto C : Clauses) {
5826 if (auto LC = dyn_cast<OMPLinearClause>(C))
5827 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005828 B.NumIterations, *this, CurScope,
5829 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005830 return StmtError();
5831 }
5832 }
5833
Alexey Bataev66b15b52015-08-21 11:14:16 +00005834 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5835 // If both simdlen and safelen clauses are specified, the value of the simdlen
5836 // parameter must be less than or equal to the value of the safelen parameter.
5837 OMPSafelenClause *Safelen = nullptr;
5838 OMPSimdlenClause *Simdlen = nullptr;
5839 for (auto *Clause : Clauses) {
5840 if (Clause->getClauseKind() == OMPC_safelen)
5841 Safelen = cast<OMPSafelenClause>(Clause);
5842 else if (Clause->getClauseKind() == OMPC_simdlen)
5843 Simdlen = cast<OMPSimdlenClause>(Clause);
5844 if (Safelen && Simdlen)
5845 break;
5846 }
5847 if (Simdlen && Safelen &&
5848 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5849 Safelen->getSafelen()))
5850 return StmtError();
5851
Alexander Musmane4e893b2014-09-23 09:33:00 +00005852 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005853 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005854 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005855}
5856
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005857StmtResult
5858Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5859 Stmt *AStmt, SourceLocation StartLoc,
5860 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005861 if (!AStmt)
5862 return StmtError();
5863
5864 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005865 auto BaseStmt = AStmt;
5866 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5867 BaseStmt = CS->getCapturedStmt();
5868 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5869 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005870 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005871 return StmtError();
5872 // All associated statements must be '#pragma omp section' except for
5873 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005874 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005875 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5876 if (SectionStmt)
5877 Diag(SectionStmt->getLocStart(),
5878 diag::err_omp_parallel_sections_substmt_not_section);
5879 return StmtError();
5880 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005881 cast<OMPSectionDirective>(SectionStmt)
5882 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005883 }
5884 } else {
5885 Diag(AStmt->getLocStart(),
5886 diag::err_omp_parallel_sections_not_compound_stmt);
5887 return StmtError();
5888 }
5889
5890 getCurFunction()->setHasBranchProtectedScope();
5891
Alexey Bataev25e5b442015-09-15 12:52:43 +00005892 return OMPParallelSectionsDirective::Create(
5893 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005894}
5895
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005896StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5897 Stmt *AStmt, SourceLocation StartLoc,
5898 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005899 if (!AStmt)
5900 return StmtError();
5901
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005902 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5903 // 1.2.2 OpenMP Language Terminology
5904 // Structured block - An executable statement with a single entry at the
5905 // top and a single exit at the bottom.
5906 // The point of exit cannot be a branch out of the structured block.
5907 // longjmp() and throw() must not violate the entry/exit criteria.
5908 CS->getCapturedDecl()->setNothrow();
5909
5910 getCurFunction()->setHasBranchProtectedScope();
5911
Alexey Bataev25e5b442015-09-15 12:52:43 +00005912 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5913 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005914}
5915
Alexey Bataev68446b72014-07-18 07:47:19 +00005916StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5917 SourceLocation EndLoc) {
5918 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5919}
5920
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005921StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5922 SourceLocation EndLoc) {
5923 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5924}
5925
Alexey Bataev2df347a2014-07-18 10:17:07 +00005926StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5927 SourceLocation EndLoc) {
5928 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5929}
5930
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005931StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5932 SourceLocation StartLoc,
5933 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005934 if (!AStmt)
5935 return StmtError();
5936
5937 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005938
5939 getCurFunction()->setHasBranchProtectedScope();
5940
5941 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5942}
5943
Alexey Bataev6125da92014-07-21 11:26:11 +00005944StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5945 SourceLocation StartLoc,
5946 SourceLocation EndLoc) {
5947 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5948 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5949}
5950
Alexey Bataev346265e2015-09-25 10:37:12 +00005951StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5952 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005953 SourceLocation StartLoc,
5954 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005955 OMPClause *DependFound = nullptr;
5956 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005957 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005958 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005959 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005960 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005961 for (auto *C : Clauses) {
5962 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5963 DependFound = C;
5964 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5965 if (DependSourceClause) {
5966 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5967 << getOpenMPDirectiveName(OMPD_ordered)
5968 << getOpenMPClauseName(OMPC_depend) << 2;
5969 ErrorFound = true;
5970 } else
5971 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005972 if (DependSinkClause) {
5973 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5974 << 0;
5975 ErrorFound = true;
5976 }
5977 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5978 if (DependSourceClause) {
5979 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5980 << 1;
5981 ErrorFound = true;
5982 }
5983 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005984 }
5985 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005986 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005987 else if (C->getClauseKind() == OMPC_simd)
5988 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005989 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005990 if (!ErrorFound && !SC &&
5991 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005992 // OpenMP [2.8.1,simd Construct, Restrictions]
5993 // An ordered construct with the simd clause is the only OpenMP construct
5994 // that can appear in the simd region.
5995 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005996 ErrorFound = true;
5997 } else if (DependFound && (TC || SC)) {
5998 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5999 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6000 ErrorFound = true;
6001 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6002 Diag(DependFound->getLocStart(),
6003 diag::err_omp_ordered_directive_without_param);
6004 ErrorFound = true;
6005 } else if (TC || Clauses.empty()) {
6006 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6007 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6008 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6009 << (TC != nullptr);
6010 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6011 ErrorFound = true;
6012 }
6013 }
6014 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006015 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006016
6017 if (AStmt) {
6018 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6019
6020 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006021 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006022
6023 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006024}
6025
Alexey Bataev1d160b12015-03-13 12:27:31 +00006026namespace {
6027/// \brief Helper class for checking expression in 'omp atomic [update]'
6028/// construct.
6029class OpenMPAtomicUpdateChecker {
6030 /// \brief Error results for atomic update expressions.
6031 enum ExprAnalysisErrorCode {
6032 /// \brief A statement is not an expression statement.
6033 NotAnExpression,
6034 /// \brief Expression is not builtin binary or unary operation.
6035 NotABinaryOrUnaryExpression,
6036 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6037 NotAnUnaryIncDecExpression,
6038 /// \brief An expression is not of scalar type.
6039 NotAScalarType,
6040 /// \brief A binary operation is not an assignment operation.
6041 NotAnAssignmentOp,
6042 /// \brief RHS part of the binary operation is not a binary expression.
6043 NotABinaryExpression,
6044 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6045 /// expression.
6046 NotABinaryOperator,
6047 /// \brief RHS binary operation does not have reference to the updated LHS
6048 /// part.
6049 NotAnUpdateExpression,
6050 /// \brief No errors is found.
6051 NoError
6052 };
6053 /// \brief Reference to Sema.
6054 Sema &SemaRef;
6055 /// \brief A location for note diagnostics (when error is found).
6056 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006057 /// \brief 'x' lvalue part of the source atomic expression.
6058 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006059 /// \brief 'expr' rvalue part of the source atomic expression.
6060 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006061 /// \brief Helper expression of the form
6062 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6063 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6064 Expr *UpdateExpr;
6065 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6066 /// important for non-associative operations.
6067 bool IsXLHSInRHSPart;
6068 BinaryOperatorKind Op;
6069 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006070 /// \brief true if the source expression is a postfix unary operation, false
6071 /// if it is a prefix unary operation.
6072 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006073
6074public:
6075 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006076 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006077 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006078 /// \brief Check specified statement that it is suitable for 'atomic update'
6079 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006080 /// expression. If DiagId and NoteId == 0, then only check is performed
6081 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006082 /// \param DiagId Diagnostic which should be emitted if error is found.
6083 /// \param NoteId Diagnostic note for the main error message.
6084 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006085 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006086 /// \brief Return the 'x' lvalue part of the source atomic expression.
6087 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006088 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6089 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006090 /// \brief Return the update expression used in calculation of the updated
6091 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6092 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6093 Expr *getUpdateExpr() const { return UpdateExpr; }
6094 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6095 /// false otherwise.
6096 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6097
Alexey Bataevb78ca832015-04-01 03:33:17 +00006098 /// \brief true if the source expression is a postfix unary operation, false
6099 /// if it is a prefix unary operation.
6100 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6101
Alexey Bataev1d160b12015-03-13 12:27:31 +00006102private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006103 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6104 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006105};
6106} // namespace
6107
6108bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6109 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6110 ExprAnalysisErrorCode ErrorFound = NoError;
6111 SourceLocation ErrorLoc, NoteLoc;
6112 SourceRange ErrorRange, NoteRange;
6113 // Allowed constructs are:
6114 // x = x binop expr;
6115 // x = expr binop x;
6116 if (AtomicBinOp->getOpcode() == BO_Assign) {
6117 X = AtomicBinOp->getLHS();
6118 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6119 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6120 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6121 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6122 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006123 Op = AtomicInnerBinOp->getOpcode();
6124 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006125 auto *LHS = AtomicInnerBinOp->getLHS();
6126 auto *RHS = AtomicInnerBinOp->getRHS();
6127 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6128 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6129 /*Canonical=*/true);
6130 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6131 /*Canonical=*/true);
6132 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6133 /*Canonical=*/true);
6134 if (XId == LHSId) {
6135 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006136 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006137 } else if (XId == RHSId) {
6138 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006139 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006140 } else {
6141 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6142 ErrorRange = AtomicInnerBinOp->getSourceRange();
6143 NoteLoc = X->getExprLoc();
6144 NoteRange = X->getSourceRange();
6145 ErrorFound = NotAnUpdateExpression;
6146 }
6147 } else {
6148 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6149 ErrorRange = AtomicInnerBinOp->getSourceRange();
6150 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6151 NoteRange = SourceRange(NoteLoc, NoteLoc);
6152 ErrorFound = NotABinaryOperator;
6153 }
6154 } else {
6155 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6156 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6157 ErrorFound = NotABinaryExpression;
6158 }
6159 } else {
6160 ErrorLoc = AtomicBinOp->getExprLoc();
6161 ErrorRange = AtomicBinOp->getSourceRange();
6162 NoteLoc = AtomicBinOp->getOperatorLoc();
6163 NoteRange = SourceRange(NoteLoc, NoteLoc);
6164 ErrorFound = NotAnAssignmentOp;
6165 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006166 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006167 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6168 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6169 return true;
6170 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006171 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006172 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006173}
6174
6175bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6176 unsigned NoteId) {
6177 ExprAnalysisErrorCode ErrorFound = NoError;
6178 SourceLocation ErrorLoc, NoteLoc;
6179 SourceRange ErrorRange, NoteRange;
6180 // Allowed constructs are:
6181 // x++;
6182 // x--;
6183 // ++x;
6184 // --x;
6185 // x binop= expr;
6186 // x = x binop expr;
6187 // x = expr binop x;
6188 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6189 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6190 if (AtomicBody->getType()->isScalarType() ||
6191 AtomicBody->isInstantiationDependent()) {
6192 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6193 AtomicBody->IgnoreParenImpCasts())) {
6194 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006195 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006196 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006197 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006198 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006199 X = AtomicCompAssignOp->getLHS();
6200 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006201 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6202 AtomicBody->IgnoreParenImpCasts())) {
6203 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006204 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6205 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006206 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006207 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6208 // Check for Unary Operation
6209 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006210 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006211 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6212 OpLoc = AtomicUnaryOp->getOperatorLoc();
6213 X = AtomicUnaryOp->getSubExpr();
6214 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6215 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006216 } else {
6217 ErrorFound = NotAnUnaryIncDecExpression;
6218 ErrorLoc = AtomicUnaryOp->getExprLoc();
6219 ErrorRange = AtomicUnaryOp->getSourceRange();
6220 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6221 NoteRange = SourceRange(NoteLoc, NoteLoc);
6222 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006223 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006224 ErrorFound = NotABinaryOrUnaryExpression;
6225 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6226 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6227 }
6228 } else {
6229 ErrorFound = NotAScalarType;
6230 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6231 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6232 }
6233 } else {
6234 ErrorFound = NotAnExpression;
6235 NoteLoc = ErrorLoc = S->getLocStart();
6236 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6237 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006238 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006239 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6240 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6241 return true;
6242 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006243 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006244 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006245 // Build an update expression of form 'OpaqueValueExpr(x) binop
6246 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6247 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6248 auto *OVEX = new (SemaRef.getASTContext())
6249 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6250 auto *OVEExpr = new (SemaRef.getASTContext())
6251 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6252 auto Update =
6253 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6254 IsXLHSInRHSPart ? OVEExpr : OVEX);
6255 if (Update.isInvalid())
6256 return true;
6257 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6258 Sema::AA_Casting);
6259 if (Update.isInvalid())
6260 return true;
6261 UpdateExpr = Update.get();
6262 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006263 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006264}
6265
Alexey Bataev0162e452014-07-22 10:10:35 +00006266StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6267 Stmt *AStmt,
6268 SourceLocation StartLoc,
6269 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006270 if (!AStmt)
6271 return StmtError();
6272
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006273 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006274 // 1.2.2 OpenMP Language Terminology
6275 // Structured block - An executable statement with a single entry at the
6276 // top and a single exit at the bottom.
6277 // The point of exit cannot be a branch out of the structured block.
6278 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006279 OpenMPClauseKind AtomicKind = OMPC_unknown;
6280 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006281 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006282 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006283 C->getClauseKind() == OMPC_update ||
6284 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006285 if (AtomicKind != OMPC_unknown) {
6286 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6287 << SourceRange(C->getLocStart(), C->getLocEnd());
6288 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6289 << getOpenMPClauseName(AtomicKind);
6290 } else {
6291 AtomicKind = C->getClauseKind();
6292 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006293 }
6294 }
6295 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006296
Alexey Bataev459dec02014-07-24 06:46:57 +00006297 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006298 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6299 Body = EWC->getSubExpr();
6300
Alexey Bataev62cec442014-11-18 10:14:22 +00006301 Expr *X = nullptr;
6302 Expr *V = nullptr;
6303 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006304 Expr *UE = nullptr;
6305 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006306 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006307 // OpenMP [2.12.6, atomic Construct]
6308 // In the next expressions:
6309 // * x and v (as applicable) are both l-value expressions with scalar type.
6310 // * During the execution of an atomic region, multiple syntactic
6311 // occurrences of x must designate the same storage location.
6312 // * Neither of v and expr (as applicable) may access the storage location
6313 // designated by x.
6314 // * Neither of x and expr (as applicable) may access the storage location
6315 // designated by v.
6316 // * expr is an expression with scalar type.
6317 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6318 // * binop, binop=, ++, and -- are not overloaded operators.
6319 // * The expression x binop expr must be numerically equivalent to x binop
6320 // (expr). This requirement is satisfied if the operators in expr have
6321 // precedence greater than binop, or by using parentheses around expr or
6322 // subexpressions of expr.
6323 // * The expression expr binop x must be numerically equivalent to (expr)
6324 // binop x. This requirement is satisfied if the operators in expr have
6325 // precedence equal to or greater than binop, or by using parentheses around
6326 // expr or subexpressions of expr.
6327 // * For forms that allow multiple occurrences of x, the number of times
6328 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006329 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006330 enum {
6331 NotAnExpression,
6332 NotAnAssignmentOp,
6333 NotAScalarType,
6334 NotAnLValue,
6335 NoError
6336 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006337 SourceLocation ErrorLoc, NoteLoc;
6338 SourceRange ErrorRange, NoteRange;
6339 // If clause is read:
6340 // v = x;
6341 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6342 auto AtomicBinOp =
6343 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6344 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6345 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6346 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6347 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6348 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6349 if (!X->isLValue() || !V->isLValue()) {
6350 auto NotLValueExpr = X->isLValue() ? V : X;
6351 ErrorFound = NotAnLValue;
6352 ErrorLoc = AtomicBinOp->getExprLoc();
6353 ErrorRange = AtomicBinOp->getSourceRange();
6354 NoteLoc = NotLValueExpr->getExprLoc();
6355 NoteRange = NotLValueExpr->getSourceRange();
6356 }
6357 } else if (!X->isInstantiationDependent() ||
6358 !V->isInstantiationDependent()) {
6359 auto NotScalarExpr =
6360 (X->isInstantiationDependent() || X->getType()->isScalarType())
6361 ? V
6362 : X;
6363 ErrorFound = NotAScalarType;
6364 ErrorLoc = AtomicBinOp->getExprLoc();
6365 ErrorRange = AtomicBinOp->getSourceRange();
6366 NoteLoc = NotScalarExpr->getExprLoc();
6367 NoteRange = NotScalarExpr->getSourceRange();
6368 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006369 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006370 ErrorFound = NotAnAssignmentOp;
6371 ErrorLoc = AtomicBody->getExprLoc();
6372 ErrorRange = AtomicBody->getSourceRange();
6373 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6374 : AtomicBody->getExprLoc();
6375 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6376 : AtomicBody->getSourceRange();
6377 }
6378 } else {
6379 ErrorFound = NotAnExpression;
6380 NoteLoc = ErrorLoc = Body->getLocStart();
6381 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006382 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006383 if (ErrorFound != NoError) {
6384 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6385 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006386 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6387 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006388 return StmtError();
6389 } else if (CurContext->isDependentContext())
6390 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006391 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006392 enum {
6393 NotAnExpression,
6394 NotAnAssignmentOp,
6395 NotAScalarType,
6396 NotAnLValue,
6397 NoError
6398 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006399 SourceLocation ErrorLoc, NoteLoc;
6400 SourceRange ErrorRange, NoteRange;
6401 // If clause is write:
6402 // x = expr;
6403 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6404 auto AtomicBinOp =
6405 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6406 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006407 X = AtomicBinOp->getLHS();
6408 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006409 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6410 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6411 if (!X->isLValue()) {
6412 ErrorFound = NotAnLValue;
6413 ErrorLoc = AtomicBinOp->getExprLoc();
6414 ErrorRange = AtomicBinOp->getSourceRange();
6415 NoteLoc = X->getExprLoc();
6416 NoteRange = X->getSourceRange();
6417 }
6418 } else if (!X->isInstantiationDependent() ||
6419 !E->isInstantiationDependent()) {
6420 auto NotScalarExpr =
6421 (X->isInstantiationDependent() || X->getType()->isScalarType())
6422 ? E
6423 : X;
6424 ErrorFound = NotAScalarType;
6425 ErrorLoc = AtomicBinOp->getExprLoc();
6426 ErrorRange = AtomicBinOp->getSourceRange();
6427 NoteLoc = NotScalarExpr->getExprLoc();
6428 NoteRange = NotScalarExpr->getSourceRange();
6429 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006430 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006431 ErrorFound = NotAnAssignmentOp;
6432 ErrorLoc = AtomicBody->getExprLoc();
6433 ErrorRange = AtomicBody->getSourceRange();
6434 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6435 : AtomicBody->getExprLoc();
6436 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6437 : AtomicBody->getSourceRange();
6438 }
6439 } else {
6440 ErrorFound = NotAnExpression;
6441 NoteLoc = ErrorLoc = Body->getLocStart();
6442 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006443 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006444 if (ErrorFound != NoError) {
6445 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6446 << ErrorRange;
6447 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6448 << NoteRange;
6449 return StmtError();
6450 } else if (CurContext->isDependentContext())
6451 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006452 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006453 // If clause is update:
6454 // x++;
6455 // x--;
6456 // ++x;
6457 // --x;
6458 // x binop= expr;
6459 // x = x binop expr;
6460 // x = expr binop x;
6461 OpenMPAtomicUpdateChecker Checker(*this);
6462 if (Checker.checkStatement(
6463 Body, (AtomicKind == OMPC_update)
6464 ? diag::err_omp_atomic_update_not_expression_statement
6465 : diag::err_omp_atomic_not_expression_statement,
6466 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006467 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006468 if (!CurContext->isDependentContext()) {
6469 E = Checker.getExpr();
6470 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006471 UE = Checker.getUpdateExpr();
6472 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006473 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006474 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006475 enum {
6476 NotAnAssignmentOp,
6477 NotACompoundStatement,
6478 NotTwoSubstatements,
6479 NotASpecificExpression,
6480 NoError
6481 } ErrorFound = NoError;
6482 SourceLocation ErrorLoc, NoteLoc;
6483 SourceRange ErrorRange, NoteRange;
6484 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6485 // If clause is a capture:
6486 // v = x++;
6487 // v = x--;
6488 // v = ++x;
6489 // v = --x;
6490 // v = x binop= expr;
6491 // v = x = x binop expr;
6492 // v = x = expr binop x;
6493 auto *AtomicBinOp =
6494 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6495 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6496 V = AtomicBinOp->getLHS();
6497 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6498 OpenMPAtomicUpdateChecker Checker(*this);
6499 if (Checker.checkStatement(
6500 Body, diag::err_omp_atomic_capture_not_expression_statement,
6501 diag::note_omp_atomic_update))
6502 return StmtError();
6503 E = Checker.getExpr();
6504 X = Checker.getX();
6505 UE = Checker.getUpdateExpr();
6506 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6507 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006508 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006509 ErrorLoc = AtomicBody->getExprLoc();
6510 ErrorRange = AtomicBody->getSourceRange();
6511 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6512 : AtomicBody->getExprLoc();
6513 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6514 : AtomicBody->getSourceRange();
6515 ErrorFound = NotAnAssignmentOp;
6516 }
6517 if (ErrorFound != NoError) {
6518 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6519 << ErrorRange;
6520 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6521 return StmtError();
6522 } else if (CurContext->isDependentContext()) {
6523 UE = V = E = X = nullptr;
6524 }
6525 } else {
6526 // If clause is a capture:
6527 // { v = x; x = expr; }
6528 // { v = x; x++; }
6529 // { v = x; x--; }
6530 // { v = x; ++x; }
6531 // { v = x; --x; }
6532 // { v = x; x binop= expr; }
6533 // { v = x; x = x binop expr; }
6534 // { v = x; x = expr binop x; }
6535 // { x++; v = x; }
6536 // { x--; v = x; }
6537 // { ++x; v = x; }
6538 // { --x; v = x; }
6539 // { x binop= expr; v = x; }
6540 // { x = x binop expr; v = x; }
6541 // { x = expr binop x; v = x; }
6542 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6543 // Check that this is { expr1; expr2; }
6544 if (CS->size() == 2) {
6545 auto *First = CS->body_front();
6546 auto *Second = CS->body_back();
6547 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6548 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6549 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6550 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6551 // Need to find what subexpression is 'v' and what is 'x'.
6552 OpenMPAtomicUpdateChecker Checker(*this);
6553 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6554 BinaryOperator *BinOp = nullptr;
6555 if (IsUpdateExprFound) {
6556 BinOp = dyn_cast<BinaryOperator>(First);
6557 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6558 }
6559 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6560 // { v = x; x++; }
6561 // { v = x; x--; }
6562 // { v = x; ++x; }
6563 // { v = x; --x; }
6564 // { v = x; x binop= expr; }
6565 // { v = x; x = x binop expr; }
6566 // { v = x; x = expr binop x; }
6567 // Check that the first expression has form v = x.
6568 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6569 llvm::FoldingSetNodeID XId, PossibleXId;
6570 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6571 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6572 IsUpdateExprFound = XId == PossibleXId;
6573 if (IsUpdateExprFound) {
6574 V = BinOp->getLHS();
6575 X = Checker.getX();
6576 E = Checker.getExpr();
6577 UE = Checker.getUpdateExpr();
6578 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006579 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006580 }
6581 }
6582 if (!IsUpdateExprFound) {
6583 IsUpdateExprFound = !Checker.checkStatement(First);
6584 BinOp = nullptr;
6585 if (IsUpdateExprFound) {
6586 BinOp = dyn_cast<BinaryOperator>(Second);
6587 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6588 }
6589 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6590 // { x++; v = x; }
6591 // { x--; v = x; }
6592 // { ++x; v = x; }
6593 // { --x; v = x; }
6594 // { x binop= expr; v = x; }
6595 // { x = x binop expr; v = x; }
6596 // { x = expr binop x; v = x; }
6597 // Check that the second expression has form v = x.
6598 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6599 llvm::FoldingSetNodeID XId, PossibleXId;
6600 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6601 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6602 IsUpdateExprFound = XId == PossibleXId;
6603 if (IsUpdateExprFound) {
6604 V = BinOp->getLHS();
6605 X = Checker.getX();
6606 E = Checker.getExpr();
6607 UE = Checker.getUpdateExpr();
6608 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006609 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006610 }
6611 }
6612 }
6613 if (!IsUpdateExprFound) {
6614 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006615 auto *FirstExpr = dyn_cast<Expr>(First);
6616 auto *SecondExpr = dyn_cast<Expr>(Second);
6617 if (!FirstExpr || !SecondExpr ||
6618 !(FirstExpr->isInstantiationDependent() ||
6619 SecondExpr->isInstantiationDependent())) {
6620 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6621 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006622 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006623 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6624 : First->getLocStart();
6625 NoteRange = ErrorRange = FirstBinOp
6626 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006627 : SourceRange(ErrorLoc, ErrorLoc);
6628 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006629 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6630 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6631 ErrorFound = NotAnAssignmentOp;
6632 NoteLoc = ErrorLoc = SecondBinOp
6633 ? SecondBinOp->getOperatorLoc()
6634 : Second->getLocStart();
6635 NoteRange = ErrorRange =
6636 SecondBinOp ? SecondBinOp->getSourceRange()
6637 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006638 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006639 auto *PossibleXRHSInFirst =
6640 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6641 auto *PossibleXLHSInSecond =
6642 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6643 llvm::FoldingSetNodeID X1Id, X2Id;
6644 PossibleXRHSInFirst->Profile(X1Id, Context,
6645 /*Canonical=*/true);
6646 PossibleXLHSInSecond->Profile(X2Id, Context,
6647 /*Canonical=*/true);
6648 IsUpdateExprFound = X1Id == X2Id;
6649 if (IsUpdateExprFound) {
6650 V = FirstBinOp->getLHS();
6651 X = SecondBinOp->getLHS();
6652 E = SecondBinOp->getRHS();
6653 UE = nullptr;
6654 IsXLHSInRHSPart = false;
6655 IsPostfixUpdate = true;
6656 } else {
6657 ErrorFound = NotASpecificExpression;
6658 ErrorLoc = FirstBinOp->getExprLoc();
6659 ErrorRange = FirstBinOp->getSourceRange();
6660 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6661 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6662 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006663 }
6664 }
6665 }
6666 }
6667 } else {
6668 NoteLoc = ErrorLoc = Body->getLocStart();
6669 NoteRange = ErrorRange =
6670 SourceRange(Body->getLocStart(), Body->getLocStart());
6671 ErrorFound = NotTwoSubstatements;
6672 }
6673 } else {
6674 NoteLoc = ErrorLoc = Body->getLocStart();
6675 NoteRange = ErrorRange =
6676 SourceRange(Body->getLocStart(), Body->getLocStart());
6677 ErrorFound = NotACompoundStatement;
6678 }
6679 if (ErrorFound != NoError) {
6680 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6681 << ErrorRange;
6682 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6683 return StmtError();
6684 } else if (CurContext->isDependentContext()) {
6685 UE = V = E = X = nullptr;
6686 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006687 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006688 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006689
6690 getCurFunction()->setHasBranchProtectedScope();
6691
Alexey Bataev62cec442014-11-18 10:14:22 +00006692 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006693 X, V, E, UE, IsXLHSInRHSPart,
6694 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006695}
6696
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006697StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6698 Stmt *AStmt,
6699 SourceLocation StartLoc,
6700 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006701 if (!AStmt)
6702 return StmtError();
6703
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006704 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6705 // 1.2.2 OpenMP Language Terminology
6706 // Structured block - An executable statement with a single entry at the
6707 // top and a single exit at the bottom.
6708 // The point of exit cannot be a branch out of the structured block.
6709 // longjmp() and throw() must not violate the entry/exit criteria.
6710 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006711
Alexey Bataev13314bf2014-10-09 04:18:56 +00006712 // OpenMP [2.16, Nesting of Regions]
6713 // If specified, a teams construct must be contained within a target
6714 // construct. That target construct must contain no statements or directives
6715 // outside of the teams construct.
6716 if (DSAStack->hasInnerTeamsRegion()) {
6717 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6718 bool OMPTeamsFound = true;
6719 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6720 auto I = CS->body_begin();
6721 while (I != CS->body_end()) {
6722 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6723 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6724 OMPTeamsFound = false;
6725 break;
6726 }
6727 ++I;
6728 }
6729 assert(I != CS->body_end() && "Not found statement");
6730 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006731 } else {
6732 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6733 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006734 }
6735 if (!OMPTeamsFound) {
6736 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6737 Diag(DSAStack->getInnerTeamsRegionLoc(),
6738 diag::note_omp_nested_teams_construct_here);
6739 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6740 << isa<OMPExecutableDirective>(S);
6741 return StmtError();
6742 }
6743 }
6744
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006745 getCurFunction()->setHasBranchProtectedScope();
6746
6747 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6748}
6749
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006750StmtResult
6751Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6752 Stmt *AStmt, SourceLocation StartLoc,
6753 SourceLocation EndLoc) {
6754 if (!AStmt)
6755 return StmtError();
6756
6757 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6758 // 1.2.2 OpenMP Language Terminology
6759 // Structured block - An executable statement with a single entry at the
6760 // top and a single exit at the bottom.
6761 // The point of exit cannot be a branch out of the structured block.
6762 // longjmp() and throw() must not violate the entry/exit criteria.
6763 CS->getCapturedDecl()->setNothrow();
6764
6765 getCurFunction()->setHasBranchProtectedScope();
6766
6767 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6768 AStmt);
6769}
6770
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006771StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6772 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6773 SourceLocation EndLoc,
6774 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6775 if (!AStmt)
6776 return StmtError();
6777
6778 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6779 // 1.2.2 OpenMP Language Terminology
6780 // Structured block - An executable statement with a single entry at the
6781 // top and a single exit at the bottom.
6782 // The point of exit cannot be a branch out of the structured block.
6783 // longjmp() and throw() must not violate the entry/exit criteria.
6784 CS->getCapturedDecl()->setNothrow();
6785
6786 OMPLoopDirective::HelperExprs B;
6787 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6788 // define the nested loops number.
6789 unsigned NestedLoopCount =
6790 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6791 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6792 VarsWithImplicitDSA, B);
6793 if (NestedLoopCount == 0)
6794 return StmtError();
6795
6796 assert((CurContext->isDependentContext() || B.builtAll()) &&
6797 "omp target parallel for loop exprs were not built");
6798
6799 if (!CurContext->isDependentContext()) {
6800 // Finalize the clauses that need pre-built expressions for CodeGen.
6801 for (auto C : Clauses) {
6802 if (auto LC = dyn_cast<OMPLinearClause>(C))
6803 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006804 B.NumIterations, *this, CurScope,
6805 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006806 return StmtError();
6807 }
6808 }
6809
6810 getCurFunction()->setHasBranchProtectedScope();
6811 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6812 NestedLoopCount, Clauses, AStmt,
6813 B, DSAStack->isCancelRegion());
6814}
6815
Samuel Antaodf67fc42016-01-19 19:15:56 +00006816/// \brief Check for existence of a map clause in the list of clauses.
6817static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6818 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6819 I != E; ++I) {
6820 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6821 return true;
6822 }
6823 }
6824
6825 return false;
6826}
6827
Michael Wong65f367f2015-07-21 13:44:28 +00006828StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6829 Stmt *AStmt,
6830 SourceLocation StartLoc,
6831 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006832 if (!AStmt)
6833 return StmtError();
6834
6835 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6836
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006837 // OpenMP [2.10.1, Restrictions, p. 97]
6838 // At least one map clause must appear on the directive.
6839 if (!HasMapClause(Clauses)) {
6840 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6841 getOpenMPDirectiveName(OMPD_target_data);
6842 return StmtError();
6843 }
6844
Michael Wong65f367f2015-07-21 13:44:28 +00006845 getCurFunction()->setHasBranchProtectedScope();
6846
6847 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6848 AStmt);
6849}
6850
Samuel Antaodf67fc42016-01-19 19:15:56 +00006851StmtResult
6852Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6853 SourceLocation StartLoc,
6854 SourceLocation EndLoc) {
6855 // OpenMP [2.10.2, Restrictions, p. 99]
6856 // At least one map clause must appear on the directive.
6857 if (!HasMapClause(Clauses)) {
6858 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6859 << getOpenMPDirectiveName(OMPD_target_enter_data);
6860 return StmtError();
6861 }
6862
6863 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6864 Clauses);
6865}
6866
Samuel Antao72590762016-01-19 20:04:50 +00006867StmtResult
6868Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6869 SourceLocation StartLoc,
6870 SourceLocation EndLoc) {
6871 // OpenMP [2.10.3, Restrictions, p. 102]
6872 // At least one map clause must appear on the directive.
6873 if (!HasMapClause(Clauses)) {
6874 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6875 << getOpenMPDirectiveName(OMPD_target_exit_data);
6876 return StmtError();
6877 }
6878
6879 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6880}
6881
Samuel Antao686c70c2016-05-26 17:30:50 +00006882StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6883 SourceLocation StartLoc,
6884 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006885 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00006886 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00006887 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00006888 seenMotionClause = true;
6889 }
Samuel Antao686c70c2016-05-26 17:30:50 +00006890 if (!seenMotionClause) {
6891 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6892 return StmtError();
6893 }
6894 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6895}
6896
Alexey Bataev13314bf2014-10-09 04:18:56 +00006897StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6898 Stmt *AStmt, SourceLocation StartLoc,
6899 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006900 if (!AStmt)
6901 return StmtError();
6902
Alexey Bataev13314bf2014-10-09 04:18:56 +00006903 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6904 // 1.2.2 OpenMP Language Terminology
6905 // Structured block - An executable statement with a single entry at the
6906 // top and a single exit at the bottom.
6907 // The point of exit cannot be a branch out of the structured block.
6908 // longjmp() and throw() must not violate the entry/exit criteria.
6909 CS->getCapturedDecl()->setNothrow();
6910
6911 getCurFunction()->setHasBranchProtectedScope();
6912
6913 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6914}
6915
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006916StmtResult
6917Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6918 SourceLocation EndLoc,
6919 OpenMPDirectiveKind CancelRegion) {
6920 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6921 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6922 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6923 << getOpenMPDirectiveName(CancelRegion);
6924 return StmtError();
6925 }
6926 if (DSAStack->isParentNowaitRegion()) {
6927 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6928 return StmtError();
6929 }
6930 if (DSAStack->isParentOrderedRegion()) {
6931 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6932 return StmtError();
6933 }
6934 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6935 CancelRegion);
6936}
6937
Alexey Bataev87933c72015-09-18 08:07:34 +00006938StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6939 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006940 SourceLocation EndLoc,
6941 OpenMPDirectiveKind CancelRegion) {
6942 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6943 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6944 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6945 << getOpenMPDirectiveName(CancelRegion);
6946 return StmtError();
6947 }
6948 if (DSAStack->isParentNowaitRegion()) {
6949 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6950 return StmtError();
6951 }
6952 if (DSAStack->isParentOrderedRegion()) {
6953 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6954 return StmtError();
6955 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006956 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006957 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6958 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006959}
6960
Alexey Bataev382967a2015-12-08 12:06:20 +00006961static bool checkGrainsizeNumTasksClauses(Sema &S,
6962 ArrayRef<OMPClause *> Clauses) {
6963 OMPClause *PrevClause = nullptr;
6964 bool ErrorFound = false;
6965 for (auto *C : Clauses) {
6966 if (C->getClauseKind() == OMPC_grainsize ||
6967 C->getClauseKind() == OMPC_num_tasks) {
6968 if (!PrevClause)
6969 PrevClause = C;
6970 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6971 S.Diag(C->getLocStart(),
6972 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6973 << getOpenMPClauseName(C->getClauseKind())
6974 << getOpenMPClauseName(PrevClause->getClauseKind());
6975 S.Diag(PrevClause->getLocStart(),
6976 diag::note_omp_previous_grainsize_num_tasks)
6977 << getOpenMPClauseName(PrevClause->getClauseKind());
6978 ErrorFound = true;
6979 }
6980 }
6981 }
6982 return ErrorFound;
6983}
6984
Alexey Bataev49f6e782015-12-01 04:18:41 +00006985StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6986 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6987 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006988 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006989 if (!AStmt)
6990 return StmtError();
6991
6992 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6993 OMPLoopDirective::HelperExprs B;
6994 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6995 // define the nested loops number.
6996 unsigned NestedLoopCount =
6997 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006998 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006999 VarsWithImplicitDSA, B);
7000 if (NestedLoopCount == 0)
7001 return StmtError();
7002
7003 assert((CurContext->isDependentContext() || B.builtAll()) &&
7004 "omp for loop exprs were not built");
7005
Alexey Bataev382967a2015-12-08 12:06:20 +00007006 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7007 // The grainsize clause and num_tasks clause are mutually exclusive and may
7008 // not appear on the same taskloop directive.
7009 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7010 return StmtError();
7011
Alexey Bataev49f6e782015-12-01 04:18:41 +00007012 getCurFunction()->setHasBranchProtectedScope();
7013 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7014 NestedLoopCount, Clauses, AStmt, B);
7015}
7016
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007017StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7018 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7019 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007020 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007021 if (!AStmt)
7022 return StmtError();
7023
7024 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7025 OMPLoopDirective::HelperExprs B;
7026 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7027 // define the nested loops number.
7028 unsigned NestedLoopCount =
7029 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7030 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7031 VarsWithImplicitDSA, B);
7032 if (NestedLoopCount == 0)
7033 return StmtError();
7034
7035 assert((CurContext->isDependentContext() || B.builtAll()) &&
7036 "omp for loop exprs were not built");
7037
Alexey Bataev5a3af132016-03-29 08:58:54 +00007038 if (!CurContext->isDependentContext()) {
7039 // Finalize the clauses that need pre-built expressions for CodeGen.
7040 for (auto C : Clauses) {
7041 if (auto LC = dyn_cast<OMPLinearClause>(C))
7042 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007043 B.NumIterations, *this, CurScope,
7044 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007045 return StmtError();
7046 }
7047 }
7048
Alexey Bataev382967a2015-12-08 12:06:20 +00007049 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7050 // The grainsize clause and num_tasks clause are mutually exclusive and may
7051 // not appear on the same taskloop directive.
7052 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7053 return StmtError();
7054
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007055 getCurFunction()->setHasBranchProtectedScope();
7056 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7057 NestedLoopCount, Clauses, AStmt, B);
7058}
7059
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007060StmtResult Sema::ActOnOpenMPDistributeDirective(
7061 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7062 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007063 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007064 if (!AStmt)
7065 return StmtError();
7066
7067 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7068 OMPLoopDirective::HelperExprs B;
7069 // In presence of clause 'collapse' with number of loops, it will
7070 // define the nested loops number.
7071 unsigned NestedLoopCount =
7072 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7073 nullptr /*ordered not a clause on distribute*/, AStmt,
7074 *this, *DSAStack, VarsWithImplicitDSA, B);
7075 if (NestedLoopCount == 0)
7076 return StmtError();
7077
7078 assert((CurContext->isDependentContext() || B.builtAll()) &&
7079 "omp for loop exprs were not built");
7080
7081 getCurFunction()->setHasBranchProtectedScope();
7082 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7083 NestedLoopCount, Clauses, AStmt, B);
7084}
7085
Carlo Bertolli9925f152016-06-27 14:55:37 +00007086StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7087 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7088 SourceLocation EndLoc,
7089 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7090 if (!AStmt)
7091 return StmtError();
7092
7093 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7094 // 1.2.2 OpenMP Language Terminology
7095 // Structured block - An executable statement with a single entry at the
7096 // top and a single exit at the bottom.
7097 // The point of exit cannot be a branch out of the structured block.
7098 // longjmp() and throw() must not violate the entry/exit criteria.
7099 CS->getCapturedDecl()->setNothrow();
7100
7101 OMPLoopDirective::HelperExprs B;
7102 // In presence of clause 'collapse' with number of loops, it will
7103 // define the nested loops number.
7104 unsigned NestedLoopCount = CheckOpenMPLoop(
7105 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7106 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7107 VarsWithImplicitDSA, B);
7108 if (NestedLoopCount == 0)
7109 return StmtError();
7110
7111 assert((CurContext->isDependentContext() || B.builtAll()) &&
7112 "omp for loop exprs were not built");
7113
7114 getCurFunction()->setHasBranchProtectedScope();
7115 return OMPDistributeParallelForDirective::Create(
7116 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7117}
7118
Kelvin Li4a39add2016-07-05 05:00:15 +00007119StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7120 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7121 SourceLocation EndLoc,
7122 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7123 if (!AStmt)
7124 return StmtError();
7125
7126 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7127 // 1.2.2 OpenMP Language Terminology
7128 // Structured block - An executable statement with a single entry at the
7129 // top and a single exit at the bottom.
7130 // The point of exit cannot be a branch out of the structured block.
7131 // longjmp() and throw() must not violate the entry/exit criteria.
7132 CS->getCapturedDecl()->setNothrow();
7133
7134 OMPLoopDirective::HelperExprs B;
7135 // In presence of clause 'collapse' with number of loops, it will
7136 // define the nested loops number.
7137 unsigned NestedLoopCount = CheckOpenMPLoop(
7138 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7139 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7140 VarsWithImplicitDSA, B);
7141 if (NestedLoopCount == 0)
7142 return StmtError();
7143
7144 assert((CurContext->isDependentContext() || B.builtAll()) &&
7145 "omp for loop exprs were not built");
7146
7147 getCurFunction()->setHasBranchProtectedScope();
7148 return OMPDistributeParallelForSimdDirective::Create(
7149 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7150}
7151
Kelvin Li787f3fc2016-07-06 04:45:38 +00007152StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7153 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7154 SourceLocation EndLoc,
7155 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7156 if (!AStmt)
7157 return StmtError();
7158
7159 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7160 // 1.2.2 OpenMP Language Terminology
7161 // Structured block - An executable statement with a single entry at the
7162 // top and a single exit at the bottom.
7163 // The point of exit cannot be a branch out of the structured block.
7164 // longjmp() and throw() must not violate the entry/exit criteria.
7165 CS->getCapturedDecl()->setNothrow();
7166
7167 OMPLoopDirective::HelperExprs B;
7168 // In presence of clause 'collapse' with number of loops, it will
7169 // define the nested loops number.
7170 unsigned NestedLoopCount =
7171 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7172 nullptr /*ordered not a clause on distribute*/, AStmt,
7173 *this, *DSAStack, VarsWithImplicitDSA, B);
7174 if (NestedLoopCount == 0)
7175 return StmtError();
7176
7177 assert((CurContext->isDependentContext() || B.builtAll()) &&
7178 "omp for loop exprs were not built");
7179
7180 getCurFunction()->setHasBranchProtectedScope();
7181 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7182 NestedLoopCount, Clauses, AStmt, B);
7183}
7184
Kelvin Lia579b912016-07-14 02:54:56 +00007185StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7186 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7187 SourceLocation EndLoc,
7188 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7189 if (!AStmt)
7190 return StmtError();
7191
7192 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7193 // 1.2.2 OpenMP Language Terminology
7194 // Structured block - An executable statement with a single entry at the
7195 // top and a single exit at the bottom.
7196 // The point of exit cannot be a branch out of the structured block.
7197 // longjmp() and throw() must not violate the entry/exit criteria.
7198 CS->getCapturedDecl()->setNothrow();
7199
7200 OMPLoopDirective::HelperExprs B;
7201 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7202 // define the nested loops number.
7203 unsigned NestedLoopCount = CheckOpenMPLoop(
7204 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7205 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7206 VarsWithImplicitDSA, B);
7207 if (NestedLoopCount == 0)
7208 return StmtError();
7209
7210 assert((CurContext->isDependentContext() || B.builtAll()) &&
7211 "omp target parallel for simd loop exprs were not built");
7212
7213 if (!CurContext->isDependentContext()) {
7214 // Finalize the clauses that need pre-built expressions for CodeGen.
7215 for (auto C : Clauses) {
7216 if (auto LC = dyn_cast<OMPLinearClause>(C))
7217 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7218 B.NumIterations, *this, CurScope,
7219 DSAStack))
7220 return StmtError();
7221 }
7222 }
7223
7224 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
7225 // If both simdlen and safelen clauses are specified, the value of the simdlen
7226 // parameter must be less than or equal to the value of the safelen parameter.
7227 OMPSafelenClause *Safelen = nullptr;
7228 OMPSimdlenClause *Simdlen = nullptr;
7229 for (auto *Clause : Clauses) {
7230 if (Clause->getClauseKind() == OMPC_safelen)
7231 Safelen = cast<OMPSafelenClause>(Clause);
7232 else if (Clause->getClauseKind() == OMPC_simdlen)
7233 Simdlen = cast<OMPSimdlenClause>(Clause);
7234 if (Safelen && Simdlen)
7235 break;
7236 }
7237 if (Simdlen && Safelen &&
7238 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
7239 Safelen->getSafelen()))
7240 return StmtError();
7241
7242 getCurFunction()->setHasBranchProtectedScope();
7243 return OMPTargetParallelForSimdDirective::Create(
7244 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7245}
7246
Alexey Bataeved09d242014-05-28 05:53:51 +00007247OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007248 SourceLocation StartLoc,
7249 SourceLocation LParenLoc,
7250 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007251 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007252 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007253 case OMPC_final:
7254 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7255 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007256 case OMPC_num_threads:
7257 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7258 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007259 case OMPC_safelen:
7260 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7261 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007262 case OMPC_simdlen:
7263 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7264 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007265 case OMPC_collapse:
7266 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7267 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007268 case OMPC_ordered:
7269 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7270 break;
Michael Wonge710d542015-08-07 16:16:36 +00007271 case OMPC_device:
7272 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7273 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007274 case OMPC_num_teams:
7275 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7276 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007277 case OMPC_thread_limit:
7278 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7279 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007280 case OMPC_priority:
7281 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7282 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007283 case OMPC_grainsize:
7284 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7285 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007286 case OMPC_num_tasks:
7287 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7288 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007289 case OMPC_hint:
7290 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7291 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007292 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007293 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007294 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007295 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007296 case OMPC_private:
7297 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007298 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007299 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007300 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007301 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007302 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007303 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007304 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007305 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007306 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007307 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007308 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007309 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007310 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007311 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007312 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007313 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007314 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007315 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007316 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007317 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007318 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007319 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007320 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007321 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007322 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007323 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007324 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007325 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007326 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007327 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007328 llvm_unreachable("Clause is not allowed.");
7329 }
7330 return Res;
7331}
7332
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007333OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7334 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007335 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007336 SourceLocation NameModifierLoc,
7337 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007338 SourceLocation EndLoc) {
7339 Expr *ValExpr = Condition;
7340 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7341 !Condition->isInstantiationDependent() &&
7342 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007343 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007344 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007345 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007346
Richard Smith03a4aa32016-06-23 19:02:52 +00007347 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007348 }
7349
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007350 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7351 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007352}
7353
Alexey Bataev3778b602014-07-17 07:32:53 +00007354OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7355 SourceLocation StartLoc,
7356 SourceLocation LParenLoc,
7357 SourceLocation EndLoc) {
7358 Expr *ValExpr = Condition;
7359 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7360 !Condition->isInstantiationDependent() &&
7361 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007362 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007363 if (Val.isInvalid())
7364 return nullptr;
7365
Richard Smith03a4aa32016-06-23 19:02:52 +00007366 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007367 }
7368
7369 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7370}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007371ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7372 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007373 if (!Op)
7374 return ExprError();
7375
7376 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7377 public:
7378 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007379 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007380 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7381 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007382 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7383 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007384 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7385 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007386 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7387 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007388 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7389 QualType T,
7390 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007391 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7392 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007393 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7394 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007395 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007396 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007397 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007398 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7399 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007400 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7401 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007402 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7403 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007404 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007405 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007406 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007407 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7408 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007409 llvm_unreachable("conversion functions are permitted");
7410 }
7411 } ConvertDiagnoser;
7412 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7413}
7414
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007415static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007416 OpenMPClauseKind CKind,
7417 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007418 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7419 !ValExpr->isInstantiationDependent()) {
7420 SourceLocation Loc = ValExpr->getExprLoc();
7421 ExprResult Value =
7422 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7423 if (Value.isInvalid())
7424 return false;
7425
7426 ValExpr = Value.get();
7427 // The expression must evaluate to a non-negative integer value.
7428 llvm::APSInt Result;
7429 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007430 Result.isSigned() &&
7431 !((!StrictlyPositive && Result.isNonNegative()) ||
7432 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007433 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007434 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7435 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007436 return false;
7437 }
7438 }
7439 return true;
7440}
7441
Alexey Bataev568a8332014-03-06 06:15:19 +00007442OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7443 SourceLocation StartLoc,
7444 SourceLocation LParenLoc,
7445 SourceLocation EndLoc) {
7446 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007447
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007448 // OpenMP [2.5, Restrictions]
7449 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007450 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7451 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007452 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007453
Alexey Bataeved09d242014-05-28 05:53:51 +00007454 return new (Context)
7455 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007456}
7457
Alexey Bataev62c87d22014-03-21 04:51:18 +00007458ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007459 OpenMPClauseKind CKind,
7460 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007461 if (!E)
7462 return ExprError();
7463 if (E->isValueDependent() || E->isTypeDependent() ||
7464 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007465 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007466 llvm::APSInt Result;
7467 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7468 if (ICE.isInvalid())
7469 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007470 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7471 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007472 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007473 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7474 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007475 return ExprError();
7476 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007477 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7478 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7479 << E->getSourceRange();
7480 return ExprError();
7481 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007482 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7483 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007484 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007485 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007486 return ICE;
7487}
7488
7489OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7490 SourceLocation LParenLoc,
7491 SourceLocation EndLoc) {
7492 // OpenMP [2.8.1, simd construct, Description]
7493 // The parameter of the safelen clause must be a constant
7494 // positive integer expression.
7495 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7496 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007497 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007498 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007499 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007500}
7501
Alexey Bataev66b15b52015-08-21 11:14:16 +00007502OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7503 SourceLocation LParenLoc,
7504 SourceLocation EndLoc) {
7505 // OpenMP [2.8.1, simd construct, Description]
7506 // The parameter of the simdlen clause must be a constant
7507 // positive integer expression.
7508 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7509 if (Simdlen.isInvalid())
7510 return nullptr;
7511 return new (Context)
7512 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7513}
7514
Alexander Musman64d33f12014-06-04 07:53:32 +00007515OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7516 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007517 SourceLocation LParenLoc,
7518 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007519 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007520 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007521 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007522 // The parameter of the collapse clause must be a constant
7523 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007524 ExprResult NumForLoopsResult =
7525 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7526 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007527 return nullptr;
7528 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007529 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007530}
7531
Alexey Bataev10e775f2015-07-30 11:36:16 +00007532OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7533 SourceLocation EndLoc,
7534 SourceLocation LParenLoc,
7535 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007536 // OpenMP [2.7.1, loop construct, Description]
7537 // OpenMP [2.8.1, simd construct, Description]
7538 // OpenMP [2.9.6, distribute construct, Description]
7539 // The parameter of the ordered clause must be a constant
7540 // positive integer expression if any.
7541 if (NumForLoops && LParenLoc.isValid()) {
7542 ExprResult NumForLoopsResult =
7543 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7544 if (NumForLoopsResult.isInvalid())
7545 return nullptr;
7546 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007547 } else
7548 NumForLoops = nullptr;
7549 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007550 return new (Context)
7551 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7552}
7553
Alexey Bataeved09d242014-05-28 05:53:51 +00007554OMPClause *Sema::ActOnOpenMPSimpleClause(
7555 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7556 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007557 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007558 switch (Kind) {
7559 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007560 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007561 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7562 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007563 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007564 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007565 Res = ActOnOpenMPProcBindClause(
7566 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7567 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007568 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007569 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007570 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007571 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007572 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007573 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007574 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007575 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007576 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007577 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007578 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007579 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007580 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007581 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007582 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007583 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007584 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007585 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007586 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007587 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007588 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007589 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007590 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007591 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007592 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007593 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007594 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007595 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007596 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007597 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007598 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007599 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007600 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007601 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007602 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007603 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007604 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007605 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007606 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007607 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007608 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007609 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007610 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007611 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007612 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007613 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007614 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007615 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007616 llvm_unreachable("Clause is not allowed.");
7617 }
7618 return Res;
7619}
7620
Alexey Bataev6402bca2015-12-28 07:25:51 +00007621static std::string
7622getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7623 ArrayRef<unsigned> Exclude = llvm::None) {
7624 std::string Values;
7625 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7626 unsigned Skipped = Exclude.size();
7627 auto S = Exclude.begin(), E = Exclude.end();
7628 for (unsigned i = First; i < Last; ++i) {
7629 if (std::find(S, E, i) != E) {
7630 --Skipped;
7631 continue;
7632 }
7633 Values += "'";
7634 Values += getOpenMPSimpleClauseTypeName(K, i);
7635 Values += "'";
7636 if (i == Bound - Skipped)
7637 Values += " or ";
7638 else if (i != Bound + 1 - Skipped)
7639 Values += ", ";
7640 }
7641 return Values;
7642}
7643
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007644OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7645 SourceLocation KindKwLoc,
7646 SourceLocation StartLoc,
7647 SourceLocation LParenLoc,
7648 SourceLocation EndLoc) {
7649 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007650 static_assert(OMPC_DEFAULT_unknown > 0,
7651 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007652 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007653 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7654 /*Last=*/OMPC_DEFAULT_unknown)
7655 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007656 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007657 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007658 switch (Kind) {
7659 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007660 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007661 break;
7662 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007663 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007664 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007665 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007666 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007667 break;
7668 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007669 return new (Context)
7670 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007671}
7672
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007673OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7674 SourceLocation KindKwLoc,
7675 SourceLocation StartLoc,
7676 SourceLocation LParenLoc,
7677 SourceLocation EndLoc) {
7678 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007679 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007680 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7681 /*Last=*/OMPC_PROC_BIND_unknown)
7682 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007683 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007684 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007685 return new (Context)
7686 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007687}
7688
Alexey Bataev56dafe82014-06-20 07:16:17 +00007689OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007690 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007691 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007692 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007693 SourceLocation EndLoc) {
7694 OMPClause *Res = nullptr;
7695 switch (Kind) {
7696 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007697 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7698 assert(Argument.size() == NumberOfElements &&
7699 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007700 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007701 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7702 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7703 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7704 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7705 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007706 break;
7707 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007708 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7709 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7710 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7711 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007712 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007713 case OMPC_dist_schedule:
7714 Res = ActOnOpenMPDistScheduleClause(
7715 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7716 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7717 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007718 case OMPC_defaultmap:
7719 enum { Modifier, DefaultmapKind };
7720 Res = ActOnOpenMPDefaultmapClause(
7721 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7722 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7723 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7724 ArgumentLoc[DefaultmapKind], EndLoc);
7725 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007726 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007727 case OMPC_num_threads:
7728 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007729 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007730 case OMPC_collapse:
7731 case OMPC_default:
7732 case OMPC_proc_bind:
7733 case OMPC_private:
7734 case OMPC_firstprivate:
7735 case OMPC_lastprivate:
7736 case OMPC_shared:
7737 case OMPC_reduction:
7738 case OMPC_linear:
7739 case OMPC_aligned:
7740 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007741 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007742 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007743 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007744 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007745 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007746 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007747 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007748 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007749 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007750 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007751 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007752 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007753 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007754 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007755 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007756 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007757 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007758 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007759 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007760 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007761 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007762 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007763 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007764 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007765 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007766 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007767 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007768 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007769 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007770 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007771 llvm_unreachable("Clause is not allowed.");
7772 }
7773 return Res;
7774}
7775
Alexey Bataev6402bca2015-12-28 07:25:51 +00007776static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7777 OpenMPScheduleClauseModifier M2,
7778 SourceLocation M1Loc, SourceLocation M2Loc) {
7779 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7780 SmallVector<unsigned, 2> Excluded;
7781 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7782 Excluded.push_back(M2);
7783 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7784 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7785 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7786 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7787 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7788 << getListOfPossibleValues(OMPC_schedule,
7789 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7790 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7791 Excluded)
7792 << getOpenMPClauseName(OMPC_schedule);
7793 return true;
7794 }
7795 return false;
7796}
7797
Alexey Bataev56dafe82014-06-20 07:16:17 +00007798OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007799 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007800 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007801 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7802 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7803 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7804 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7805 return nullptr;
7806 // OpenMP, 2.7.1, Loop Construct, Restrictions
7807 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7808 // but not both.
7809 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7810 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7811 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7812 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7813 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7814 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7815 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7816 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7817 return nullptr;
7818 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007819 if (Kind == OMPC_SCHEDULE_unknown) {
7820 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007821 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7822 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7823 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7824 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7825 Exclude);
7826 } else {
7827 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7828 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007829 }
7830 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7831 << Values << getOpenMPClauseName(OMPC_schedule);
7832 return nullptr;
7833 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007834 // OpenMP, 2.7.1, Loop Construct, Restrictions
7835 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7836 // schedule(guided).
7837 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7838 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7839 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7840 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7841 diag::err_omp_schedule_nonmonotonic_static);
7842 return nullptr;
7843 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007844 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007845 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007846 if (ChunkSize) {
7847 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7848 !ChunkSize->isInstantiationDependent() &&
7849 !ChunkSize->containsUnexpandedParameterPack()) {
7850 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7851 ExprResult Val =
7852 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7853 if (Val.isInvalid())
7854 return nullptr;
7855
7856 ValExpr = Val.get();
7857
7858 // OpenMP [2.7.1, Restrictions]
7859 // chunk_size must be a loop invariant integer expression with a positive
7860 // value.
7861 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007862 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7863 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7864 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007865 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007866 return nullptr;
7867 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007868 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7869 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007870 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7871 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7872 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007873 }
7874 }
7875 }
7876
Alexey Bataev6402bca2015-12-28 07:25:51 +00007877 return new (Context)
7878 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007879 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007880}
7881
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007882OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7883 SourceLocation StartLoc,
7884 SourceLocation EndLoc) {
7885 OMPClause *Res = nullptr;
7886 switch (Kind) {
7887 case OMPC_ordered:
7888 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7889 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007890 case OMPC_nowait:
7891 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7892 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007893 case OMPC_untied:
7894 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7895 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007896 case OMPC_mergeable:
7897 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7898 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007899 case OMPC_read:
7900 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7901 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007902 case OMPC_write:
7903 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7904 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007905 case OMPC_update:
7906 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7907 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007908 case OMPC_capture:
7909 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7910 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007911 case OMPC_seq_cst:
7912 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7913 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007914 case OMPC_threads:
7915 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7916 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007917 case OMPC_simd:
7918 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7919 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007920 case OMPC_nogroup:
7921 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7922 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007923 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007924 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007925 case OMPC_num_threads:
7926 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007927 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007928 case OMPC_collapse:
7929 case OMPC_schedule:
7930 case OMPC_private:
7931 case OMPC_firstprivate:
7932 case OMPC_lastprivate:
7933 case OMPC_shared:
7934 case OMPC_reduction:
7935 case OMPC_linear:
7936 case OMPC_aligned:
7937 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007938 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007939 case OMPC_default:
7940 case OMPC_proc_bind:
7941 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007942 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007943 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007944 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007945 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007946 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007947 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007948 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007949 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007950 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007951 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007952 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007953 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007954 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007955 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007956 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007957 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007958 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007959 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007960 llvm_unreachable("Clause is not allowed.");
7961 }
7962 return Res;
7963}
7964
Alexey Bataev236070f2014-06-20 11:19:47 +00007965OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7966 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007967 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007968 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7969}
7970
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007971OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7972 SourceLocation EndLoc) {
7973 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7974}
7975
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007976OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7977 SourceLocation EndLoc) {
7978 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7979}
7980
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007981OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7982 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007983 return new (Context) OMPReadClause(StartLoc, EndLoc);
7984}
7985
Alexey Bataevdea47612014-07-23 07:46:59 +00007986OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7987 SourceLocation EndLoc) {
7988 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7989}
7990
Alexey Bataev67a4f222014-07-23 10:25:33 +00007991OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7992 SourceLocation EndLoc) {
7993 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7994}
7995
Alexey Bataev459dec02014-07-24 06:46:57 +00007996OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7997 SourceLocation EndLoc) {
7998 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7999}
8000
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008001OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8002 SourceLocation EndLoc) {
8003 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8004}
8005
Alexey Bataev346265e2015-09-25 10:37:12 +00008006OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8007 SourceLocation EndLoc) {
8008 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8009}
8010
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008011OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8012 SourceLocation EndLoc) {
8013 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8014}
8015
Alexey Bataevb825de12015-12-07 10:51:44 +00008016OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8017 SourceLocation EndLoc) {
8018 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8019}
8020
Alexey Bataevc5e02582014-06-16 07:08:35 +00008021OMPClause *Sema::ActOnOpenMPVarListClause(
8022 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8023 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8024 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008025 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00008026 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8027 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8028 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008029 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008030 switch (Kind) {
8031 case OMPC_private:
8032 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8033 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008034 case OMPC_firstprivate:
8035 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8036 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008037 case OMPC_lastprivate:
8038 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8039 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008040 case OMPC_shared:
8041 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8042 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008043 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008044 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8045 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008046 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008047 case OMPC_linear:
8048 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008049 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008050 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008051 case OMPC_aligned:
8052 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8053 ColonLoc, EndLoc);
8054 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008055 case OMPC_copyin:
8056 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8057 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008058 case OMPC_copyprivate:
8059 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8060 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008061 case OMPC_flush:
8062 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8063 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008064 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008065 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8066 StartLoc, LParenLoc, EndLoc);
8067 break;
8068 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008069 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8070 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8071 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008072 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008073 case OMPC_to:
8074 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8075 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008076 case OMPC_from:
8077 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8078 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008079 case OMPC_use_device_ptr:
8080 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8081 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008082 case OMPC_is_device_ptr:
8083 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8084 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008085 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008086 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008087 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008088 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008089 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008090 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008091 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008092 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008093 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008094 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008095 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008096 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008097 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008098 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008099 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008100 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008101 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008102 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008103 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008104 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008105 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008106 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008107 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008108 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008109 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008110 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008111 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008112 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008113 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008114 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008115 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008116 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008117 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008118 llvm_unreachable("Clause is not allowed.");
8119 }
8120 return Res;
8121}
8122
Alexey Bataev90c228f2016-02-08 09:29:13 +00008123ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008124 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008125 ExprResult Res = BuildDeclRefExpr(
8126 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8127 if (!Res.isUsable())
8128 return ExprError();
8129 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8130 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8131 if (!Res.isUsable())
8132 return ExprError();
8133 }
8134 if (VK != VK_LValue && Res.get()->isGLValue()) {
8135 Res = DefaultLvalueConversion(Res.get());
8136 if (!Res.isUsable())
8137 return ExprError();
8138 }
8139 return Res;
8140}
8141
Alexey Bataev60da77e2016-02-29 05:54:20 +00008142static std::pair<ValueDecl *, bool>
8143getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8144 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008145 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8146 RefExpr->containsUnexpandedParameterPack())
8147 return std::make_pair(nullptr, true);
8148
Alexey Bataevd985eda2016-02-10 11:29:16 +00008149 // OpenMP [3.1, C/C++]
8150 // A list item is a variable name.
8151 // OpenMP [2.9.3.3, Restrictions, p.1]
8152 // A variable that is part of another variable (as an array or
8153 // structure element) cannot appear in a private clause.
8154 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008155 enum {
8156 NoArrayExpr = -1,
8157 ArraySubscript = 0,
8158 OMPArraySection = 1
8159 } IsArrayExpr = NoArrayExpr;
8160 if (AllowArraySection) {
8161 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8162 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8163 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8164 Base = TempASE->getBase()->IgnoreParenImpCasts();
8165 RefExpr = Base;
8166 IsArrayExpr = ArraySubscript;
8167 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8168 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8169 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8170 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8171 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8172 Base = TempASE->getBase()->IgnoreParenImpCasts();
8173 RefExpr = Base;
8174 IsArrayExpr = OMPArraySection;
8175 }
8176 }
8177 ELoc = RefExpr->getExprLoc();
8178 ERange = RefExpr->getSourceRange();
8179 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008180 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8181 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8182 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8183 (S.getCurrentThisType().isNull() || !ME ||
8184 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8185 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008186 if (IsArrayExpr != NoArrayExpr)
8187 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8188 << ERange;
8189 else {
8190 S.Diag(ELoc,
8191 AllowArraySection
8192 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8193 : diag::err_omp_expected_var_name_member_expr)
8194 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8195 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008196 return std::make_pair(nullptr, false);
8197 }
8198 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8199}
8200
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008201OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8202 SourceLocation StartLoc,
8203 SourceLocation LParenLoc,
8204 SourceLocation EndLoc) {
8205 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008206 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008207 for (auto &RefExpr : VarList) {
8208 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008209 SourceLocation ELoc;
8210 SourceRange ERange;
8211 Expr *SimpleRefExpr = RefExpr;
8212 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008213 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008214 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008215 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008216 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008217 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008218 ValueDecl *D = Res.first;
8219 if (!D)
8220 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008221
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008222 QualType Type = D->getType();
8223 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008224
8225 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8226 // A variable that appears in a private clause must not have an incomplete
8227 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008228 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008229 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008230 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008231
Alexey Bataev758e55e2013-09-06 18:03:48 +00008232 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8233 // in a Construct]
8234 // Variables with the predetermined data-sharing attributes may not be
8235 // listed in data-sharing attributes clauses, except for the cases
8236 // listed below. For these exceptions only, listing a predetermined
8237 // variable in a data-sharing attribute clause is allowed and overrides
8238 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008239 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008240 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008241 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8242 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008243 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008244 continue;
8245 }
8246
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008247 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008248 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008249 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008250 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8251 << getOpenMPClauseName(OMPC_private) << Type
8252 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8253 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008254 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008255 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008256 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008257 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008258 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008259 continue;
8260 }
8261
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008262 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8263 // A list item cannot appear in both a map clause and a data-sharing
8264 // attribute clause on the same construct
8265 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008266 if (DSAStack->checkMappableExprComponentListsForDecl(
8267 VD, /* CurrentRegionOnly = */ true,
8268 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8269 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008270 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8271 << getOpenMPClauseName(OMPC_private)
8272 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8273 ReportOriginalDSA(*this, DSAStack, D, DVar);
8274 continue;
8275 }
8276 }
8277
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008278 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8279 // A variable of class type (or array thereof) that appears in a private
8280 // clause requires an accessible, unambiguous default constructor for the
8281 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008282 // Generate helper private variable and initialize it with the default
8283 // value. The address of the original variable is replaced by the address of
8284 // the new private variable in CodeGen. This new variable is not added to
8285 // IdResolver, so the code in the OpenMP region uses original variable for
8286 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008287 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008288 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8289 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008290 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008291 if (VDPrivate->isInvalidDecl())
8292 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008293 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008294 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008295
Alexey Bataev90c228f2016-02-08 09:29:13 +00008296 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008297 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008298 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008299 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008300 Vars.push_back((VD || CurContext->isDependentContext())
8301 ? RefExpr->IgnoreParens()
8302 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008303 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008304 }
8305
Alexey Bataeved09d242014-05-28 05:53:51 +00008306 if (Vars.empty())
8307 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008308
Alexey Bataev03b340a2014-10-21 03:16:40 +00008309 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8310 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008311}
8312
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008313namespace {
8314class DiagsUninitializedSeveretyRAII {
8315private:
8316 DiagnosticsEngine &Diags;
8317 SourceLocation SavedLoc;
8318 bool IsIgnored;
8319
8320public:
8321 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8322 bool IsIgnored)
8323 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8324 if (!IsIgnored) {
8325 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8326 /*Map*/ diag::Severity::Ignored, Loc);
8327 }
8328 }
8329 ~DiagsUninitializedSeveretyRAII() {
8330 if (!IsIgnored)
8331 Diags.popMappings(SavedLoc);
8332 }
8333};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008334}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008335
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008336OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8337 SourceLocation StartLoc,
8338 SourceLocation LParenLoc,
8339 SourceLocation EndLoc) {
8340 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008341 SmallVector<Expr *, 8> PrivateCopies;
8342 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008343 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008344 bool IsImplicitClause =
8345 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8346 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8347
Alexey Bataeved09d242014-05-28 05:53:51 +00008348 for (auto &RefExpr : VarList) {
8349 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008350 SourceLocation ELoc;
8351 SourceRange ERange;
8352 Expr *SimpleRefExpr = RefExpr;
8353 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008354 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008355 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008356 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008357 PrivateCopies.push_back(nullptr);
8358 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008359 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008360 ValueDecl *D = Res.first;
8361 if (!D)
8362 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008363
Alexey Bataev60da77e2016-02-29 05:54:20 +00008364 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008365 QualType Type = D->getType();
8366 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008367
8368 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8369 // A variable that appears in a private clause must not have an incomplete
8370 // type or a reference type.
8371 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008372 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008373 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008374 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008375
8376 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8377 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008378 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008379 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008380 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008381
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008382 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008383 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008384 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008385 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008386 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008387 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008388 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8389 // A list item that specifies a given variable may not appear in more
8390 // than one clause on the same directive, except that a variable may be
8391 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008392 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008393 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008394 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008395 << getOpenMPClauseName(DVar.CKind)
8396 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008397 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008398 continue;
8399 }
8400
8401 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8402 // in a Construct]
8403 // Variables with the predetermined data-sharing attributes may not be
8404 // listed in data-sharing attributes clauses, except for the cases
8405 // listed below. For these exceptions only, listing a predetermined
8406 // variable in a data-sharing attribute clause is allowed and overrides
8407 // the variable's predetermined data-sharing attributes.
8408 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8409 // in a Construct, C/C++, p.2]
8410 // Variables with const-qualified type having no mutable member may be
8411 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008412 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008413 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8414 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008415 << getOpenMPClauseName(DVar.CKind)
8416 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008417 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008418 continue;
8419 }
8420
Alexey Bataevf29276e2014-06-18 04:14:57 +00008421 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008422 // OpenMP [2.9.3.4, Restrictions, p.2]
8423 // A list item that is private within a parallel region must not appear
8424 // in a firstprivate clause on a worksharing construct if any of the
8425 // worksharing regions arising from the worksharing construct ever bind
8426 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008427 if (isOpenMPWorksharingDirective(CurrDir) &&
8428 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008429 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008430 if (DVar.CKind != OMPC_shared &&
8431 (isOpenMPParallelDirective(DVar.DKind) ||
8432 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008433 Diag(ELoc, diag::err_omp_required_access)
8434 << getOpenMPClauseName(OMPC_firstprivate)
8435 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008436 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008437 continue;
8438 }
8439 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008440 // OpenMP [2.9.3.4, Restrictions, p.3]
8441 // A list item that appears in a reduction clause of a parallel construct
8442 // must not appear in a firstprivate clause on a worksharing or task
8443 // construct if any of the worksharing or task regions arising from the
8444 // worksharing or task construct ever bind to any of the parallel regions
8445 // arising from the parallel construct.
8446 // OpenMP [2.9.3.4, Restrictions, p.4]
8447 // A list item that appears in a reduction clause in worksharing
8448 // construct must not appear in a firstprivate clause in a task construct
8449 // encountered during execution of any of the worksharing regions arising
8450 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008451 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008452 DVar = DSAStack->hasInnermostDSA(
8453 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8454 [](OpenMPDirectiveKind K) -> bool {
8455 return isOpenMPParallelDirective(K) ||
8456 isOpenMPWorksharingDirective(K);
8457 },
8458 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008459 if (DVar.CKind == OMPC_reduction &&
8460 (isOpenMPParallelDirective(DVar.DKind) ||
8461 isOpenMPWorksharingDirective(DVar.DKind))) {
8462 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8463 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008464 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008465 continue;
8466 }
8467 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008468
8469 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8470 // A list item that is private within a teams region must not appear in a
8471 // firstprivate clause on a distribute construct if any of the distribute
8472 // regions arising from the distribute construct ever bind to any of the
8473 // teams regions arising from the teams construct.
8474 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8475 // A list item that appears in a reduction clause of a teams construct
8476 // must not appear in a firstprivate clause on a distribute construct if
8477 // any of the distribute regions arising from the distribute construct
8478 // ever bind to any of the teams regions arising from the teams construct.
8479 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8480 // A list item may appear in a firstprivate or lastprivate clause but not
8481 // both.
8482 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008483 DVar = DSAStack->hasInnermostDSA(
8484 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8485 [](OpenMPDirectiveKind K) -> bool {
8486 return isOpenMPTeamsDirective(K);
8487 },
8488 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008489 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8490 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008491 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008492 continue;
8493 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008494 DVar = DSAStack->hasInnermostDSA(
8495 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8496 [](OpenMPDirectiveKind K) -> bool {
8497 return isOpenMPTeamsDirective(K);
8498 },
8499 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008500 if (DVar.CKind == OMPC_reduction &&
8501 isOpenMPTeamsDirective(DVar.DKind)) {
8502 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008503 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008504 continue;
8505 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008506 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008507 if (DVar.CKind == OMPC_lastprivate) {
8508 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008509 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008510 continue;
8511 }
8512 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008513 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8514 // A list item cannot appear in both a map clause and a data-sharing
8515 // attribute clause on the same construct
8516 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008517 if (DSAStack->checkMappableExprComponentListsForDecl(
8518 VD, /* CurrentRegionOnly = */ true,
8519 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8520 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008521 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8522 << getOpenMPClauseName(OMPC_firstprivate)
8523 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8524 ReportOriginalDSA(*this, DSAStack, D, DVar);
8525 continue;
8526 }
8527 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008528 }
8529
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008530 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008531 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008532 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008533 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8534 << getOpenMPClauseName(OMPC_firstprivate) << Type
8535 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8536 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008537 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008538 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008539 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008540 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008541 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008542 continue;
8543 }
8544
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008545 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008546 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8547 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008548 // Generate helper private variable and initialize it with the value of the
8549 // original variable. The address of the original variable is replaced by
8550 // the address of the new private variable in the CodeGen. This new variable
8551 // is not added to IdResolver, so the code in the OpenMP region uses
8552 // original variable for proper diagnostics and variable capturing.
8553 Expr *VDInitRefExpr = nullptr;
8554 // For arrays generate initializer for single element and replace it by the
8555 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008556 if (Type->isArrayType()) {
8557 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008558 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008559 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008560 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008561 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008562 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008563 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008564 InitializedEntity Entity =
8565 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008566 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8567
8568 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8569 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8570 if (Result.isInvalid())
8571 VDPrivate->setInvalidDecl();
8572 else
8573 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008574 // Remove temp variable declaration.
8575 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008576 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008577 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8578 ".firstprivate.temp");
8579 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8580 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008581 AddInitializerToDecl(VDPrivate,
8582 DefaultLvalueConversion(VDInitRefExpr).get(),
8583 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008584 }
8585 if (VDPrivate->isInvalidDecl()) {
8586 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008587 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008588 diag::note_omp_task_predetermined_firstprivate_here);
8589 }
8590 continue;
8591 }
8592 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008593 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008594 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8595 RefExpr->getExprLoc());
8596 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008597 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008598 if (TopDVar.CKind == OMPC_lastprivate)
8599 Ref = TopDVar.PrivateCopy;
8600 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008601 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008602 if (!IsOpenMPCapturedDecl(D))
8603 ExprCaptures.push_back(Ref->getDecl());
8604 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008605 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008606 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008607 Vars.push_back((VD || CurContext->isDependentContext())
8608 ? RefExpr->IgnoreParens()
8609 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008610 PrivateCopies.push_back(VDPrivateRefExpr);
8611 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008612 }
8613
Alexey Bataeved09d242014-05-28 05:53:51 +00008614 if (Vars.empty())
8615 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008616
8617 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008618 Vars, PrivateCopies, Inits,
8619 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008620}
8621
Alexander Musman1bb328c2014-06-04 13:06:39 +00008622OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8623 SourceLocation StartLoc,
8624 SourceLocation LParenLoc,
8625 SourceLocation EndLoc) {
8626 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008627 SmallVector<Expr *, 8> SrcExprs;
8628 SmallVector<Expr *, 8> DstExprs;
8629 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008630 SmallVector<Decl *, 4> ExprCaptures;
8631 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008632 for (auto &RefExpr : VarList) {
8633 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008634 SourceLocation ELoc;
8635 SourceRange ERange;
8636 Expr *SimpleRefExpr = RefExpr;
8637 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008638 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008639 // It will be analyzed later.
8640 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008641 SrcExprs.push_back(nullptr);
8642 DstExprs.push_back(nullptr);
8643 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008644 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008645 ValueDecl *D = Res.first;
8646 if (!D)
8647 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008648
Alexey Bataev74caaf22016-02-20 04:09:36 +00008649 QualType Type = D->getType();
8650 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008651
8652 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8653 // A variable that appears in a lastprivate clause must not have an
8654 // incomplete type or a reference type.
8655 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008656 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008657 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008658 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008659
8660 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8661 // in a Construct]
8662 // Variables with the predetermined data-sharing attributes may not be
8663 // listed in data-sharing attributes clauses, except for the cases
8664 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008665 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008666 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8667 DVar.CKind != OMPC_firstprivate &&
8668 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8669 Diag(ELoc, diag::err_omp_wrong_dsa)
8670 << getOpenMPClauseName(DVar.CKind)
8671 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008672 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008673 continue;
8674 }
8675
Alexey Bataevf29276e2014-06-18 04:14:57 +00008676 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8677 // OpenMP [2.14.3.5, Restrictions, p.2]
8678 // A list item that is private within a parallel region, or that appears in
8679 // the reduction clause of a parallel construct, must not appear in a
8680 // lastprivate clause on a worksharing construct if any of the corresponding
8681 // worksharing regions ever binds to any of the corresponding parallel
8682 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008683 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008684 if (isOpenMPWorksharingDirective(CurrDir) &&
8685 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008686 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008687 if (DVar.CKind != OMPC_shared) {
8688 Diag(ELoc, diag::err_omp_required_access)
8689 << getOpenMPClauseName(OMPC_lastprivate)
8690 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008691 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008692 continue;
8693 }
8694 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008695
8696 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8697 // A list item may appear in a firstprivate or lastprivate clause but not
8698 // both.
8699 if (CurrDir == OMPD_distribute) {
8700 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8701 if (DVar.CKind == OMPC_firstprivate) {
8702 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8703 ReportOriginalDSA(*this, DSAStack, D, DVar);
8704 continue;
8705 }
8706 }
8707
Alexander Musman1bb328c2014-06-04 13:06:39 +00008708 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008709 // A variable of class type (or array thereof) that appears in a
8710 // lastprivate clause requires an accessible, unambiguous default
8711 // constructor for the class type, unless the list item is also specified
8712 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008713 // A variable of class type (or array thereof) that appears in a
8714 // lastprivate clause requires an accessible, unambiguous copy assignment
8715 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008716 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008717 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008718 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008719 D->hasAttrs() ? &D->getAttrs() : nullptr);
8720 auto *PseudoSrcExpr =
8721 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008722 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008723 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008724 D->hasAttrs() ? &D->getAttrs() : nullptr);
8725 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008726 // For arrays generate assignment operation for single element and replace
8727 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008728 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008729 PseudoDstExpr, PseudoSrcExpr);
8730 if (AssignmentOp.isInvalid())
8731 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008732 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008733 /*DiscardedValue=*/true);
8734 if (AssignmentOp.isInvalid())
8735 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008736
Alexey Bataev74caaf22016-02-20 04:09:36 +00008737 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008738 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008739 if (TopDVar.CKind == OMPC_firstprivate)
8740 Ref = TopDVar.PrivateCopy;
8741 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008742 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008743 if (!IsOpenMPCapturedDecl(D))
8744 ExprCaptures.push_back(Ref->getDecl());
8745 }
8746 if (TopDVar.CKind == OMPC_firstprivate ||
8747 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008748 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008749 ExprResult RefRes = DefaultLvalueConversion(Ref);
8750 if (!RefRes.isUsable())
8751 continue;
8752 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008753 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8754 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008755 if (!PostUpdateRes.isUsable())
8756 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008757 ExprPostUpdates.push_back(
8758 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008759 }
8760 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008761 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008762 Vars.push_back((VD || CurContext->isDependentContext())
8763 ? RefExpr->IgnoreParens()
8764 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008765 SrcExprs.push_back(PseudoSrcExpr);
8766 DstExprs.push_back(PseudoDstExpr);
8767 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008768 }
8769
8770 if (Vars.empty())
8771 return nullptr;
8772
8773 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008774 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008775 buildPreInits(Context, ExprCaptures),
8776 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008777}
8778
Alexey Bataev758e55e2013-09-06 18:03:48 +00008779OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8780 SourceLocation StartLoc,
8781 SourceLocation LParenLoc,
8782 SourceLocation EndLoc) {
8783 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008784 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008785 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008786 SourceLocation ELoc;
8787 SourceRange ERange;
8788 Expr *SimpleRefExpr = RefExpr;
8789 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008790 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008791 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008792 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008793 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008794 ValueDecl *D = Res.first;
8795 if (!D)
8796 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008797
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008798 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008799 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8800 // in a Construct]
8801 // Variables with the predetermined data-sharing attributes may not be
8802 // listed in data-sharing attributes clauses, except for the cases
8803 // listed below. For these exceptions only, listing a predetermined
8804 // variable in a data-sharing attribute clause is allowed and overrides
8805 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008806 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008807 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8808 DVar.RefExpr) {
8809 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8810 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008811 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008812 continue;
8813 }
8814
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008815 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008816 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008817 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008818 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008819 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8820 ? RefExpr->IgnoreParens()
8821 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008822 }
8823
Alexey Bataeved09d242014-05-28 05:53:51 +00008824 if (Vars.empty())
8825 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008826
8827 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8828}
8829
Alexey Bataevc5e02582014-06-16 07:08:35 +00008830namespace {
8831class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8832 DSAStackTy *Stack;
8833
8834public:
8835 bool VisitDeclRefExpr(DeclRefExpr *E) {
8836 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008837 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008838 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8839 return false;
8840 if (DVar.CKind != OMPC_unknown)
8841 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008842 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8843 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8844 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008845 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008846 return true;
8847 return false;
8848 }
8849 return false;
8850 }
8851 bool VisitStmt(Stmt *S) {
8852 for (auto Child : S->children()) {
8853 if (Child && Visit(Child))
8854 return true;
8855 }
8856 return false;
8857 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008858 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008859};
Alexey Bataev23b69422014-06-18 07:08:49 +00008860} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008861
Alexey Bataev60da77e2016-02-29 05:54:20 +00008862namespace {
8863// Transform MemberExpression for specified FieldDecl of current class to
8864// DeclRefExpr to specified OMPCapturedExprDecl.
8865class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8866 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8867 ValueDecl *Field;
8868 DeclRefExpr *CapturedExpr;
8869
8870public:
8871 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8872 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8873
8874 ExprResult TransformMemberExpr(MemberExpr *E) {
8875 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8876 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008877 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008878 return CapturedExpr;
8879 }
8880 return BaseTransform::TransformMemberExpr(E);
8881 }
8882 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8883};
8884} // namespace
8885
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008886template <typename T>
8887static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8888 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8889 for (auto &Set : Lookups) {
8890 for (auto *D : Set) {
8891 if (auto Res = Gen(cast<ValueDecl>(D)))
8892 return Res;
8893 }
8894 }
8895 return T();
8896}
8897
8898static ExprResult
8899buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8900 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8901 const DeclarationNameInfo &ReductionId, QualType Ty,
8902 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8903 if (ReductionIdScopeSpec.isInvalid())
8904 return ExprError();
8905 SmallVector<UnresolvedSet<8>, 4> Lookups;
8906 if (S) {
8907 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8908 Lookup.suppressDiagnostics();
8909 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8910 auto *D = Lookup.getRepresentativeDecl();
8911 do {
8912 S = S->getParent();
8913 } while (S && !S->isDeclScope(D));
8914 if (S)
8915 S = S->getParent();
8916 Lookups.push_back(UnresolvedSet<8>());
8917 Lookups.back().append(Lookup.begin(), Lookup.end());
8918 Lookup.clear();
8919 }
8920 } else if (auto *ULE =
8921 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8922 Lookups.push_back(UnresolvedSet<8>());
8923 Decl *PrevD = nullptr;
8924 for(auto *D : ULE->decls()) {
8925 if (D == PrevD)
8926 Lookups.push_back(UnresolvedSet<8>());
8927 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8928 Lookups.back().addDecl(DRD);
8929 PrevD = D;
8930 }
8931 }
8932 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8933 Ty->containsUnexpandedParameterPack() ||
8934 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8935 return !D->isInvalidDecl() &&
8936 (D->getType()->isDependentType() ||
8937 D->getType()->isInstantiationDependentType() ||
8938 D->getType()->containsUnexpandedParameterPack());
8939 })) {
8940 UnresolvedSet<8> ResSet;
8941 for (auto &Set : Lookups) {
8942 ResSet.append(Set.begin(), Set.end());
8943 // The last item marks the end of all declarations at the specified scope.
8944 ResSet.addDecl(Set[Set.size() - 1]);
8945 }
8946 return UnresolvedLookupExpr::Create(
8947 SemaRef.Context, /*NamingClass=*/nullptr,
8948 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8949 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8950 }
8951 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8952 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8953 if (!D->isInvalidDecl() &&
8954 SemaRef.Context.hasSameType(D->getType(), Ty))
8955 return D;
8956 return nullptr;
8957 }))
8958 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8959 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8960 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8961 if (!D->isInvalidDecl() &&
8962 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8963 !Ty.isMoreQualifiedThan(D->getType()))
8964 return D;
8965 return nullptr;
8966 })) {
8967 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8968 /*DetectVirtual=*/false);
8969 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8970 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8971 VD->getType().getUnqualifiedType()))) {
8972 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8973 /*DiagID=*/0) !=
8974 Sema::AR_inaccessible) {
8975 SemaRef.BuildBasePathArray(Paths, BasePath);
8976 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8977 }
8978 }
8979 }
8980 }
8981 if (ReductionIdScopeSpec.isSet()) {
8982 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8983 return ExprError();
8984 }
8985 return ExprEmpty();
8986}
8987
Alexey Bataevc5e02582014-06-16 07:08:35 +00008988OMPClause *Sema::ActOnOpenMPReductionClause(
8989 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8990 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008991 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8992 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008993 auto DN = ReductionId.getName();
8994 auto OOK = DN.getCXXOverloadedOperator();
8995 BinaryOperatorKind BOK = BO_Comma;
8996
8997 // OpenMP [2.14.3.6, reduction clause]
8998 // C
8999 // reduction-identifier is either an identifier or one of the following
9000 // operators: +, -, *, &, |, ^, && and ||
9001 // C++
9002 // reduction-identifier is either an id-expression or one of the following
9003 // operators: +, -, *, &, |, ^, && and ||
9004 // FIXME: Only 'min' and 'max' identifiers are supported for now.
9005 switch (OOK) {
9006 case OO_Plus:
9007 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009008 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009009 break;
9010 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009011 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009012 break;
9013 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009014 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009015 break;
9016 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009017 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009018 break;
9019 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009020 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009021 break;
9022 case OO_AmpAmp:
9023 BOK = BO_LAnd;
9024 break;
9025 case OO_PipePipe:
9026 BOK = BO_LOr;
9027 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009028 case OO_New:
9029 case OO_Delete:
9030 case OO_Array_New:
9031 case OO_Array_Delete:
9032 case OO_Slash:
9033 case OO_Percent:
9034 case OO_Tilde:
9035 case OO_Exclaim:
9036 case OO_Equal:
9037 case OO_Less:
9038 case OO_Greater:
9039 case OO_LessEqual:
9040 case OO_GreaterEqual:
9041 case OO_PlusEqual:
9042 case OO_MinusEqual:
9043 case OO_StarEqual:
9044 case OO_SlashEqual:
9045 case OO_PercentEqual:
9046 case OO_CaretEqual:
9047 case OO_AmpEqual:
9048 case OO_PipeEqual:
9049 case OO_LessLess:
9050 case OO_GreaterGreater:
9051 case OO_LessLessEqual:
9052 case OO_GreaterGreaterEqual:
9053 case OO_EqualEqual:
9054 case OO_ExclaimEqual:
9055 case OO_PlusPlus:
9056 case OO_MinusMinus:
9057 case OO_Comma:
9058 case OO_ArrowStar:
9059 case OO_Arrow:
9060 case OO_Call:
9061 case OO_Subscript:
9062 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009063 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009064 case NUM_OVERLOADED_OPERATORS:
9065 llvm_unreachable("Unexpected reduction identifier");
9066 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009067 if (auto II = DN.getAsIdentifierInfo()) {
9068 if (II->isStr("max"))
9069 BOK = BO_GT;
9070 else if (II->isStr("min"))
9071 BOK = BO_LT;
9072 }
9073 break;
9074 }
9075 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009076 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009077 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009078 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009079
9080 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009081 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009082 SmallVector<Expr *, 8> LHSs;
9083 SmallVector<Expr *, 8> RHSs;
9084 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009085 SmallVector<Decl *, 4> ExprCaptures;
9086 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009087 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9088 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009089 for (auto RefExpr : VarList) {
9090 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009091 // OpenMP [2.1, C/C++]
9092 // A list item is a variable or array section, subject to the restrictions
9093 // specified in Section 2.4 on page 42 and in each of the sections
9094 // describing clauses and directives for which a list appears.
9095 // OpenMP [2.14.3.3, Restrictions, p.1]
9096 // A variable that is part of another variable (as an array or
9097 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009098 if (!FirstIter && IR != ER)
9099 ++IR;
9100 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009101 SourceLocation ELoc;
9102 SourceRange ERange;
9103 Expr *SimpleRefExpr = RefExpr;
9104 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9105 /*AllowArraySection=*/true);
9106 if (Res.second) {
9107 // It will be analyzed later.
9108 Vars.push_back(RefExpr);
9109 Privates.push_back(nullptr);
9110 LHSs.push_back(nullptr);
9111 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009112 // Try to find 'declare reduction' corresponding construct before using
9113 // builtin/overloaded operators.
9114 QualType Type = Context.DependentTy;
9115 CXXCastPath BasePath;
9116 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9117 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9118 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9119 if (CurContext->isDependentContext() &&
9120 (DeclareReductionRef.isUnset() ||
9121 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9122 ReductionOps.push_back(DeclareReductionRef.get());
9123 else
9124 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009125 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009126 ValueDecl *D = Res.first;
9127 if (!D)
9128 continue;
9129
Alexey Bataeva1764212015-09-30 09:22:36 +00009130 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009131 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9132 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9133 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009134 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009135 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009136 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9137 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9138 Type = ATy->getElementType();
9139 else
9140 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009141 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009142 } else
9143 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9144 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009145
Alexey Bataevc5e02582014-06-16 07:08:35 +00009146 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9147 // A variable that appears in a private clause must not have an incomplete
9148 // type or a reference type.
9149 if (RequireCompleteType(ELoc, Type,
9150 diag::err_omp_reduction_incomplete_type))
9151 continue;
9152 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009153 // A list item that appears in a reduction clause must not be
9154 // const-qualified.
9155 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009156 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009157 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009158 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009159 bool IsDecl = !VD ||
9160 VD->isThisDeclarationADefinition(Context) ==
9161 VarDecl::DeclarationOnly;
9162 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009163 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009164 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009165 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009166 continue;
9167 }
9168 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9169 // If a list-item is a reference type then it must bind to the same object
9170 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009171 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009172 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009173 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009174 DSARefChecker Check(DSAStack);
9175 if (Check.Visit(VDDef->getInit())) {
9176 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9177 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9178 continue;
9179 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009180 }
9181 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009182
Alexey Bataevc5e02582014-06-16 07:08:35 +00009183 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9184 // in a Construct]
9185 // Variables with the predetermined data-sharing attributes may not be
9186 // listed in data-sharing attributes clauses, except for the cases
9187 // listed below. For these exceptions only, listing a predetermined
9188 // variable in a data-sharing attribute clause is allowed and overrides
9189 // the variable's predetermined data-sharing attributes.
9190 // OpenMP [2.14.3.6, Restrictions, p.3]
9191 // Any number of reduction clauses can be specified on the directive,
9192 // but a list item can appear only once in the reduction clauses for that
9193 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009194 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009195 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009196 if (DVar.CKind == OMPC_reduction) {
9197 Diag(ELoc, diag::err_omp_once_referenced)
9198 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009199 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009200 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009201 } else if (DVar.CKind != OMPC_unknown) {
9202 Diag(ELoc, diag::err_omp_wrong_dsa)
9203 << getOpenMPClauseName(DVar.CKind)
9204 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009205 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009206 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009207 }
9208
9209 // OpenMP [2.14.3.6, Restrictions, p.1]
9210 // A list item that appears in a reduction clause of a worksharing
9211 // construct must be shared in the parallel regions to which any of the
9212 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009213 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9214 if (isOpenMPWorksharingDirective(CurrDir) &&
9215 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009216 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009217 if (DVar.CKind != OMPC_shared) {
9218 Diag(ELoc, diag::err_omp_required_access)
9219 << getOpenMPClauseName(OMPC_reduction)
9220 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009221 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009222 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009223 }
9224 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009225
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009226 // Try to find 'declare reduction' corresponding construct before using
9227 // builtin/overloaded operators.
9228 CXXCastPath BasePath;
9229 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9230 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9231 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9232 if (DeclareReductionRef.isInvalid())
9233 continue;
9234 if (CurContext->isDependentContext() &&
9235 (DeclareReductionRef.isUnset() ||
9236 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9237 Vars.push_back(RefExpr);
9238 Privates.push_back(nullptr);
9239 LHSs.push_back(nullptr);
9240 RHSs.push_back(nullptr);
9241 ReductionOps.push_back(DeclareReductionRef.get());
9242 continue;
9243 }
9244 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9245 // Not allowed reduction identifier is found.
9246 Diag(ReductionId.getLocStart(),
9247 diag::err_omp_unknown_reduction_identifier)
9248 << Type << ReductionIdRange;
9249 continue;
9250 }
9251
9252 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9253 // The type of a list item that appears in a reduction clause must be valid
9254 // for the reduction-identifier. For a max or min reduction in C, the type
9255 // of the list item must be an allowed arithmetic data type: char, int,
9256 // float, double, or _Bool, possibly modified with long, short, signed, or
9257 // unsigned. For a max or min reduction in C++, the type of the list item
9258 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9259 // double, or bool, possibly modified with long, short, signed, or unsigned.
9260 if (DeclareReductionRef.isUnset()) {
9261 if ((BOK == BO_GT || BOK == BO_LT) &&
9262 !(Type->isScalarType() ||
9263 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9264 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9265 << getLangOpts().CPlusPlus;
9266 if (!ASE && !OASE) {
9267 bool IsDecl = !VD ||
9268 VD->isThisDeclarationADefinition(Context) ==
9269 VarDecl::DeclarationOnly;
9270 Diag(D->getLocation(),
9271 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9272 << D;
9273 }
9274 continue;
9275 }
9276 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9277 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9278 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9279 if (!ASE && !OASE) {
9280 bool IsDecl = !VD ||
9281 VD->isThisDeclarationADefinition(Context) ==
9282 VarDecl::DeclarationOnly;
9283 Diag(D->getLocation(),
9284 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9285 << D;
9286 }
9287 continue;
9288 }
9289 }
9290
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009291 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009292 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009293 D->hasAttrs() ? &D->getAttrs() : nullptr);
9294 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9295 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009296 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009297 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009298 (!ASE &&
9299 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009300 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009301 // Create pseudo array type for private copy. The size for this array will
9302 // be generated during codegen.
9303 // For array subscripts or single variables Private Ty is the same as Type
9304 // (type of the variable or single array element).
9305 PrivateTy = Context.getVariableArrayType(
9306 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9307 Context.getSizeType(), VK_RValue),
9308 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009309 } else if (!ASE && !OASE &&
9310 Context.getAsArrayType(D->getType().getNonReferenceType()))
9311 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009312 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009313 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9314 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009315 // Add initializer for private variable.
9316 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009317 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9318 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9319 if (DeclareReductionRef.isUsable()) {
9320 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9321 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9322 if (DRD->getInitializer()) {
9323 Init = DRDRef;
9324 RHSVD->setInit(DRDRef);
9325 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009326 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009327 } else {
9328 switch (BOK) {
9329 case BO_Add:
9330 case BO_Xor:
9331 case BO_Or:
9332 case BO_LOr:
9333 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9334 if (Type->isScalarType() || Type->isAnyComplexType())
9335 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9336 break;
9337 case BO_Mul:
9338 case BO_LAnd:
9339 if (Type->isScalarType() || Type->isAnyComplexType()) {
9340 // '*' and '&&' reduction ops - initializer is '1'.
9341 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009342 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009343 break;
9344 case BO_And: {
9345 // '&' reduction op - initializer is '~0'.
9346 QualType OrigType = Type;
9347 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9348 Type = ComplexTy->getElementType();
9349 if (Type->isRealFloatingType()) {
9350 llvm::APFloat InitValue =
9351 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9352 /*isIEEE=*/true);
9353 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9354 Type, ELoc);
9355 } else if (Type->isScalarType()) {
9356 auto Size = Context.getTypeSize(Type);
9357 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9358 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9359 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9360 }
9361 if (Init && OrigType->isAnyComplexType()) {
9362 // Init = 0xFFFF + 0xFFFFi;
9363 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9364 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9365 }
9366 Type = OrigType;
9367 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009368 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009369 case BO_LT:
9370 case BO_GT: {
9371 // 'min' reduction op - initializer is 'Largest representable number in
9372 // the reduction list item type'.
9373 // 'max' reduction op - initializer is 'Least representable number in
9374 // the reduction list item type'.
9375 if (Type->isIntegerType() || Type->isPointerType()) {
9376 bool IsSigned = Type->hasSignedIntegerRepresentation();
9377 auto Size = Context.getTypeSize(Type);
9378 QualType IntTy =
9379 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9380 llvm::APInt InitValue =
9381 (BOK != BO_LT)
9382 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9383 : llvm::APInt::getMinValue(Size)
9384 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9385 : llvm::APInt::getMaxValue(Size);
9386 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9387 if (Type->isPointerType()) {
9388 // Cast to pointer type.
9389 auto CastExpr = BuildCStyleCastExpr(
9390 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9391 SourceLocation(), Init);
9392 if (CastExpr.isInvalid())
9393 continue;
9394 Init = CastExpr.get();
9395 }
9396 } else if (Type->isRealFloatingType()) {
9397 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9398 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9399 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9400 Type, ELoc);
9401 }
9402 break;
9403 }
9404 case BO_PtrMemD:
9405 case BO_PtrMemI:
9406 case BO_MulAssign:
9407 case BO_Div:
9408 case BO_Rem:
9409 case BO_Sub:
9410 case BO_Shl:
9411 case BO_Shr:
9412 case BO_LE:
9413 case BO_GE:
9414 case BO_EQ:
9415 case BO_NE:
9416 case BO_AndAssign:
9417 case BO_XorAssign:
9418 case BO_OrAssign:
9419 case BO_Assign:
9420 case BO_AddAssign:
9421 case BO_SubAssign:
9422 case BO_DivAssign:
9423 case BO_RemAssign:
9424 case BO_ShlAssign:
9425 case BO_ShrAssign:
9426 case BO_Comma:
9427 llvm_unreachable("Unexpected reduction operation");
9428 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009429 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009430 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009431 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9432 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009433 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009434 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009435 if (RHSVD->isInvalidDecl())
9436 continue;
9437 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009438 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9439 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009440 bool IsDecl =
9441 !VD ||
9442 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9443 Diag(D->getLocation(),
9444 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9445 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009446 continue;
9447 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009448 // Store initializer for single element in private copy. Will be used during
9449 // codegen.
9450 PrivateVD->setInit(RHSVD->getInit());
9451 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009452 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009453 ExprResult ReductionOp;
9454 if (DeclareReductionRef.isUsable()) {
9455 QualType RedTy = DeclareReductionRef.get()->getType();
9456 QualType PtrRedTy = Context.getPointerType(RedTy);
9457 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9458 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9459 if (!BasePath.empty()) {
9460 LHS = DefaultLvalueConversion(LHS.get());
9461 RHS = DefaultLvalueConversion(RHS.get());
9462 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9463 CK_UncheckedDerivedToBase, LHS.get(),
9464 &BasePath, LHS.get()->getValueKind());
9465 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9466 CK_UncheckedDerivedToBase, RHS.get(),
9467 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009468 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009469 FunctionProtoType::ExtProtoInfo EPI;
9470 QualType Params[] = {PtrRedTy, PtrRedTy};
9471 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9472 auto *OVE = new (Context) OpaqueValueExpr(
9473 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9474 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9475 Expr *Args[] = {LHS.get(), RHS.get()};
9476 ReductionOp = new (Context)
9477 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9478 } else {
9479 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9480 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9481 if (ReductionOp.isUsable()) {
9482 if (BOK != BO_LT && BOK != BO_GT) {
9483 ReductionOp =
9484 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9485 BO_Assign, LHSDRE, ReductionOp.get());
9486 } else {
9487 auto *ConditionalOp = new (Context) ConditionalOperator(
9488 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9489 RHSDRE, Type, VK_LValue, OK_Ordinary);
9490 ReductionOp =
9491 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9492 BO_Assign, LHSDRE, ConditionalOp);
9493 }
9494 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9495 }
9496 if (ReductionOp.isInvalid())
9497 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009498 }
9499
Alexey Bataev60da77e2016-02-29 05:54:20 +00009500 DeclRefExpr *Ref = nullptr;
9501 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009502 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009503 if (ASE || OASE) {
9504 TransformExprToCaptures RebuildToCapture(*this, D);
9505 VarsExpr =
9506 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9507 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009508 } else {
9509 VarsExpr = Ref =
9510 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009511 }
9512 if (!IsOpenMPCapturedDecl(D)) {
9513 ExprCaptures.push_back(Ref->getDecl());
9514 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9515 ExprResult RefRes = DefaultLvalueConversion(Ref);
9516 if (!RefRes.isUsable())
9517 continue;
9518 ExprResult PostUpdateRes =
9519 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9520 SimpleRefExpr, RefRes.get());
9521 if (!PostUpdateRes.isUsable())
9522 continue;
9523 ExprPostUpdates.push_back(
9524 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009525 }
9526 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009527 }
9528 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9529 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009530 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009531 LHSs.push_back(LHSDRE);
9532 RHSs.push_back(RHSDRE);
9533 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009534 }
9535
9536 if (Vars.empty())
9537 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009538
Alexey Bataevc5e02582014-06-16 07:08:35 +00009539 return OMPReductionClause::Create(
9540 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009541 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009542 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9543 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009544}
9545
Alexey Bataevecba70f2016-04-12 11:02:11 +00009546bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9547 SourceLocation LinLoc) {
9548 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9549 LinKind == OMPC_LINEAR_unknown) {
9550 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9551 return true;
9552 }
9553 return false;
9554}
9555
9556bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9557 OpenMPLinearClauseKind LinKind,
9558 QualType Type) {
9559 auto *VD = dyn_cast_or_null<VarDecl>(D);
9560 // A variable must not have an incomplete type or a reference type.
9561 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9562 return true;
9563 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9564 !Type->isReferenceType()) {
9565 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9566 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9567 return true;
9568 }
9569 Type = Type.getNonReferenceType();
9570
9571 // A list item must not be const-qualified.
9572 if (Type.isConstant(Context)) {
9573 Diag(ELoc, diag::err_omp_const_variable)
9574 << getOpenMPClauseName(OMPC_linear);
9575 if (D) {
9576 bool IsDecl =
9577 !VD ||
9578 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9579 Diag(D->getLocation(),
9580 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9581 << D;
9582 }
9583 return true;
9584 }
9585
9586 // A list item must be of integral or pointer type.
9587 Type = Type.getUnqualifiedType().getCanonicalType();
9588 const auto *Ty = Type.getTypePtrOrNull();
9589 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9590 !Ty->isPointerType())) {
9591 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9592 if (D) {
9593 bool IsDecl =
9594 !VD ||
9595 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9596 Diag(D->getLocation(),
9597 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9598 << D;
9599 }
9600 return true;
9601 }
9602 return false;
9603}
9604
Alexey Bataev182227b2015-08-20 10:54:39 +00009605OMPClause *Sema::ActOnOpenMPLinearClause(
9606 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9607 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9608 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009609 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009610 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009611 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009612 SmallVector<Decl *, 4> ExprCaptures;
9613 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009614 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009615 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009616 for (auto &RefExpr : VarList) {
9617 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009618 SourceLocation ELoc;
9619 SourceRange ERange;
9620 Expr *SimpleRefExpr = RefExpr;
9621 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9622 /*AllowArraySection=*/false);
9623 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009624 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009625 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009626 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009627 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009628 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009629 ValueDecl *D = Res.first;
9630 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009631 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009632
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009633 QualType Type = D->getType();
9634 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009635
9636 // OpenMP [2.14.3.7, linear clause]
9637 // A list-item cannot appear in more than one linear clause.
9638 // A list-item that appears in a linear clause cannot appear in any
9639 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009640 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009641 if (DVar.RefExpr) {
9642 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9643 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009644 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009645 continue;
9646 }
9647
Alexey Bataevecba70f2016-04-12 11:02:11 +00009648 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009649 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009650 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009651
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009652 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009653 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9654 D->hasAttrs() ? &D->getAttrs() : nullptr);
9655 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009656 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009657 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009658 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009659 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009660 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009661 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9662 if (!IsOpenMPCapturedDecl(D)) {
9663 ExprCaptures.push_back(Ref->getDecl());
9664 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9665 ExprResult RefRes = DefaultLvalueConversion(Ref);
9666 if (!RefRes.isUsable())
9667 continue;
9668 ExprResult PostUpdateRes =
9669 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9670 SimpleRefExpr, RefRes.get());
9671 if (!PostUpdateRes.isUsable())
9672 continue;
9673 ExprPostUpdates.push_back(
9674 IgnoredValueConversions(PostUpdateRes.get()).get());
9675 }
9676 }
9677 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009678 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009679 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009680 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009681 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009682 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009683 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9684 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9685
9686 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009687 Vars.push_back((VD || CurContext->isDependentContext())
9688 ? RefExpr->IgnoreParens()
9689 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009690 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009691 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009692 }
9693
9694 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009695 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009696
9697 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009698 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009699 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9700 !Step->isInstantiationDependent() &&
9701 !Step->containsUnexpandedParameterPack()) {
9702 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009703 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009704 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009705 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009706 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009707
Alexander Musman3276a272015-03-21 10:12:56 +00009708 // Build var to save the step value.
9709 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009710 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009711 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009712 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009713 ExprResult CalcStep =
9714 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009715 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009716
Alexander Musman8dba6642014-04-22 13:09:42 +00009717 // Warn about zero linear step (it would be probably better specified as
9718 // making corresponding variables 'const').
9719 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009720 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9721 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009722 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9723 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009724 if (!IsConstant && CalcStep.isUsable()) {
9725 // Calculate the step beforehand instead of doing this on each iteration.
9726 // (This is not used if the number of iterations may be kfold-ed).
9727 CalcStepExpr = CalcStep.get();
9728 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009729 }
9730
Alexey Bataev182227b2015-08-20 10:54:39 +00009731 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9732 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009733 StepExpr, CalcStepExpr,
9734 buildPreInits(Context, ExprCaptures),
9735 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009736}
9737
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009738static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9739 Expr *NumIterations, Sema &SemaRef,
9740 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009741 // Walk the vars and build update/final expressions for the CodeGen.
9742 SmallVector<Expr *, 8> Updates;
9743 SmallVector<Expr *, 8> Finals;
9744 Expr *Step = Clause.getStep();
9745 Expr *CalcStep = Clause.getCalcStep();
9746 // OpenMP [2.14.3.7, linear clause]
9747 // If linear-step is not specified it is assumed to be 1.
9748 if (Step == nullptr)
9749 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009750 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009751 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009752 }
Alexander Musman3276a272015-03-21 10:12:56 +00009753 bool HasErrors = false;
9754 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009755 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009756 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009757 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009758 SourceLocation ELoc;
9759 SourceRange ERange;
9760 Expr *SimpleRefExpr = RefExpr;
9761 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9762 /*AllowArraySection=*/false);
9763 ValueDecl *D = Res.first;
9764 if (Res.second || !D) {
9765 Updates.push_back(nullptr);
9766 Finals.push_back(nullptr);
9767 HasErrors = true;
9768 continue;
9769 }
9770 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9771 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9772 ->getMemberDecl();
9773 }
9774 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009775 Expr *InitExpr = *CurInit;
9776
9777 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009778 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009779 Expr *CapturedRef;
9780 if (LinKind == OMPC_LINEAR_uval)
9781 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9782 else
9783 CapturedRef =
9784 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9785 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9786 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009787
9788 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009789 ExprResult Update;
9790 if (!Info.first) {
9791 Update =
9792 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9793 InitExpr, IV, Step, /* Subtract */ false);
9794 } else
9795 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009796 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9797 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009798
9799 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009800 ExprResult Final;
9801 if (!Info.first) {
9802 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9803 InitExpr, NumIterations, Step,
9804 /* Subtract */ false);
9805 } else
9806 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009807 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9808 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009809
Alexander Musman3276a272015-03-21 10:12:56 +00009810 if (!Update.isUsable() || !Final.isUsable()) {
9811 Updates.push_back(nullptr);
9812 Finals.push_back(nullptr);
9813 HasErrors = true;
9814 } else {
9815 Updates.push_back(Update.get());
9816 Finals.push_back(Final.get());
9817 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009818 ++CurInit;
9819 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009820 }
9821 Clause.setUpdates(Updates);
9822 Clause.setFinals(Finals);
9823 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009824}
9825
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009826OMPClause *Sema::ActOnOpenMPAlignedClause(
9827 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9828 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9829
9830 SmallVector<Expr *, 8> Vars;
9831 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009832 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9833 SourceLocation ELoc;
9834 SourceRange ERange;
9835 Expr *SimpleRefExpr = RefExpr;
9836 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9837 /*AllowArraySection=*/false);
9838 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009839 // It will be analyzed later.
9840 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009841 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009842 ValueDecl *D = Res.first;
9843 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009844 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009845
Alexey Bataev1efd1662016-03-29 10:59:56 +00009846 QualType QType = D->getType();
9847 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009848
9849 // OpenMP [2.8.1, simd construct, Restrictions]
9850 // The type of list items appearing in the aligned clause must be
9851 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009852 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009853 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009854 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009855 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009856 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009857 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009858 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009859 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009860 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009861 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009862 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009863 continue;
9864 }
9865
9866 // OpenMP [2.8.1, simd construct, Restrictions]
9867 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009868 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009869 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009870 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9871 << getOpenMPClauseName(OMPC_aligned);
9872 continue;
9873 }
9874
Alexey Bataev1efd1662016-03-29 10:59:56 +00009875 DeclRefExpr *Ref = nullptr;
9876 if (!VD && IsOpenMPCapturedDecl(D))
9877 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9878 Vars.push_back(DefaultFunctionArrayConversion(
9879 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9880 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009881 }
9882
9883 // OpenMP [2.8.1, simd construct, Description]
9884 // The parameter of the aligned clause, alignment, must be a constant
9885 // positive integer expression.
9886 // If no optional parameter is specified, implementation-defined default
9887 // alignments for SIMD instructions on the target platforms are assumed.
9888 if (Alignment != nullptr) {
9889 ExprResult AlignResult =
9890 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9891 if (AlignResult.isInvalid())
9892 return nullptr;
9893 Alignment = AlignResult.get();
9894 }
9895 if (Vars.empty())
9896 return nullptr;
9897
9898 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9899 EndLoc, Vars, Alignment);
9900}
9901
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009902OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9903 SourceLocation StartLoc,
9904 SourceLocation LParenLoc,
9905 SourceLocation EndLoc) {
9906 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009907 SmallVector<Expr *, 8> SrcExprs;
9908 SmallVector<Expr *, 8> DstExprs;
9909 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009910 for (auto &RefExpr : VarList) {
9911 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9912 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009913 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009914 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009915 SrcExprs.push_back(nullptr);
9916 DstExprs.push_back(nullptr);
9917 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009918 continue;
9919 }
9920
Alexey Bataeved09d242014-05-28 05:53:51 +00009921 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009922 // OpenMP [2.1, C/C++]
9923 // A list item is a variable name.
9924 // OpenMP [2.14.4.1, Restrictions, p.1]
9925 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009926 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009927 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009928 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9929 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009930 continue;
9931 }
9932
9933 Decl *D = DE->getDecl();
9934 VarDecl *VD = cast<VarDecl>(D);
9935
9936 QualType Type = VD->getType();
9937 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9938 // It will be analyzed later.
9939 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009940 SrcExprs.push_back(nullptr);
9941 DstExprs.push_back(nullptr);
9942 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009943 continue;
9944 }
9945
9946 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9947 // A list item that appears in a copyin clause must be threadprivate.
9948 if (!DSAStack->isThreadPrivate(VD)) {
9949 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009950 << getOpenMPClauseName(OMPC_copyin)
9951 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009952 continue;
9953 }
9954
9955 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9956 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009957 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009958 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009959 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009960 auto *SrcVD =
9961 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9962 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009963 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009964 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9965 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009966 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9967 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009968 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009969 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009970 // For arrays generate assignment operation for single element and replace
9971 // it by the original array element in CodeGen.
9972 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9973 PseudoDstExpr, PseudoSrcExpr);
9974 if (AssignmentOp.isInvalid())
9975 continue;
9976 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9977 /*DiscardedValue=*/true);
9978 if (AssignmentOp.isInvalid())
9979 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009980
9981 DSAStack->addDSA(VD, DE, OMPC_copyin);
9982 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009983 SrcExprs.push_back(PseudoSrcExpr);
9984 DstExprs.push_back(PseudoDstExpr);
9985 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009986 }
9987
Alexey Bataeved09d242014-05-28 05:53:51 +00009988 if (Vars.empty())
9989 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009990
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009991 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9992 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009993}
9994
Alexey Bataevbae9a792014-06-27 10:37:06 +00009995OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9996 SourceLocation StartLoc,
9997 SourceLocation LParenLoc,
9998 SourceLocation EndLoc) {
9999 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000010000 SmallVector<Expr *, 8> SrcExprs;
10001 SmallVector<Expr *, 8> DstExprs;
10002 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010003 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010004 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10005 SourceLocation ELoc;
10006 SourceRange ERange;
10007 Expr *SimpleRefExpr = RefExpr;
10008 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10009 /*AllowArraySection=*/false);
10010 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010011 // It will be analyzed later.
10012 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010013 SrcExprs.push_back(nullptr);
10014 DstExprs.push_back(nullptr);
10015 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010016 }
Alexey Bataeve122da12016-03-17 10:50:17 +000010017 ValueDecl *D = Res.first;
10018 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000010019 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010020
Alexey Bataeve122da12016-03-17 10:50:17 +000010021 QualType Type = D->getType();
10022 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010023
10024 // OpenMP [2.14.4.2, Restrictions, p.2]
10025 // A list item that appears in a copyprivate clause may not appear in a
10026 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000010027 if (!VD || !DSAStack->isThreadPrivate(VD)) {
10028 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010029 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10030 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010031 Diag(ELoc, diag::err_omp_wrong_dsa)
10032 << getOpenMPClauseName(DVar.CKind)
10033 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +000010034 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010035 continue;
10036 }
10037
10038 // OpenMP [2.11.4.2, Restrictions, p.1]
10039 // All list items that appear in a copyprivate clause must be either
10040 // threadprivate or private in the enclosing context.
10041 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010042 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010043 if (DVar.CKind == OMPC_shared) {
10044 Diag(ELoc, diag::err_omp_required_access)
10045 << getOpenMPClauseName(OMPC_copyprivate)
10046 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000010047 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010048 continue;
10049 }
10050 }
10051 }
10052
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010053 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010054 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010055 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010056 << getOpenMPClauseName(OMPC_copyprivate) << Type
10057 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010058 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000010059 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010060 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010061 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010062 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010063 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010064 continue;
10065 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010066
Alexey Bataevbae9a792014-06-27 10:37:06 +000010067 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10068 // A variable of class type (or array thereof) that appears in a
10069 // copyin clause requires an accessible, unambiguous copy assignment
10070 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010071 Type = Context.getBaseElementType(Type.getNonReferenceType())
10072 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010073 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010074 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10075 D->hasAttrs() ? &D->getAttrs() : nullptr);
10076 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010077 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010078 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10079 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010080 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010081 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10082 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010083 PseudoDstExpr, PseudoSrcExpr);
10084 if (AssignmentOp.isInvalid())
10085 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010086 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010087 /*DiscardedValue=*/true);
10088 if (AssignmentOp.isInvalid())
10089 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010090
10091 // No need to mark vars as copyprivate, they are already threadprivate or
10092 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010093 assert(VD || IsOpenMPCapturedDecl(D));
10094 Vars.push_back(
10095 VD ? RefExpr->IgnoreParens()
10096 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010097 SrcExprs.push_back(PseudoSrcExpr);
10098 DstExprs.push_back(PseudoDstExpr);
10099 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010100 }
10101
10102 if (Vars.empty())
10103 return nullptr;
10104
Alexey Bataeva63048e2015-03-23 06:18:07 +000010105 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10106 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010107}
10108
Alexey Bataev6125da92014-07-21 11:26:11 +000010109OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10110 SourceLocation StartLoc,
10111 SourceLocation LParenLoc,
10112 SourceLocation EndLoc) {
10113 if (VarList.empty())
10114 return nullptr;
10115
10116 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10117}
Alexey Bataevdea47612014-07-23 07:46:59 +000010118
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010119OMPClause *
10120Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10121 SourceLocation DepLoc, SourceLocation ColonLoc,
10122 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10123 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010124 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010125 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010126 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010127 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010128 return nullptr;
10129 }
10130 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010131 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10132 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010133 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010134 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010135 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10136 /*Last=*/OMPC_DEPEND_unknown, Except)
10137 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010138 return nullptr;
10139 }
10140 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010141 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010142 llvm::APSInt DepCounter(/*BitWidth=*/32);
10143 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10144 if (DepKind == OMPC_DEPEND_sink) {
10145 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10146 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10147 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010148 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010149 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010150 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10151 DSAStack->getParentOrderedRegionParam()) {
10152 for (auto &RefExpr : VarList) {
10153 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010154 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010155 // It will be analyzed later.
10156 Vars.push_back(RefExpr);
10157 continue;
10158 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010159
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010160 SourceLocation ELoc = RefExpr->getExprLoc();
10161 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10162 if (DepKind == OMPC_DEPEND_sink) {
10163 if (DepCounter >= TotalDepCount) {
10164 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10165 continue;
10166 }
10167 ++DepCounter;
10168 // OpenMP [2.13.9, Summary]
10169 // depend(dependence-type : vec), where dependence-type is:
10170 // 'sink' and where vec is the iteration vector, which has the form:
10171 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10172 // where n is the value specified by the ordered clause in the loop
10173 // directive, xi denotes the loop iteration variable of the i-th nested
10174 // loop associated with the loop directive, and di is a constant
10175 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010176 if (CurContext->isDependentContext()) {
10177 // It will be analyzed later.
10178 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010179 continue;
10180 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010181 SimpleExpr = SimpleExpr->IgnoreImplicit();
10182 OverloadedOperatorKind OOK = OO_None;
10183 SourceLocation OOLoc;
10184 Expr *LHS = SimpleExpr;
10185 Expr *RHS = nullptr;
10186 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10187 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10188 OOLoc = BO->getOperatorLoc();
10189 LHS = BO->getLHS()->IgnoreParenImpCasts();
10190 RHS = BO->getRHS()->IgnoreParenImpCasts();
10191 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10192 OOK = OCE->getOperator();
10193 OOLoc = OCE->getOperatorLoc();
10194 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10195 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10196 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10197 OOK = MCE->getMethodDecl()
10198 ->getNameInfo()
10199 .getName()
10200 .getCXXOverloadedOperator();
10201 OOLoc = MCE->getCallee()->getExprLoc();
10202 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10203 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10204 }
10205 SourceLocation ELoc;
10206 SourceRange ERange;
10207 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10208 /*AllowArraySection=*/false);
10209 if (Res.second) {
10210 // It will be analyzed later.
10211 Vars.push_back(RefExpr);
10212 }
10213 ValueDecl *D = Res.first;
10214 if (!D)
10215 continue;
10216
10217 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10218 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10219 continue;
10220 }
10221 if (RHS) {
10222 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10223 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10224 if (RHSRes.isInvalid())
10225 continue;
10226 }
10227 if (!CurContext->isDependentContext() &&
10228 DSAStack->getParentOrderedRegionParam() &&
10229 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10230 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10231 << DSAStack->getParentLoopControlVariable(
10232 DepCounter.getZExtValue());
10233 continue;
10234 }
10235 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010236 } else {
10237 // OpenMP [2.11.1.1, Restrictions, p.3]
10238 // A variable that is part of another variable (such as a field of a
10239 // structure) but is not an array element or an array section cannot
10240 // appear in a depend clause.
10241 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10242 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10243 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10244 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10245 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010246 (ASE &&
10247 !ASE->getBase()
10248 ->getType()
10249 .getNonReferenceType()
10250 ->isPointerType() &&
10251 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010252 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10253 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010254 continue;
10255 }
10256 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010257 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10258 }
10259
10260 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10261 TotalDepCount > VarList.size() &&
10262 DSAStack->getParentOrderedRegionParam()) {
10263 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10264 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10265 }
10266 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10267 Vars.empty())
10268 return nullptr;
10269 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010270 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10271 DepKind, DepLoc, ColonLoc, Vars);
10272 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10273 DSAStack->addDoacrossDependClause(C, OpsOffs);
10274 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010275}
Michael Wonge710d542015-08-07 16:16:36 +000010276
10277OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10278 SourceLocation LParenLoc,
10279 SourceLocation EndLoc) {
10280 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010281
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010282 // OpenMP [2.9.1, Restrictions]
10283 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010284 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10285 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010286 return nullptr;
10287
Michael Wonge710d542015-08-07 16:16:36 +000010288 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10289}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010290
10291static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10292 DSAStackTy *Stack, CXXRecordDecl *RD) {
10293 if (!RD || RD->isInvalidDecl())
10294 return true;
10295
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010296 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10297 if (auto *CTD = CTSD->getSpecializedTemplate())
10298 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010299 auto QTy = SemaRef.Context.getRecordType(RD);
10300 if (RD->isDynamicClass()) {
10301 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10302 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10303 return false;
10304 }
10305 auto *DC = RD;
10306 bool IsCorrect = true;
10307 for (auto *I : DC->decls()) {
10308 if (I) {
10309 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10310 if (MD->isStatic()) {
10311 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10312 SemaRef.Diag(MD->getLocation(),
10313 diag::note_omp_static_member_in_target);
10314 IsCorrect = false;
10315 }
10316 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10317 if (VD->isStaticDataMember()) {
10318 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10319 SemaRef.Diag(VD->getLocation(),
10320 diag::note_omp_static_member_in_target);
10321 IsCorrect = false;
10322 }
10323 }
10324 }
10325 }
10326
10327 for (auto &I : RD->bases()) {
10328 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10329 I.getType()->getAsCXXRecordDecl()))
10330 IsCorrect = false;
10331 }
10332 return IsCorrect;
10333}
10334
10335static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10336 DSAStackTy *Stack, QualType QTy) {
10337 NamedDecl *ND;
10338 if (QTy->isIncompleteType(&ND)) {
10339 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10340 return false;
10341 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10342 if (!RD->isInvalidDecl() &&
10343 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10344 return false;
10345 }
10346 return true;
10347}
10348
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010349/// \brief Return true if it can be proven that the provided array expression
10350/// (array section or array subscript) does NOT specify the whole size of the
10351/// array whose base type is \a BaseQTy.
10352static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10353 const Expr *E,
10354 QualType BaseQTy) {
10355 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10356
10357 // If this is an array subscript, it refers to the whole size if the size of
10358 // the dimension is constant and equals 1. Also, an array section assumes the
10359 // format of an array subscript if no colon is used.
10360 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10361 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10362 return ATy->getSize().getSExtValue() != 1;
10363 // Size can't be evaluated statically.
10364 return false;
10365 }
10366
10367 assert(OASE && "Expecting array section if not an array subscript.");
10368 auto *LowerBound = OASE->getLowerBound();
10369 auto *Length = OASE->getLength();
10370
10371 // If there is a lower bound that does not evaluates to zero, we are not
10372 // convering the whole dimension.
10373 if (LowerBound) {
10374 llvm::APSInt ConstLowerBound;
10375 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10376 return false; // Can't get the integer value as a constant.
10377 if (ConstLowerBound.getSExtValue())
10378 return true;
10379 }
10380
10381 // If we don't have a length we covering the whole dimension.
10382 if (!Length)
10383 return false;
10384
10385 // If the base is a pointer, we don't have a way to get the size of the
10386 // pointee.
10387 if (BaseQTy->isPointerType())
10388 return false;
10389
10390 // We can only check if the length is the same as the size of the dimension
10391 // if we have a constant array.
10392 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10393 if (!CATy)
10394 return false;
10395
10396 llvm::APSInt ConstLength;
10397 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10398 return false; // Can't get the integer value as a constant.
10399
10400 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10401}
10402
10403// Return true if it can be proven that the provided array expression (array
10404// section or array subscript) does NOT specify a single element of the array
10405// whose base type is \a BaseQTy.
10406static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10407 const Expr *E,
10408 QualType BaseQTy) {
10409 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10410
10411 // An array subscript always refer to a single element. Also, an array section
10412 // assumes the format of an array subscript if no colon is used.
10413 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10414 return false;
10415
10416 assert(OASE && "Expecting array section if not an array subscript.");
10417 auto *Length = OASE->getLength();
10418
10419 // If we don't have a length we have to check if the array has unitary size
10420 // for this dimension. Also, we should always expect a length if the base type
10421 // is pointer.
10422 if (!Length) {
10423 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10424 return ATy->getSize().getSExtValue() != 1;
10425 // We cannot assume anything.
10426 return false;
10427 }
10428
10429 // Check if the length evaluates to 1.
10430 llvm::APSInt ConstLength;
10431 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10432 return false; // Can't get the integer value as a constant.
10433
10434 return ConstLength.getSExtValue() != 1;
10435}
10436
Samuel Antao661c0902016-05-26 17:39:58 +000010437// Return the expression of the base of the mappable expression or null if it
10438// cannot be determined and do all the necessary checks to see if the expression
10439// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010440// components of the expression.
10441static Expr *CheckMapClauseExpressionBase(
10442 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010443 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10444 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010445 SourceLocation ELoc = E->getExprLoc();
10446 SourceRange ERange = E->getSourceRange();
10447
10448 // The base of elements of list in a map clause have to be either:
10449 // - a reference to variable or field.
10450 // - a member expression.
10451 // - an array expression.
10452 //
10453 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10454 // reference to 'r'.
10455 //
10456 // If we have:
10457 //
10458 // struct SS {
10459 // Bla S;
10460 // foo() {
10461 // #pragma omp target map (S.Arr[:12]);
10462 // }
10463 // }
10464 //
10465 // We want to retrieve the member expression 'this->S';
10466
10467 Expr *RelevantExpr = nullptr;
10468
Samuel Antao5de996e2016-01-22 20:21:36 +000010469 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10470 // If a list item is an array section, it must specify contiguous storage.
10471 //
10472 // For this restriction it is sufficient that we make sure only references
10473 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010474 // exist except in the rightmost expression (unless they cover the whole
10475 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010476 //
10477 // r.ArrS[3:5].Arr[6:7]
10478 //
10479 // r.ArrS[3:5].x
10480 //
10481 // but these would be valid:
10482 // r.ArrS[3].Arr[6:7]
10483 //
10484 // r.ArrS[3].x
10485
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010486 bool AllowUnitySizeArraySection = true;
10487 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010488
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010489 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010490 E = E->IgnoreParenImpCasts();
10491
10492 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10493 if (!isa<VarDecl>(CurE->getDecl()))
10494 break;
10495
10496 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010497
10498 // If we got a reference to a declaration, we should not expect any array
10499 // section before that.
10500 AllowUnitySizeArraySection = false;
10501 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010502
10503 // Record the component.
10504 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10505 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010506 continue;
10507 }
10508
10509 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10510 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10511
10512 if (isa<CXXThisExpr>(BaseE))
10513 // We found a base expression: this->Val.
10514 RelevantExpr = CurE;
10515 else
10516 E = BaseE;
10517
10518 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10519 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10520 << CurE->getSourceRange();
10521 break;
10522 }
10523
10524 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10525
10526 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10527 // A bit-field cannot appear in a map clause.
10528 //
10529 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010530 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10531 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010532 break;
10533 }
10534
10535 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10536 // If the type of a list item is a reference to a type T then the type
10537 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010538 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010539
10540 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10541 // A list item cannot be a variable that is a member of a structure with
10542 // a union type.
10543 //
10544 if (auto *RT = CurType->getAs<RecordType>())
10545 if (RT->isUnionType()) {
10546 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10547 << CurE->getSourceRange();
10548 break;
10549 }
10550
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010551 // If we got a member expression, we should not expect any array section
10552 // before that:
10553 //
10554 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10555 // If a list item is an element of a structure, only the rightmost symbol
10556 // of the variable reference can be an array section.
10557 //
10558 AllowUnitySizeArraySection = false;
10559 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010560
10561 // Record the component.
10562 CurComponents.push_back(
10563 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010564 continue;
10565 }
10566
10567 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10568 E = CurE->getBase()->IgnoreParenImpCasts();
10569
10570 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10571 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10572 << 0 << CurE->getSourceRange();
10573 break;
10574 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010575
10576 // If we got an array subscript that express the whole dimension we
10577 // can have any array expressions before. If it only expressing part of
10578 // the dimension, we can only have unitary-size array expressions.
10579 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10580 E->getType()))
10581 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010582
10583 // Record the component - we don't have any declaration associated.
10584 CurComponents.push_back(
10585 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010586 continue;
10587 }
10588
10589 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010590 E = CurE->getBase()->IgnoreParenImpCasts();
10591
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010592 auto CurType =
10593 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10594
Samuel Antao5de996e2016-01-22 20:21:36 +000010595 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10596 // If the type of a list item is a reference to a type T then the type
10597 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010598 if (CurType->isReferenceType())
10599 CurType = CurType->getPointeeType();
10600
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010601 bool IsPointer = CurType->isAnyPointerType();
10602
10603 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010604 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10605 << 0 << CurE->getSourceRange();
10606 break;
10607 }
10608
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010609 bool NotWhole =
10610 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10611 bool NotUnity =
10612 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10613
10614 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
10615 // Any array section is currently allowed.
10616 //
10617 // If this array section refers to the whole dimension we can still
10618 // accept other array sections before this one, except if the base is a
10619 // pointer. Otherwise, only unitary sections are accepted.
10620 if (NotWhole || IsPointer)
10621 AllowWholeSizeArraySection = false;
10622 } else if ((AllowUnitySizeArraySection && NotUnity) ||
10623 (AllowWholeSizeArraySection && NotWhole)) {
10624 // A unity or whole array section is not allowed and that is not
10625 // compatible with the properties of the current array section.
10626 SemaRef.Diag(
10627 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10628 << CurE->getSourceRange();
10629 break;
10630 }
Samuel Antao90927002016-04-26 14:54:23 +000010631
10632 // Record the component - we don't have any declaration associated.
10633 CurComponents.push_back(
10634 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010635 continue;
10636 }
10637
10638 // If nothing else worked, this is not a valid map clause expression.
10639 SemaRef.Diag(ELoc,
10640 diag::err_omp_expected_named_var_member_or_array_expression)
10641 << ERange;
10642 break;
10643 }
10644
10645 return RelevantExpr;
10646}
10647
10648// Return true if expression E associated with value VD has conflicts with other
10649// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010650static bool CheckMapConflicts(
10651 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10652 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010653 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10654 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010655 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010656 SourceLocation ELoc = E->getExprLoc();
10657 SourceRange ERange = E->getSourceRange();
10658
10659 // In order to easily check the conflicts we need to match each component of
10660 // the expression under test with the components of the expressions that are
10661 // already in the stack.
10662
Samuel Antao5de996e2016-01-22 20:21:36 +000010663 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010664 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010665 "Map clause expression with unexpected base!");
10666
10667 // Variables to help detecting enclosing problems in data environment nests.
10668 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010669 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010670
Samuel Antao90927002016-04-26 14:54:23 +000010671 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10672 VD, CurrentRegionOnly,
10673 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10674 StackComponents) -> bool {
10675
Samuel Antao5de996e2016-01-22 20:21:36 +000010676 assert(!StackComponents.empty() &&
10677 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010678 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010679 "Map clause expression with unexpected base!");
10680
Samuel Antao90927002016-04-26 14:54:23 +000010681 // The whole expression in the stack.
10682 auto *RE = StackComponents.front().getAssociatedExpression();
10683
Samuel Antao5de996e2016-01-22 20:21:36 +000010684 // Expressions must start from the same base. Here we detect at which
10685 // point both expressions diverge from each other and see if we can
10686 // detect if the memory referred to both expressions is contiguous and
10687 // do not overlap.
10688 auto CI = CurComponents.rbegin();
10689 auto CE = CurComponents.rend();
10690 auto SI = StackComponents.rbegin();
10691 auto SE = StackComponents.rend();
10692 for (; CI != CE && SI != SE; ++CI, ++SI) {
10693
10694 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10695 // At most one list item can be an array item derived from a given
10696 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010697 if (CurrentRegionOnly &&
10698 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10699 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10700 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10701 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10702 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010703 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010704 << CI->getAssociatedExpression()->getSourceRange();
10705 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10706 diag::note_used_here)
10707 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010708 return true;
10709 }
10710
10711 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010712 if (CI->getAssociatedExpression()->getStmtClass() !=
10713 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010714 break;
10715
10716 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010717 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010718 break;
10719 }
10720
10721 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10722 // List items of map clauses in the same construct must not share
10723 // original storage.
10724 //
10725 // If the expressions are exactly the same or one is a subset of the
10726 // other, it means they are sharing storage.
10727 if (CI == CE && SI == SE) {
10728 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010729 if (CKind == OMPC_map)
10730 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10731 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010732 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010733 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10734 << ERange;
10735 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010736 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10737 << RE->getSourceRange();
10738 return true;
10739 } else {
10740 // If we find the same expression in the enclosing data environment,
10741 // that is legal.
10742 IsEnclosedByDataEnvironmentExpr = true;
10743 return false;
10744 }
10745 }
10746
Samuel Antao90927002016-04-26 14:54:23 +000010747 QualType DerivedType =
10748 std::prev(CI)->getAssociatedDeclaration()->getType();
10749 SourceLocation DerivedLoc =
10750 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010751
10752 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10753 // If the type of a list item is a reference to a type T then the type
10754 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010755 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010756
10757 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10758 // A variable for which the type is pointer and an array section
10759 // derived from that variable must not appear as list items of map
10760 // clauses of the same construct.
10761 //
10762 // Also, cover one of the cases in:
10763 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10764 // If any part of the original storage of a list item has corresponding
10765 // storage in the device data environment, all of the original storage
10766 // must have corresponding storage in the device data environment.
10767 //
10768 if (DerivedType->isAnyPointerType()) {
10769 if (CI == CE || SI == SE) {
10770 SemaRef.Diag(
10771 DerivedLoc,
10772 diag::err_omp_pointer_mapped_along_with_derived_section)
10773 << DerivedLoc;
10774 } else {
10775 assert(CI != CE && SI != SE);
10776 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10777 << DerivedLoc;
10778 }
10779 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10780 << RE->getSourceRange();
10781 return true;
10782 }
10783
10784 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10785 // List items of map clauses in the same construct must not share
10786 // original storage.
10787 //
10788 // An expression is a subset of the other.
10789 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010790 if (CKind == OMPC_map)
10791 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10792 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010793 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010794 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10795 << ERange;
10796 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010797 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10798 << RE->getSourceRange();
10799 return true;
10800 }
10801
10802 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010803 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010804 if (!CurrentRegionOnly && SI != SE)
10805 EnclosingExpr = RE;
10806
10807 // The current expression is a subset of the expression in the data
10808 // environment.
10809 IsEnclosedByDataEnvironmentExpr |=
10810 (!CurrentRegionOnly && CI != CE && SI == SE);
10811
10812 return false;
10813 });
10814
10815 if (CurrentRegionOnly)
10816 return FoundError;
10817
10818 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10819 // If any part of the original storage of a list item has corresponding
10820 // storage in the device data environment, all of the original storage must
10821 // have corresponding storage in the device data environment.
10822 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10823 // If a list item is an element of a structure, and a different element of
10824 // the structure has a corresponding list item in the device data environment
10825 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010826 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010827 // data environment prior to the task encountering the construct.
10828 //
10829 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10830 SemaRef.Diag(ELoc,
10831 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10832 << ERange;
10833 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10834 << EnclosingExpr->getSourceRange();
10835 return true;
10836 }
10837
10838 return FoundError;
10839}
10840
Samuel Antao661c0902016-05-26 17:39:58 +000010841namespace {
10842// Utility struct that gathers all the related lists associated with a mappable
10843// expression.
10844struct MappableVarListInfo final {
10845 // The list of expressions.
10846 ArrayRef<Expr *> VarList;
10847 // The list of processed expressions.
10848 SmallVector<Expr *, 16> ProcessedVarList;
10849 // The mappble components for each expression.
10850 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10851 // The base declaration of the variable.
10852 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10853
10854 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10855 // We have a list of components and base declarations for each entry in the
10856 // variable list.
10857 VarComponents.reserve(VarList.size());
10858 VarBaseDeclarations.reserve(VarList.size());
10859 }
10860};
10861}
10862
10863// Check the validity of the provided variable list for the provided clause kind
10864// \a CKind. In the check process the valid expressions, and mappable expression
10865// components and variables are extracted and used to fill \a Vars,
10866// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10867// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10868static void
10869checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10870 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10871 SourceLocation StartLoc,
10872 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10873 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010874 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10875 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010876 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010877
Samuel Antao90927002016-04-26 14:54:23 +000010878 // Keep track of the mappable components and base declarations in this clause.
10879 // Each entry in the list is going to have a list of components associated. We
10880 // record each set of the components so that we can build the clause later on.
10881 // In the end we should have the same amount of declarations and component
10882 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010883
Samuel Antao661c0902016-05-26 17:39:58 +000010884 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010885 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010886 SourceLocation ELoc = RE->getExprLoc();
10887
Kelvin Li0bff7af2015-11-23 05:32:03 +000010888 auto *VE = RE->IgnoreParenLValueCasts();
10889
10890 if (VE->isValueDependent() || VE->isTypeDependent() ||
10891 VE->isInstantiationDependent() ||
10892 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010893 // We can only analyze this information once the missing information is
10894 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000010895 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010896 continue;
10897 }
10898
10899 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010900
Samuel Antao5de996e2016-01-22 20:21:36 +000010901 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010902 SemaRef.Diag(ELoc,
10903 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000010904 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010905 continue;
10906 }
10907
Samuel Antao90927002016-04-26 14:54:23 +000010908 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10909 ValueDecl *CurDeclaration = nullptr;
10910
10911 // Obtain the array or member expression bases if required. Also, fill the
10912 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000010913 auto *BE =
10914 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010915 if (!BE)
10916 continue;
10917
Samuel Antao90927002016-04-26 14:54:23 +000010918 assert(!CurComponents.empty() &&
10919 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010920
Samuel Antao90927002016-04-26 14:54:23 +000010921 // For the following checks, we rely on the base declaration which is
10922 // expected to be associated with the last component. The declaration is
10923 // expected to be a variable or a field (if 'this' is being mapped).
10924 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10925 assert(CurDeclaration && "Null decl on map clause.");
10926 assert(
10927 CurDeclaration->isCanonicalDecl() &&
10928 "Expecting components to have associated only canonical declarations.");
10929
10930 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10931 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010932
10933 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010934 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010935
10936 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000010937 // threadprivate variables cannot appear in a map clause.
10938 // OpenMP 4.5 [2.10.5, target update Construct]
10939 // threadprivate variables cannot appear in a from clause.
10940 if (VD && DSAS->isThreadPrivate(VD)) {
10941 auto DVar = DSAS->getTopDSA(VD, false);
10942 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10943 << getOpenMPClauseName(CKind);
10944 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010945 continue;
10946 }
10947
Samuel Antao5de996e2016-01-22 20:21:36 +000010948 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10949 // A list item cannot appear in both a map clause and a data-sharing
10950 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010951
Samuel Antao5de996e2016-01-22 20:21:36 +000010952 // Check conflicts with other map clause expressions. We check the conflicts
10953 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000010954 // environment, because the restrictions are different. We only have to
10955 // check conflicts across regions for the map clauses.
10956 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10957 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010958 break;
Samuel Antao661c0902016-05-26 17:39:58 +000010959 if (CKind == OMPC_map &&
10960 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10961 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010962 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010963
Samuel Antao661c0902016-05-26 17:39:58 +000010964 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000010965 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10966 // If the type of a list item is a reference to a type T then the type will
10967 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010968 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010969
Samuel Antao661c0902016-05-26 17:39:58 +000010970 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10971 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000010972 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010973 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000010974 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10975 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010976 continue;
10977
Samuel Antao661c0902016-05-26 17:39:58 +000010978 if (CKind == OMPC_map) {
10979 // target enter data
10980 // OpenMP [2.10.2, Restrictions, p. 99]
10981 // A map-type must be specified in all map clauses and must be either
10982 // to or alloc.
10983 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10984 if (DKind == OMPD_target_enter_data &&
10985 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10986 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10987 << (IsMapTypeImplicit ? 1 : 0)
10988 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10989 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010990 continue;
10991 }
Samuel Antao661c0902016-05-26 17:39:58 +000010992
10993 // target exit_data
10994 // OpenMP [2.10.3, Restrictions, p. 102]
10995 // A map-type must be specified in all map clauses and must be either
10996 // from, release, or delete.
10997 if (DKind == OMPD_target_exit_data &&
10998 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10999 MapType == OMPC_MAP_delete)) {
11000 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11001 << (IsMapTypeImplicit ? 1 : 0)
11002 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11003 << getOpenMPDirectiveName(DKind);
11004 continue;
11005 }
11006
11007 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11008 // A list item cannot appear in both a map clause and a data-sharing
11009 // attribute clause on the same construct
11010 if (DKind == OMPD_target && VD) {
11011 auto DVar = DSAS->getTopDSA(VD, false);
11012 if (isOpenMPPrivate(DVar.CKind)) {
11013 SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
11014 << getOpenMPClauseName(DVar.CKind)
11015 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11016 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11017 continue;
11018 }
11019 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011020 }
11021
Samuel Antao90927002016-04-26 14:54:23 +000011022 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000011023 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000011024
11025 // Store the components in the stack so that they can be used to check
11026 // against other clauses later on.
Samuel Antao661c0902016-05-26 17:39:58 +000011027 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
Samuel Antao90927002016-04-26 14:54:23 +000011028
11029 // Save the components and declaration to create the clause. For purposes of
11030 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000011031 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000011032 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11033 MVLI.VarComponents.back().append(CurComponents.begin(),
11034 CurComponents.end());
11035 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11036 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011037 }
Samuel Antao661c0902016-05-26 17:39:58 +000011038}
11039
11040OMPClause *
11041Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11042 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11043 SourceLocation MapLoc, SourceLocation ColonLoc,
11044 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11045 SourceLocation LParenLoc, SourceLocation EndLoc) {
11046 MappableVarListInfo MVLI(VarList);
11047 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11048 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011049
Samuel Antao5de996e2016-01-22 20:21:36 +000011050 // We need to produce a map clause even if we don't have variables so that
11051 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000011052 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11053 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11054 MVLI.VarComponents, MapTypeModifier, MapType,
11055 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011056}
Kelvin Li099bb8c2015-11-24 20:50:12 +000011057
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011058QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11059 TypeResult ParsedType) {
11060 assert(ParsedType.isUsable());
11061
11062 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11063 if (ReductionType.isNull())
11064 return QualType();
11065
11066 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11067 // A type name in a declare reduction directive cannot be a function type, an
11068 // array type, a reference type, or a type qualified with const, volatile or
11069 // restrict.
11070 if (ReductionType.hasQualifiers()) {
11071 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11072 return QualType();
11073 }
11074
11075 if (ReductionType->isFunctionType()) {
11076 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11077 return QualType();
11078 }
11079 if (ReductionType->isReferenceType()) {
11080 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11081 return QualType();
11082 }
11083 if (ReductionType->isArrayType()) {
11084 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11085 return QualType();
11086 }
11087 return ReductionType;
11088}
11089
11090Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11091 Scope *S, DeclContext *DC, DeclarationName Name,
11092 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11093 AccessSpecifier AS, Decl *PrevDeclInScope) {
11094 SmallVector<Decl *, 8> Decls;
11095 Decls.reserve(ReductionTypes.size());
11096
11097 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11098 ForRedeclaration);
11099 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11100 // A reduction-identifier may not be re-declared in the current scope for the
11101 // same type or for a type that is compatible according to the base language
11102 // rules.
11103 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11104 OMPDeclareReductionDecl *PrevDRD = nullptr;
11105 bool InCompoundScope = true;
11106 if (S != nullptr) {
11107 // Find previous declaration with the same name not referenced in other
11108 // declarations.
11109 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11110 InCompoundScope =
11111 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11112 LookupName(Lookup, S);
11113 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11114 /*AllowInlineNamespace=*/false);
11115 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11116 auto Filter = Lookup.makeFilter();
11117 while (Filter.hasNext()) {
11118 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11119 if (InCompoundScope) {
11120 auto I = UsedAsPrevious.find(PrevDecl);
11121 if (I == UsedAsPrevious.end())
11122 UsedAsPrevious[PrevDecl] = false;
11123 if (auto *D = PrevDecl->getPrevDeclInScope())
11124 UsedAsPrevious[D] = true;
11125 }
11126 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11127 PrevDecl->getLocation();
11128 }
11129 Filter.done();
11130 if (InCompoundScope) {
11131 for (auto &PrevData : UsedAsPrevious) {
11132 if (!PrevData.second) {
11133 PrevDRD = PrevData.first;
11134 break;
11135 }
11136 }
11137 }
11138 } else if (PrevDeclInScope != nullptr) {
11139 auto *PrevDRDInScope = PrevDRD =
11140 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11141 do {
11142 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11143 PrevDRDInScope->getLocation();
11144 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11145 } while (PrevDRDInScope != nullptr);
11146 }
11147 for (auto &TyData : ReductionTypes) {
11148 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11149 bool Invalid = false;
11150 if (I != PreviousRedeclTypes.end()) {
11151 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11152 << TyData.first;
11153 Diag(I->second, diag::note_previous_definition);
11154 Invalid = true;
11155 }
11156 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11157 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11158 Name, TyData.first, PrevDRD);
11159 DC->addDecl(DRD);
11160 DRD->setAccess(AS);
11161 Decls.push_back(DRD);
11162 if (Invalid)
11163 DRD->setInvalidDecl();
11164 else
11165 PrevDRD = DRD;
11166 }
11167
11168 return DeclGroupPtrTy::make(
11169 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11170}
11171
11172void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11173 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11174
11175 // Enter new function scope.
11176 PushFunctionScope();
11177 getCurFunction()->setHasBranchProtectedScope();
11178 getCurFunction()->setHasOMPDeclareReductionCombiner();
11179
11180 if (S != nullptr)
11181 PushDeclContext(S, DRD);
11182 else
11183 CurContext = DRD;
11184
11185 PushExpressionEvaluationContext(PotentiallyEvaluated);
11186
11187 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011188 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11189 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11190 // uses semantics of argument handles by value, but it should be passed by
11191 // reference. C lang does not support references, so pass all parameters as
11192 // pointers.
11193 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011194 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011195 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011196 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11197 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11198 // uses semantics of argument handles by value, but it should be passed by
11199 // reference. C lang does not support references, so pass all parameters as
11200 // pointers.
11201 // Create 'T omp_out;' variable.
11202 auto *OmpOutParm =
11203 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11204 if (S != nullptr) {
11205 PushOnScopeChains(OmpInParm, S);
11206 PushOnScopeChains(OmpOutParm, S);
11207 } else {
11208 DRD->addDecl(OmpInParm);
11209 DRD->addDecl(OmpOutParm);
11210 }
11211}
11212
11213void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11214 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11215 DiscardCleanupsInEvaluationContext();
11216 PopExpressionEvaluationContext();
11217
11218 PopDeclContext();
11219 PopFunctionScopeInfo();
11220
11221 if (Combiner != nullptr)
11222 DRD->setCombiner(Combiner);
11223 else
11224 DRD->setInvalidDecl();
11225}
11226
11227void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11228 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11229
11230 // Enter new function scope.
11231 PushFunctionScope();
11232 getCurFunction()->setHasBranchProtectedScope();
11233
11234 if (S != nullptr)
11235 PushDeclContext(S, DRD);
11236 else
11237 CurContext = DRD;
11238
11239 PushExpressionEvaluationContext(PotentiallyEvaluated);
11240
11241 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011242 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11243 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11244 // uses semantics of argument handles by value, but it should be passed by
11245 // reference. C lang does not support references, so pass all parameters as
11246 // pointers.
11247 // Create 'T omp_priv;' variable.
11248 auto *OmpPrivParm =
11249 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011250 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11251 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11252 // uses semantics of argument handles by value, but it should be passed by
11253 // reference. C lang does not support references, so pass all parameters as
11254 // pointers.
11255 // Create 'T omp_orig;' variable.
11256 auto *OmpOrigParm =
11257 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011258 if (S != nullptr) {
11259 PushOnScopeChains(OmpPrivParm, S);
11260 PushOnScopeChains(OmpOrigParm, S);
11261 } else {
11262 DRD->addDecl(OmpPrivParm);
11263 DRD->addDecl(OmpOrigParm);
11264 }
11265}
11266
11267void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11268 Expr *Initializer) {
11269 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11270 DiscardCleanupsInEvaluationContext();
11271 PopExpressionEvaluationContext();
11272
11273 PopDeclContext();
11274 PopFunctionScopeInfo();
11275
11276 if (Initializer != nullptr)
11277 DRD->setInitializer(Initializer);
11278 else
11279 DRD->setInvalidDecl();
11280}
11281
11282Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11283 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11284 for (auto *D : DeclReductions.get()) {
11285 if (IsValid) {
11286 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11287 if (S != nullptr)
11288 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11289 } else
11290 D->setInvalidDecl();
11291 }
11292 return DeclReductions;
11293}
11294
Kelvin Li099bb8c2015-11-24 20:50:12 +000011295OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11296 SourceLocation StartLoc,
11297 SourceLocation LParenLoc,
11298 SourceLocation EndLoc) {
11299 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011300
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011301 // OpenMP [teams Constrcut, Restrictions]
11302 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011303 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11304 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011305 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011306
11307 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11308}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011309
11310OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11311 SourceLocation StartLoc,
11312 SourceLocation LParenLoc,
11313 SourceLocation EndLoc) {
11314 Expr *ValExpr = ThreadLimit;
11315
11316 // OpenMP [teams Constrcut, Restrictions]
11317 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011318 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11319 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011320 return nullptr;
11321
11322 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11323 EndLoc);
11324}
Alexey Bataeva0569352015-12-01 10:17:31 +000011325
11326OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11327 SourceLocation StartLoc,
11328 SourceLocation LParenLoc,
11329 SourceLocation EndLoc) {
11330 Expr *ValExpr = Priority;
11331
11332 // OpenMP [2.9.1, task Constrcut]
11333 // The priority-value is a non-negative numerical scalar expression.
11334 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11335 /*StrictlyPositive=*/false))
11336 return nullptr;
11337
11338 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11339}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011340
11341OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11342 SourceLocation StartLoc,
11343 SourceLocation LParenLoc,
11344 SourceLocation EndLoc) {
11345 Expr *ValExpr = Grainsize;
11346
11347 // OpenMP [2.9.2, taskloop Constrcut]
11348 // The parameter of the grainsize clause must be a positive integer
11349 // expression.
11350 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11351 /*StrictlyPositive=*/true))
11352 return nullptr;
11353
11354 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11355}
Alexey Bataev382967a2015-12-08 12:06:20 +000011356
11357OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11358 SourceLocation StartLoc,
11359 SourceLocation LParenLoc,
11360 SourceLocation EndLoc) {
11361 Expr *ValExpr = NumTasks;
11362
11363 // OpenMP [2.9.2, taskloop Constrcut]
11364 // The parameter of the num_tasks clause must be a positive integer
11365 // expression.
11366 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11367 /*StrictlyPositive=*/true))
11368 return nullptr;
11369
11370 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11371}
11372
Alexey Bataev28c75412015-12-15 08:19:24 +000011373OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11374 SourceLocation LParenLoc,
11375 SourceLocation EndLoc) {
11376 // OpenMP [2.13.2, critical construct, Description]
11377 // ... where hint-expression is an integer constant expression that evaluates
11378 // to a valid lock hint.
11379 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11380 if (HintExpr.isInvalid())
11381 return nullptr;
11382 return new (Context)
11383 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11384}
11385
Carlo Bertollib4adf552016-01-15 18:50:31 +000011386OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11387 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11388 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11389 SourceLocation EndLoc) {
11390 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11391 std::string Values;
11392 Values += "'";
11393 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11394 Values += "'";
11395 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11396 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11397 return nullptr;
11398 }
11399 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011400 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011401 if (ChunkSize) {
11402 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11403 !ChunkSize->isInstantiationDependent() &&
11404 !ChunkSize->containsUnexpandedParameterPack()) {
11405 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11406 ExprResult Val =
11407 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11408 if (Val.isInvalid())
11409 return nullptr;
11410
11411 ValExpr = Val.get();
11412
11413 // OpenMP [2.7.1, Restrictions]
11414 // chunk_size must be a loop invariant integer expression with a positive
11415 // value.
11416 llvm::APSInt Result;
11417 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11418 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11419 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11420 << "dist_schedule" << ChunkSize->getSourceRange();
11421 return nullptr;
11422 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011423 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11424 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011425 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11426 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11427 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011428 }
11429 }
11430 }
11431
11432 return new (Context)
11433 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011434 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011435}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011436
11437OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11438 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11439 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11440 SourceLocation KindLoc, SourceLocation EndLoc) {
11441 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11442 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11443 Kind != OMPC_DEFAULTMAP_scalar) {
11444 std::string Value;
11445 SourceLocation Loc;
11446 Value += "'";
11447 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11448 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11449 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11450 Loc = MLoc;
11451 } else {
11452 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11453 OMPC_DEFAULTMAP_scalar);
11454 Loc = KindLoc;
11455 }
11456 Value += "'";
11457 Diag(Loc, diag::err_omp_unexpected_clause_value)
11458 << Value << getOpenMPClauseName(OMPC_defaultmap);
11459 return nullptr;
11460 }
11461
11462 return new (Context)
11463 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11464}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011465
11466bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11467 DeclContext *CurLexicalContext = getCurLexicalContext();
11468 if (!CurLexicalContext->isFileContext() &&
11469 !CurLexicalContext->isExternCContext() &&
11470 !CurLexicalContext->isExternCXXContext()) {
11471 Diag(Loc, diag::err_omp_region_not_file_context);
11472 return false;
11473 }
11474 if (IsInOpenMPDeclareTargetContext) {
11475 Diag(Loc, diag::err_omp_enclosed_declare_target);
11476 return false;
11477 }
11478
11479 IsInOpenMPDeclareTargetContext = true;
11480 return true;
11481}
11482
11483void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11484 assert(IsInOpenMPDeclareTargetContext &&
11485 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11486
11487 IsInOpenMPDeclareTargetContext = false;
11488}
11489
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011490void
11491Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11492 const DeclarationNameInfo &Id,
11493 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11494 NamedDeclSetType &SameDirectiveDecls) {
11495 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11496 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11497
11498 if (Lookup.isAmbiguous())
11499 return;
11500 Lookup.suppressDiagnostics();
11501
11502 if (!Lookup.isSingleResult()) {
11503 if (TypoCorrection Corrected =
11504 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11505 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11506 CTK_ErrorRecovery)) {
11507 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11508 << Id.getName());
11509 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11510 return;
11511 }
11512
11513 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11514 return;
11515 }
11516
11517 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11518 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11519 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11520 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11521
11522 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11523 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11524 ND->addAttr(A);
11525 if (ASTMutationListener *ML = Context.getASTMutationListener())
11526 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11527 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11528 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11529 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11530 << Id.getName();
11531 }
11532 } else
11533 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11534}
11535
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011536static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11537 Sema &SemaRef, Decl *D) {
11538 if (!D)
11539 return;
11540 Decl *LD = nullptr;
11541 if (isa<TagDecl>(D)) {
11542 LD = cast<TagDecl>(D)->getDefinition();
11543 } else if (isa<VarDecl>(D)) {
11544 LD = cast<VarDecl>(D)->getDefinition();
11545
11546 // If this is an implicit variable that is legal and we do not need to do
11547 // anything.
11548 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011549 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11550 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11551 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011552 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011553 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011554 return;
11555 }
11556
11557 } else if (isa<FunctionDecl>(D)) {
11558 const FunctionDecl *FD = nullptr;
11559 if (cast<FunctionDecl>(D)->hasBody(FD))
11560 LD = const_cast<FunctionDecl *>(FD);
11561
11562 // If the definition is associated with the current declaration in the
11563 // target region (it can be e.g. a lambda) that is legal and we do not need
11564 // to do anything else.
11565 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011566 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11567 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11568 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011569 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011570 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011571 return;
11572 }
11573 }
11574 if (!LD)
11575 LD = D;
11576 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11577 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11578 // Outlined declaration is not declared target.
11579 if (LD->isOutOfLine()) {
11580 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11581 SemaRef.Diag(SL, diag::note_used_here) << SR;
11582 } else {
11583 DeclContext *DC = LD->getDeclContext();
11584 while (DC) {
11585 if (isa<FunctionDecl>(DC) &&
11586 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11587 break;
11588 DC = DC->getParent();
11589 }
11590 if (DC)
11591 return;
11592
11593 // Is not declared in target context.
11594 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11595 SemaRef.Diag(SL, diag::note_used_here) << SR;
11596 }
11597 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011598 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11599 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11600 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011601 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011602 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011603 }
11604}
11605
11606static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11607 Sema &SemaRef, DSAStackTy *Stack,
11608 ValueDecl *VD) {
11609 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11610 return true;
11611 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11612 return false;
11613 return true;
11614}
11615
11616void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11617 if (!D || D->isInvalidDecl())
11618 return;
11619 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11620 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11621 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11622 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11623 if (DSAStack->isThreadPrivate(VD)) {
11624 Diag(SL, diag::err_omp_threadprivate_in_target);
11625 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11626 return;
11627 }
11628 }
11629 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11630 // Problem if any with var declared with incomplete type will be reported
11631 // as normal, so no need to check it here.
11632 if ((E || !VD->getType()->isIncompleteType()) &&
11633 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11634 // Mark decl as declared target to prevent further diagnostic.
11635 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011636 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11637 Context, OMPDeclareTargetDeclAttr::MT_To);
11638 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011639 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011640 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011641 }
11642 return;
11643 }
11644 }
11645 if (!E) {
11646 // Checking declaration inside declare target region.
11647 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11648 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011649 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11650 Context, OMPDeclareTargetDeclAttr::MT_To);
11651 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011652 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011653 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011654 }
11655 return;
11656 }
11657 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11658}
Samuel Antao661c0902016-05-26 17:39:58 +000011659
11660OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11661 SourceLocation StartLoc,
11662 SourceLocation LParenLoc,
11663 SourceLocation EndLoc) {
11664 MappableVarListInfo MVLI(VarList);
11665 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11666 if (MVLI.ProcessedVarList.empty())
11667 return nullptr;
11668
11669 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11670 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11671 MVLI.VarComponents);
11672}
Samuel Antaoec172c62016-05-26 17:49:04 +000011673
11674OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11675 SourceLocation StartLoc,
11676 SourceLocation LParenLoc,
11677 SourceLocation EndLoc) {
11678 MappableVarListInfo MVLI(VarList);
11679 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11680 if (MVLI.ProcessedVarList.empty())
11681 return nullptr;
11682
11683 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11684 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11685 MVLI.VarComponents);
11686}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011687
11688OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11689 SourceLocation StartLoc,
11690 SourceLocation LParenLoc,
11691 SourceLocation EndLoc) {
11692 SmallVector<Expr *, 8> Vars;
11693 for (auto &RefExpr : VarList) {
11694 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11695 SourceLocation ELoc;
11696 SourceRange ERange;
11697 Expr *SimpleRefExpr = RefExpr;
11698 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11699 if (Res.second) {
11700 // It will be analyzed later.
11701 Vars.push_back(RefExpr);
11702 }
11703 ValueDecl *D = Res.first;
11704 if (!D)
11705 continue;
11706
11707 QualType Type = D->getType();
11708 // item should be a pointer or reference to pointer
11709 if (!Type.getNonReferenceType()->isPointerType()) {
11710 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11711 << 0 << RefExpr->getSourceRange();
11712 continue;
11713 }
11714 Vars.push_back(RefExpr->IgnoreParens());
11715 }
11716
11717 if (Vars.empty())
11718 return nullptr;
11719
11720 return OMPUseDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11721 Vars);
11722}
Carlo Bertolli70594e92016-07-13 17:16:49 +000011723
11724OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11725 SourceLocation StartLoc,
11726 SourceLocation LParenLoc,
11727 SourceLocation EndLoc) {
11728 SmallVector<Expr *, 8> Vars;
11729 for (auto &RefExpr : VarList) {
11730 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11731 SourceLocation ELoc;
11732 SourceRange ERange;
11733 Expr *SimpleRefExpr = RefExpr;
11734 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11735 if (Res.second) {
11736 // It will be analyzed later.
11737 Vars.push_back(RefExpr);
11738 }
11739 ValueDecl *D = Res.first;
11740 if (!D)
11741 continue;
11742
11743 QualType Type = D->getType();
11744 // item should be a pointer or array or reference to pointer or array
11745 if (!Type.getNonReferenceType()->isPointerType() &&
11746 !Type.getNonReferenceType()->isArrayType()) {
11747 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11748 << 0 << RefExpr->getSourceRange();
11749 continue;
11750 }
11751 Vars.push_back(RefExpr->IgnoreParens());
11752 }
11753
11754 if (Vars.empty())
11755 return nullptr;
11756
11757 return OMPIsDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11758 Vars);
11759}