blob: d55d6ecc79a813554da771c28c81407eded29028 [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 Bataeve3978122016-07-19 05:06:39 +0000774 if (FromParent && StartI != EndI)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000775 StartI = std::next(StartI);
Alexey Bataeve3978122016-07-19 05:06:39 +0000776 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000777 return DSAVarData();
Alexey Bataeve3978122016-07-19 05:06:39 +0000778 DSAVarData DVar = getDSA(StartI, D);
779 return CPred(DVar.CKind) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000780}
781
Alexey Bataevaac108a2015-06-23 04:51:00 +0000782bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000783 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000784 unsigned Level, bool NotLastprivate) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000785 if (CPred(ClauseKindMode))
786 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000787 D = getCanonicalDecl(D);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000788 auto StartI = std::next(Stack.begin());
789 auto EndI = Stack.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000790 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000791 return false;
792 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000793 return (StartI->SharingMap.count(D) > 0) &&
794 StartI->SharingMap[D].RefExpr.getPointer() &&
795 CPred(StartI->SharingMap[D].Attributes) &&
796 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +0000797}
798
Samuel Antao4be30e92015-10-02 17:14:03 +0000799bool DSAStackTy::hasExplicitDirective(
800 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
801 unsigned Level) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000802 auto StartI = std::next(Stack.begin());
803 auto EndI = Stack.end();
Samuel Antao4be30e92015-10-02 17:14:03 +0000804 if (std::distance(StartI, EndI) <= (int)Level)
805 return false;
806 std::advance(StartI, Level);
807 return DPred(StartI->Directive);
808}
809
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000810bool DSAStackTy::hasDirective(
811 const llvm::function_ref<bool(OpenMPDirectiveKind,
812 const DeclarationNameInfo &, SourceLocation)>
813 &DPred,
814 bool FromParent) {
Samuel Antaof0d79752016-05-27 15:21:27 +0000815 // We look only in the enclosing region.
816 if (Stack.size() < 2)
817 return false;
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000818 auto StartI = std::next(Stack.rbegin());
819 auto EndI = std::prev(Stack.rend());
820 if (FromParent && StartI != EndI) {
821 StartI = std::next(StartI);
822 }
823 for (auto I = StartI, EE = EndI; I != EE; ++I) {
824 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
825 return true;
826 }
827 return false;
828}
829
Alexey Bataev758e55e2013-09-06 18:03:48 +0000830void Sema::InitDataSharingAttributesStack() {
831 VarDataSharingAttributesStack = new DSAStackTy(*this);
832}
833
834#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
835
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000836bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000837 assert(LangOpts.OpenMP && "OpenMP is not allowed");
838
839 auto &Ctx = getASTContext();
840 bool IsByRef = true;
841
842 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000843 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000844
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000845 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000846 // This table summarizes how a given variable should be passed to the device
847 // given its type and the clauses where it appears. This table is based on
848 // the description in OpenMP 4.5 [2.10.4, target Construct] and
849 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
850 //
851 // =========================================================================
852 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
853 // | |(tofrom:scalar)| | pvt | | | |
854 // =========================================================================
855 // | scl | | | | - | | bycopy|
856 // | scl | | - | x | - | - | bycopy|
857 // | scl | | x | - | - | - | null |
858 // | scl | x | | | - | | byref |
859 // | scl | x | - | x | - | - | bycopy|
860 // | scl | x | x | - | - | - | null |
861 // | scl | | - | - | - | x | byref |
862 // | scl | x | - | - | - | x | byref |
863 //
864 // | agg | n.a. | | | - | | byref |
865 // | agg | n.a. | - | x | - | - | byref |
866 // | agg | n.a. | x | - | - | - | null |
867 // | agg | n.a. | - | - | - | x | byref |
868 // | agg | n.a. | - | - | - | x[] | byref |
869 //
870 // | ptr | n.a. | | | - | | bycopy|
871 // | ptr | n.a. | - | x | - | - | bycopy|
872 // | ptr | n.a. | x | - | - | - | null |
873 // | ptr | n.a. | - | - | - | x | byref |
874 // | ptr | n.a. | - | - | - | x[] | bycopy|
875 // | ptr | n.a. | - | - | x | | bycopy|
876 // | ptr | n.a. | - | - | x | x | bycopy|
877 // | ptr | n.a. | - | - | x | x[] | bycopy|
878 // =========================================================================
879 // Legend:
880 // scl - scalar
881 // ptr - pointer
882 // agg - aggregate
883 // x - applies
884 // - - invalid in this combination
885 // [] - mapped with an array section
886 // byref - should be mapped by reference
887 // byval - should be mapped by value
888 // null - initialize a local variable to null on the device
889 //
890 // Observations:
891 // - All scalar declarations that show up in a map clause have to be passed
892 // by reference, because they may have been mapped in the enclosing data
893 // environment.
894 // - If the scalar value does not fit the size of uintptr, it has to be
895 // passed by reference, regardless the result in the table above.
896 // - For pointers mapped by value that have either an implicit map or an
897 // array section, the runtime library may pass the NULL value to the
898 // device instead of the value passed to it by the compiler.
899
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000900
901 if (Ty->isReferenceType())
902 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000903
904 // Locate map clauses and see if the variable being captured is referred to
905 // in any of those clauses. Here we only care about variables, not fields,
906 // because fields are part of aggregates.
907 bool IsVariableUsedInMapClause = false;
908 bool IsVariableAssociatedWithSection = false;
909
910 DSAStack->checkMappableExprComponentListsForDecl(
911 D, /*CurrentRegionOnly=*/true,
912 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
913 MapExprComponents) {
914
915 auto EI = MapExprComponents.rbegin();
916 auto EE = MapExprComponents.rend();
917
918 assert(EI != EE && "Invalid map expression!");
919
920 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
921 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
922
923 ++EI;
924 if (EI == EE)
925 return false;
926
927 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
928 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
929 isa<MemberExpr>(EI->getAssociatedExpression())) {
930 IsVariableAssociatedWithSection = true;
931 // There is nothing more we need to know about this variable.
932 return true;
933 }
934
935 // Keep looking for more map info.
936 return false;
937 });
938
939 if (IsVariableUsedInMapClause) {
940 // If variable is identified in a map clause it is always captured by
941 // reference except if it is a pointer that is dereferenced somehow.
942 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
943 } else {
944 // By default, all the data that has a scalar type is mapped by copy.
945 IsByRef = !Ty->isScalarType();
946 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000947 }
948
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000949 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
950 IsByRef = !DSAStack->hasExplicitDSA(
951 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
952 Level, /*NotLastprivate=*/true);
953 }
954
Samuel Antao86ace552016-04-27 22:40:57 +0000955 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000956 // and alignment, because the runtime library only deals with uintptr types.
957 // If it does not fit the uintptr size, we need to pass the data by reference
958 // instead.
959 if (!IsByRef &&
960 (Ctx.getTypeSizeInChars(Ty) >
961 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000962 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000963 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000964 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000965
966 return IsByRef;
967}
968
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000969unsigned Sema::getOpenMPNestingLevel() const {
970 assert(getLangOpts().OpenMP);
971 return DSAStack->getNestingLevel();
972}
973
Alexey Bataev90c228f2016-02-08 09:29:13 +0000974VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000975 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000976 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000977
978 // If we are attempting to capture a global variable in a directive with
979 // 'target' we return true so that this global is also mapped to the device.
980 //
981 // FIXME: If the declaration is enclosed in a 'declare target' directive,
982 // then it should not be captured. Therefore, an extra check has to be
983 // inserted here once support for 'declare target' is added.
984 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000985 auto *VD = dyn_cast<VarDecl>(D);
986 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000987 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000988 !DSAStack->isClauseParsingMode())
989 return VD;
Samuel Antaof0d79752016-05-27 15:21:27 +0000990 if (DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000991 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
992 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000993 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000994 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000995 false))
996 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000997 }
998
Alexey Bataev48977c32015-08-04 08:10:48 +0000999 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1000 (!DSAStack->isClauseParsingMode() ||
1001 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001002 auto &&Info = DSAStack->isLoopControlVariable(D);
1003 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001004 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001005 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001006 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001007 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001008 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001009 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001010 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001011 DVarPrivate = DSAStack->hasDSA(
1012 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1013 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001014 if (DVarPrivate.CKind != OMPC_unknown)
1015 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001016 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001017 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001018}
1019
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001020bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001021 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1022 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001023 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001024}
1025
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001026bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001027 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1028 // Return true if the current level is no longer enclosed in a target region.
1029
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001030 auto *VD = dyn_cast<VarDecl>(D);
1031 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001032 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1033 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001034}
1035
Alexey Bataeved09d242014-05-28 05:53:51 +00001036void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001037
1038void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1039 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001040 Scope *CurScope, SourceLocation Loc) {
1041 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001042 PushExpressionEvaluationContext(PotentiallyEvaluated);
1043}
1044
Alexey Bataevaac108a2015-06-23 04:51:00 +00001045void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1046 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001047}
1048
Alexey Bataevaac108a2015-06-23 04:51:00 +00001049void Sema::EndOpenMPClause() {
1050 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001051}
1052
Alexey Bataev758e55e2013-09-06 18:03:48 +00001053void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001054 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1055 // A variable of class type (or array thereof) that appears in a lastprivate
1056 // clause requires an accessible, unambiguous default constructor for the
1057 // class type, unless the list item is also specified in a firstprivate
1058 // clause.
1059 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001060 for (auto *C : D->clauses()) {
1061 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1062 SmallVector<Expr *, 8> PrivateCopies;
1063 for (auto *DE : Clause->varlists()) {
1064 if (DE->isValueDependent() || DE->isTypeDependent()) {
1065 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001066 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001067 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001068 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001069 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1070 QualType Type = VD->getType().getNonReferenceType();
1071 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001072 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001073 // Generate helper private variable and initialize it with the
1074 // default value. The address of the original variable is replaced
1075 // by the address of the new private variable in CodeGen. This new
1076 // variable is not added to IdResolver, so the code in the OpenMP
1077 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001078 auto *VDPrivate = buildVarDecl(
1079 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001080 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001081 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1082 if (VDPrivate->isInvalidDecl())
1083 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001084 PrivateCopies.push_back(buildDeclRefExpr(
1085 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001086 } else {
1087 // The variable is also a firstprivate, so initialization sequence
1088 // for private copy is generated already.
1089 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001090 }
1091 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001092 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001093 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001094 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001095 }
1096 }
1097 }
1098
Alexey Bataev758e55e2013-09-06 18:03:48 +00001099 DSAStack->pop();
1100 DiscardCleanupsInEvaluationContext();
1101 PopExpressionEvaluationContext();
1102}
1103
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001104static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1105 Expr *NumIterations, Sema &SemaRef,
1106 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001107
Alexey Bataeva769e072013-03-22 06:34:35 +00001108namespace {
1109
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001110class VarDeclFilterCCC : public CorrectionCandidateCallback {
1111private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001112 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001113
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001114public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001115 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001116 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001117 NamedDecl *ND = Candidate.getCorrectionDecl();
1118 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1119 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001120 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1121 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001122 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001123 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001124 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001125};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001126
1127class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1128private:
1129 Sema &SemaRef;
1130
1131public:
1132 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1133 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1134 NamedDecl *ND = Candidate.getCorrectionDecl();
1135 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1136 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1137 SemaRef.getCurScope());
1138 }
1139 return false;
1140 }
1141};
1142
Alexey Bataeved09d242014-05-28 05:53:51 +00001143} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001144
1145ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1146 CXXScopeSpec &ScopeSpec,
1147 const DeclarationNameInfo &Id) {
1148 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1149 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1150
1151 if (Lookup.isAmbiguous())
1152 return ExprError();
1153
1154 VarDecl *VD;
1155 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001156 if (TypoCorrection Corrected = CorrectTypo(
1157 Id, LookupOrdinaryName, CurScope, nullptr,
1158 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001159 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001160 PDiag(Lookup.empty()
1161 ? diag::err_undeclared_var_use_suggest
1162 : diag::err_omp_expected_var_arg_suggest)
1163 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001164 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001165 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001166 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1167 : diag::err_omp_expected_var_arg)
1168 << Id.getName();
1169 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001170 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 } else {
1172 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001173 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001174 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1175 return ExprError();
1176 }
1177 }
1178 Lookup.suppressDiagnostics();
1179
1180 // OpenMP [2.9.2, Syntax, C/C++]
1181 // Variables must be file-scope, namespace-scope, or static block-scope.
1182 if (!VD->hasGlobalStorage()) {
1183 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001184 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1185 bool IsDecl =
1186 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001187 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001188 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1189 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001190 return ExprError();
1191 }
1192
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001193 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1194 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001195 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1196 // A threadprivate directive for file-scope variables must appear outside
1197 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001198 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1199 !getCurLexicalContext()->isTranslationUnit()) {
1200 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001201 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1202 bool IsDecl =
1203 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1204 Diag(VD->getLocation(),
1205 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1206 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001207 return ExprError();
1208 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001209 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1210 // A threadprivate directive for static class member variables must appear
1211 // in the class definition, in the same scope in which the member
1212 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001213 if (CanonicalVD->isStaticDataMember() &&
1214 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1215 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001216 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1217 bool IsDecl =
1218 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1219 Diag(VD->getLocation(),
1220 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1221 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001222 return ExprError();
1223 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001224 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1225 // A threadprivate directive for namespace-scope variables must appear
1226 // outside any definition or declaration other than the namespace
1227 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001228 if (CanonicalVD->getDeclContext()->isNamespace() &&
1229 (!getCurLexicalContext()->isFileContext() ||
1230 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1231 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001232 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1233 bool IsDecl =
1234 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1235 Diag(VD->getLocation(),
1236 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1237 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001238 return ExprError();
1239 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001240 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1241 // A threadprivate directive for static block-scope variables must appear
1242 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001243 if (CanonicalVD->isStaticLocal() && CurScope &&
1244 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001245 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001246 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1247 bool IsDecl =
1248 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1249 Diag(VD->getLocation(),
1250 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1251 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001252 return ExprError();
1253 }
1254
1255 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1256 // A threadprivate directive must lexically precede all references to any
1257 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001258 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001259 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001260 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001261 return ExprError();
1262 }
1263
1264 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001265 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1266 SourceLocation(), VD,
1267 /*RefersToEnclosingVariableOrCapture=*/false,
1268 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001269}
1270
Alexey Bataeved09d242014-05-28 05:53:51 +00001271Sema::DeclGroupPtrTy
1272Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1273 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001274 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001275 CurContext->addDecl(D);
1276 return DeclGroupPtrTy::make(DeclGroupRef(D));
1277 }
David Blaikie0403cb12016-01-15 23:43:25 +00001278 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001279}
1280
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001281namespace {
1282class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1283 Sema &SemaRef;
1284
1285public:
1286 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1287 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1288 if (VD->hasLocalStorage()) {
1289 SemaRef.Diag(E->getLocStart(),
1290 diag::err_omp_local_var_in_threadprivate_init)
1291 << E->getSourceRange();
1292 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1293 << VD << VD->getSourceRange();
1294 return true;
1295 }
1296 }
1297 return false;
1298 }
1299 bool VisitStmt(const Stmt *S) {
1300 for (auto Child : S->children()) {
1301 if (Child && Visit(Child))
1302 return true;
1303 }
1304 return false;
1305 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001306 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001307};
1308} // namespace
1309
Alexey Bataeved09d242014-05-28 05:53:51 +00001310OMPThreadPrivateDecl *
1311Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001312 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001313 for (auto &RefExpr : VarList) {
1314 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001315 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1316 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001317
Alexey Bataev376b4a42016-02-09 09:41:09 +00001318 // Mark variable as used.
1319 VD->setReferenced();
1320 VD->markUsed(Context);
1321
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001322 QualType QType = VD->getType();
1323 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1324 // It will be analyzed later.
1325 Vars.push_back(DE);
1326 continue;
1327 }
1328
Alexey Bataeva769e072013-03-22 06:34:35 +00001329 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1330 // A threadprivate variable must not have an incomplete type.
1331 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001332 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001333 continue;
1334 }
1335
1336 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1337 // A threadprivate variable must not have a reference type.
1338 if (VD->getType()->isReferenceType()) {
1339 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001340 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1341 bool IsDecl =
1342 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1343 Diag(VD->getLocation(),
1344 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1345 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001346 continue;
1347 }
1348
Samuel Antaof8b50122015-07-13 22:54:53 +00001349 // Check if this is a TLS variable. If TLS is not being supported, produce
1350 // the corresponding diagnostic.
1351 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1352 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1353 getLangOpts().OpenMPUseTLS &&
1354 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001355 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1356 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001357 Diag(ILoc, diag::err_omp_var_thread_local)
1358 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001359 bool IsDecl =
1360 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1361 Diag(VD->getLocation(),
1362 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1363 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001364 continue;
1365 }
1366
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001367 // Check if initial value of threadprivate variable reference variable with
1368 // local storage (it is not supported by runtime).
1369 if (auto Init = VD->getAnyInitializer()) {
1370 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001371 if (Checker.Visit(Init))
1372 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001373 }
1374
Alexey Bataeved09d242014-05-28 05:53:51 +00001375 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001376 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001377 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1378 Context, SourceRange(Loc, Loc)));
1379 if (auto *ML = Context.getASTMutationListener())
1380 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001381 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001382 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001383 if (!Vars.empty()) {
1384 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1385 Vars);
1386 D->setAccess(AS_public);
1387 }
1388 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001389}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001390
Alexey Bataev7ff55242014-06-19 09:13:45 +00001391static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001392 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001393 bool IsLoopIterVar = false) {
1394 if (DVar.RefExpr) {
1395 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1396 << getOpenMPClauseName(DVar.CKind);
1397 return;
1398 }
1399 enum {
1400 PDSA_StaticMemberShared,
1401 PDSA_StaticLocalVarShared,
1402 PDSA_LoopIterVarPrivate,
1403 PDSA_LoopIterVarLinear,
1404 PDSA_LoopIterVarLastprivate,
1405 PDSA_ConstVarShared,
1406 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001407 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001408 PDSA_LocalVarPrivate,
1409 PDSA_Implicit
1410 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001411 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001412 auto ReportLoc = D->getLocation();
1413 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001414 if (IsLoopIterVar) {
1415 if (DVar.CKind == OMPC_private)
1416 Reason = PDSA_LoopIterVarPrivate;
1417 else if (DVar.CKind == OMPC_lastprivate)
1418 Reason = PDSA_LoopIterVarLastprivate;
1419 else
1420 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001421 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1422 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001423 Reason = PDSA_TaskVarFirstprivate;
1424 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001425 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001426 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001427 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001428 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001429 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001430 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001431 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001432 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001433 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001434 ReportHint = true;
1435 Reason = PDSA_LocalVarPrivate;
1436 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001437 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001438 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001439 << Reason << ReportHint
1440 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1441 } else if (DVar.ImplicitDSALoc.isValid()) {
1442 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1443 << getOpenMPClauseName(DVar.CKind);
1444 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001445}
1446
Alexey Bataev758e55e2013-09-06 18:03:48 +00001447namespace {
1448class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1449 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001450 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001451 bool ErrorFound;
1452 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001453 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001454 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001455
Alexey Bataev758e55e2013-09-06 18:03:48 +00001456public:
1457 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001458 if (E->isTypeDependent() || E->isValueDependent() ||
1459 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1460 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001461 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001462 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001463 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1464 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001465
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001466 auto DVar = Stack->getTopDSA(VD, false);
1467 // Check if the variable has explicit DSA set and stop analysis if it so.
1468 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001470 auto ELoc = E->getExprLoc();
1471 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001472 // The default(none) clause requires that each variable that is referenced
1473 // in the construct, and does not have a predetermined data-sharing
1474 // attribute, must have its data-sharing attribute explicitly determined
1475 // by being listed in a data-sharing attribute clause.
1476 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001477 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001478 VarsWithInheritedDSA.count(VD) == 0) {
1479 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001480 return;
1481 }
1482
1483 // OpenMP [2.9.3.6, Restrictions, p.2]
1484 // A list item that appears in a reduction clause of the innermost
1485 // enclosing worksharing or parallel construct may not be accessed in an
1486 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001487 DVar = Stack->hasInnermostDSA(
1488 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1489 [](OpenMPDirectiveKind K) -> bool {
1490 return isOpenMPParallelDirective(K) ||
1491 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1492 },
1493 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001494 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001495 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001496 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1497 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001498 return;
1499 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001500
1501 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001502 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001503 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1504 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001505 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001506 }
1507 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001508 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001509 if (E->isTypeDependent() || E->isValueDependent() ||
1510 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1511 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001512 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1513 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1514 auto DVar = Stack->getTopDSA(FD, false);
1515 // Check if the variable has explicit DSA set and stop analysis if it
1516 // so.
1517 if (DVar.RefExpr)
1518 return;
1519
1520 auto ELoc = E->getExprLoc();
1521 auto DKind = Stack->getCurrentDirective();
1522 // OpenMP [2.9.3.6, Restrictions, p.2]
1523 // A list item that appears in a reduction clause of the innermost
1524 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001525 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001526 DVar = Stack->hasInnermostDSA(
1527 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1528 [](OpenMPDirectiveKind K) -> bool {
1529 return isOpenMPParallelDirective(K) ||
1530 isOpenMPWorksharingDirective(K) ||
1531 isOpenMPTeamsDirective(K);
1532 },
1533 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001534 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001535 ErrorFound = true;
1536 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1537 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1538 return;
1539 }
1540
1541 // Define implicit data-sharing attributes for task.
1542 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001543 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1544 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001545 ImplicitFirstprivate.push_back(E);
1546 }
1547 }
1548 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001549 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001550 for (auto *C : S->clauses()) {
1551 // Skip analysis of arguments of implicitly defined firstprivate clause
1552 // for task directives.
1553 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1554 for (auto *CC : C->children()) {
1555 if (CC)
1556 Visit(CC);
1557 }
1558 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001559 }
1560 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001561 for (auto *C : S->children()) {
1562 if (C && !isa<OMPExecutableDirective>(C))
1563 Visit(C);
1564 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001565 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001566
1567 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001568 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001569 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001570 return VarsWithInheritedDSA;
1571 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001572
Alexey Bataev7ff55242014-06-19 09:13:45 +00001573 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1574 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001575};
Alexey Bataeved09d242014-05-28 05:53:51 +00001576} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001577
Alexey Bataevbae9a792014-06-27 10:37:06 +00001578void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001579 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00001580 case OMPD_parallel:
1581 case OMPD_parallel_for:
1582 case OMPD_parallel_for_simd:
1583 case OMPD_parallel_sections:
1584 case OMPD_teams: {
Alexey Bataev9959db52014-05-06 10:08:46 +00001585 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001586 QualType KmpInt32PtrTy =
1587 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001588 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001589 std::make_pair(".global_tid.", KmpInt32PtrTy),
1590 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1591 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001592 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001593 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1594 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001595 break;
1596 }
Kelvin Li70a12c52016-07-13 21:51:49 +00001597 case OMPD_simd:
1598 case OMPD_for:
1599 case OMPD_for_simd:
1600 case OMPD_sections:
1601 case OMPD_section:
1602 case OMPD_single:
1603 case OMPD_master:
1604 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00001605 case OMPD_taskgroup:
1606 case OMPD_distribute:
Kelvin Li70a12c52016-07-13 21:51:49 +00001607 case OMPD_ordered:
1608 case OMPD_atomic:
1609 case OMPD_target_data:
1610 case OMPD_target:
1611 case OMPD_target_parallel:
1612 case OMPD_target_parallel_for:
Kelvin Lia579b912016-07-14 02:54:56 +00001613 case OMPD_target_parallel_for_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001614 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001615 std::make_pair(StringRef(), QualType()) // __context with shared vars
1616 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001617 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1618 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001619 break;
1620 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001621 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001622 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001623 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1624 FunctionProtoType::ExtProtoInfo EPI;
1625 EPI.Variadic = true;
1626 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001627 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001628 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001629 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1630 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1631 std::make_pair(".copy_fn.",
1632 Context.getPointerType(CopyFnType).withConst()),
1633 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001634 std::make_pair(StringRef(), QualType()) // __context with shared vars
1635 };
1636 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1637 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001638 // Mark this captured region as inlined, because we don't use outlined
1639 // function directly.
1640 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1641 AlwaysInlineAttr::CreateImplicit(
1642 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001643 break;
1644 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001645 case OMPD_taskloop:
1646 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001647 QualType KmpInt32Ty =
1648 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1649 QualType KmpUInt64Ty =
1650 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1651 QualType KmpInt64Ty =
1652 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1653 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1654 FunctionProtoType::ExtProtoInfo EPI;
1655 EPI.Variadic = true;
1656 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001657 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001658 std::make_pair(".global_tid.", KmpInt32Ty),
1659 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1660 std::make_pair(".privates.",
1661 Context.VoidPtrTy.withConst().withRestrict()),
1662 std::make_pair(
1663 ".copy_fn.",
1664 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1665 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1666 std::make_pair(".lb.", KmpUInt64Ty),
1667 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1668 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001669 std::make_pair(StringRef(), QualType()) // __context with shared vars
1670 };
1671 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1672 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001673 // Mark this captured region as inlined, because we don't use outlined
1674 // function directly.
1675 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1676 AlwaysInlineAttr::CreateImplicit(
1677 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001678 break;
1679 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001680 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001681 case OMPD_distribute_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001682 case OMPD_distribute_parallel_for: {
1683 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1684 QualType KmpInt32PtrTy =
1685 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1686 Sema::CapturedParamNameType Params[] = {
1687 std::make_pair(".global_tid.", KmpInt32PtrTy),
1688 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1689 std::make_pair(".previous.lb.", Context.getSizeType()),
1690 std::make_pair(".previous.ub.", Context.getSizeType()),
1691 std::make_pair(StringRef(), QualType()) // __context with shared vars
1692 };
1693 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1694 Params);
1695 break;
1696 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001697 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001698 case OMPD_taskyield:
1699 case OMPD_barrier:
1700 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001701 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001702 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001703 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001704 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001705 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001706 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001707 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001708 case OMPD_declare_target:
1709 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001710 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001711 llvm_unreachable("OpenMP Directive is not allowed");
1712 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001713 llvm_unreachable("Unknown OpenMP directive");
1714 }
1715}
1716
Alexey Bataev3392d762016-02-16 11:18:12 +00001717static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001718 Expr *CaptureExpr, bool WithInit,
1719 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001720 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001721 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001722 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001723 QualType Ty = Init->getType();
1724 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1725 if (S.getLangOpts().CPlusPlus)
1726 Ty = C.getLValueReferenceType(Ty);
1727 else {
1728 Ty = C.getPointerType(Ty);
1729 ExprResult Res =
1730 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1731 if (!Res.isUsable())
1732 return nullptr;
1733 Init = Res.get();
1734 }
Alexey Bataev61205072016-03-02 04:57:40 +00001735 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001736 }
1737 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001738 if (!WithInit)
1739 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001740 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001741 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1742 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001743 return CED;
1744}
1745
Alexey Bataev61205072016-03-02 04:57:40 +00001746static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1747 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001748 OMPCapturedExprDecl *CD;
1749 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1750 CD = cast<OMPCapturedExprDecl>(VD);
1751 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001752 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1753 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001754 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001755 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001756}
1757
Alexey Bataev5a3af132016-03-29 08:58:54 +00001758static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1759 if (!Ref) {
1760 auto *CD =
1761 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1762 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1763 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1764 CaptureExpr->getExprLoc());
1765 }
1766 ExprResult Res = Ref;
1767 if (!S.getLangOpts().CPlusPlus &&
1768 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1769 Ref->getType()->isPointerType())
1770 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1771 if (!Res.isUsable())
1772 return ExprError();
1773 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001774}
1775
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001776StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1777 ArrayRef<OMPClause *> Clauses) {
1778 if (!S.isUsable()) {
1779 ActOnCapturedRegionError();
1780 return StmtError();
1781 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001782
1783 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001784 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001785 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001786 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001787 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001788 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001789 Clause->getClauseKind() == OMPC_copyprivate ||
1790 (getLangOpts().OpenMPUseTLS &&
1791 getASTContext().getTargetInfo().isTLSSupported() &&
1792 Clause->getClauseKind() == OMPC_copyin)) {
1793 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001794 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001795 for (auto *VarRef : Clause->children()) {
1796 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001797 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001798 }
1799 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001800 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001801 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001802 // Mark all variables in private list clauses as used in inner region.
1803 // Required for proper codegen of combined directives.
1804 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001805 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001806 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1807 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001808 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1809 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001810 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001811 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1812 if (auto *E = C->getPostUpdateExpr())
1813 MarkDeclarationsReferencedInExpr(E);
1814 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001815 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001816 if (Clause->getClauseKind() == OMPC_schedule)
1817 SC = cast<OMPScheduleClause>(Clause);
1818 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001819 OC = cast<OMPOrderedClause>(Clause);
1820 else if (Clause->getClauseKind() == OMPC_linear)
1821 LCs.push_back(cast<OMPLinearClause>(Clause));
1822 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001823 bool ErrorFound = false;
1824 // OpenMP, 2.7.1 Loop Construct, Restrictions
1825 // The nonmonotonic modifier cannot be specified if an ordered clause is
1826 // specified.
1827 if (SC &&
1828 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1829 SC->getSecondScheduleModifier() ==
1830 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1831 OC) {
1832 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1833 ? SC->getFirstScheduleModifierLoc()
1834 : SC->getSecondScheduleModifierLoc(),
1835 diag::err_omp_schedule_nonmonotonic_ordered)
1836 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1837 ErrorFound = true;
1838 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001839 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1840 for (auto *C : LCs) {
1841 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1842 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1843 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001844 ErrorFound = true;
1845 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001846 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1847 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1848 OC->getNumForLoops()) {
1849 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1850 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1851 ErrorFound = true;
1852 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001853 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001854 ActOnCapturedRegionError();
1855 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001856 }
1857 return ActOnCapturedRegionEnd(S.get());
1858}
1859
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001860static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1861 OpenMPDirectiveKind CurrentRegion,
1862 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001863 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001864 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001865 // Allowed nesting of constructs
1866 // +------------------+-----------------+------------------------------------+
1867 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1868 // +------------------+-----------------+------------------------------------+
1869 // | parallel | parallel | * |
1870 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001871 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001872 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001873 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001874 // | parallel | simd | * |
1875 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001876 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001877 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001878 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001879 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001880 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001881 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001882 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001883 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001884 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001885 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001886 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001887 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001888 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001889 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001890 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001891 // | parallel | target parallel | * |
1892 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001893 // | parallel | target enter | * |
1894 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001895 // | parallel | target exit | * |
1896 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001897 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001898 // | parallel | cancellation | |
1899 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001900 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001901 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001902 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001903 // | parallel | distribute | + |
1904 // | parallel | distribute | + |
1905 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001906 // | parallel | distribute | + |
1907 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001908 // | parallel | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001909 // +------------------+-----------------+------------------------------------+
1910 // | for | parallel | * |
1911 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001912 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001913 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001914 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001915 // | for | simd | * |
1916 // | for | sections | + |
1917 // | for | section | + |
1918 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001919 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001920 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001921 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001922 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001923 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001924 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001925 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001926 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001927 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001928 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001929 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001930 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001931 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001932 // | for | target parallel | * |
1933 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001934 // | for | target enter | * |
1935 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001936 // | for | target exit | * |
1937 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001938 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001939 // | for | cancellation | |
1940 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001941 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001942 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001943 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001944 // | for | distribute | + |
1945 // | for | distribute | + |
1946 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001947 // | for | distribute | + |
1948 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001949 // | for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001950 // | for | target parallel | + |
1951 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001952 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001953 // | master | parallel | * |
1954 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001955 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001956 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001957 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001958 // | master | simd | * |
1959 // | master | sections | + |
1960 // | master | section | + |
1961 // | master | single | + |
1962 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001963 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001964 // | master |parallel sections| * |
1965 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001966 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001967 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001968 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001969 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001970 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001971 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001972 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001973 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001974 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001975 // | master | target parallel | * |
1976 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001977 // | master | target enter | * |
1978 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001979 // | master | target exit | * |
1980 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001981 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001982 // | master | cancellation | |
1983 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001984 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001985 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001986 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001987 // | master | distribute | + |
1988 // | master | distribute | + |
1989 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001990 // | master | distribute | + |
1991 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001992 // | master | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001993 // | master | target parallel | + |
1994 // | | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001995 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001996 // | critical | parallel | * |
1997 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001998 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001999 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002000 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002001 // | critical | simd | * |
2002 // | critical | sections | + |
2003 // | critical | section | + |
2004 // | critical | single | + |
2005 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002006 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002007 // | critical |parallel sections| * |
2008 // | critical | task | * |
2009 // | critical | taskyield | * |
2010 // | critical | barrier | + |
2011 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002012 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002013 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002014 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002015 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002016 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002017 // | critical | target parallel | * |
2018 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002019 // | critical | target enter | * |
2020 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002021 // | critical | target exit | * |
2022 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002023 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002024 // | critical | cancellation | |
2025 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002026 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002027 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002028 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002029 // | critical | distribute | + |
2030 // | critical | distribute | + |
2031 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002032 // | critical | distribute | + |
2033 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002034 // | critical | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002035 // | critical | target parallel | + |
2036 // | | for simd | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002037 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002038 // | simd | parallel | |
2039 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002040 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002041 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002042 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002043 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002044 // | simd | sections | |
2045 // | simd | section | |
2046 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002047 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002048 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002049 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002050 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002051 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002052 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002053 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002054 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002055 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002056 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002057 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002058 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002059 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002060 // | simd | target parallel | |
2061 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002062 // | simd | target enter | |
2063 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002064 // | simd | target exit | |
2065 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002066 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002067 // | simd | cancellation | |
2068 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002069 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002070 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002071 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002072 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002073 // | simd | distribute | |
2074 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002075 // | simd | distribute | |
2076 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002077 // | simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002078 // | simd | target parallel | |
2079 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002080 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002081 // | for simd | parallel | |
2082 // | for simd | for | |
2083 // | for simd | for simd | |
2084 // | for simd | master | |
2085 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002086 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002087 // | for simd | sections | |
2088 // | for simd | section | |
2089 // | for simd | single | |
2090 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002091 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002092 // | for simd |parallel sections| |
2093 // | for simd | task | |
2094 // | for simd | taskyield | |
2095 // | for simd | barrier | |
2096 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002097 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002098 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002099 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002100 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002101 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002102 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002103 // | for simd | target parallel | |
2104 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002105 // | for simd | target enter | |
2106 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002107 // | for simd | target exit | |
2108 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002109 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002110 // | for simd | cancellation | |
2111 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002112 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002113 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002114 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002115 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002116 // | for simd | distribute | |
2117 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002118 // | for simd | distribute | |
2119 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002120 // | for simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002121 // | for simd | target parallel | |
2122 // | | for simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002123 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002124 // | parallel for simd| parallel | |
2125 // | parallel for simd| for | |
2126 // | parallel for simd| for simd | |
2127 // | parallel for simd| master | |
2128 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002129 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002130 // | parallel for simd| sections | |
2131 // | parallel for simd| section | |
2132 // | parallel for simd| single | |
2133 // | parallel for simd| parallel for | |
2134 // | parallel for simd|parallel for simd| |
2135 // | parallel for simd|parallel sections| |
2136 // | parallel for simd| task | |
2137 // | parallel for simd| taskyield | |
2138 // | parallel for simd| barrier | |
2139 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002140 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002141 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002142 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002143 // | parallel for simd| atomic | |
2144 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002145 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002146 // | parallel for simd| target parallel | |
2147 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002148 // | parallel for simd| target enter | |
2149 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002150 // | parallel for simd| target exit | |
2151 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002152 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002153 // | parallel for simd| cancellation | |
2154 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002155 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002156 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002157 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002158 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002159 // | parallel for simd| distribute | |
2160 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002161 // | parallel for simd| distribute | |
2162 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002163 // | parallel for simd| distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002164 // | | for simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002165 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002166 // | sections | parallel | * |
2167 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002168 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002169 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002170 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002171 // | sections | simd | * |
2172 // | sections | sections | + |
2173 // | sections | section | * |
2174 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002175 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002176 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002177 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002178 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002179 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002180 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002181 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002182 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002183 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002184 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002185 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002186 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002187 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002188 // | sections | target parallel | * |
2189 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002190 // | sections | target enter | * |
2191 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002192 // | sections | target exit | * |
2193 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002194 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002195 // | sections | cancellation | |
2196 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002197 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002198 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002199 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002200 // | sections | distribute | + |
2201 // | sections | distribute | + |
2202 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002203 // | sections | distribute | + |
2204 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002205 // | sections | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002206 // | sections | target parallel | + |
2207 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002208 // +------------------+-----------------+------------------------------------+
2209 // | section | parallel | * |
2210 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002211 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002212 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002213 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002214 // | section | simd | * |
2215 // | section | sections | + |
2216 // | section | section | + |
2217 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002218 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002219 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002220 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002221 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002222 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002223 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002224 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002225 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002226 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002227 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002228 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002229 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002230 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002231 // | section | target parallel | * |
2232 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002233 // | section | target enter | * |
2234 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002235 // | section | target exit | * |
2236 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002237 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002238 // | section | cancellation | |
2239 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002240 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002241 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002242 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002243 // | section | distribute | + |
2244 // | section | distribute | + |
2245 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002246 // | section | distribute | + |
2247 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002248 // | section | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002249 // | section | target parallel | + |
2250 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002251 // +------------------+-----------------+------------------------------------+
2252 // | single | parallel | * |
2253 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002254 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002255 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002256 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002257 // | single | simd | * |
2258 // | single | sections | + |
2259 // | single | section | + |
2260 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002261 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002262 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002263 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002264 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002265 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002266 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002267 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002268 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002269 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002270 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002271 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002272 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002273 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002274 // | single | target parallel | * |
2275 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002276 // | single | target enter | * |
2277 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002278 // | single | target exit | * |
2279 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002280 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002281 // | single | cancellation | |
2282 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002283 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002284 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002285 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002286 // | single | distribute | + |
2287 // | single | distribute | + |
2288 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002289 // | single | distribute | + |
2290 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002291 // | single | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002292 // | single | target parallel | + |
2293 // | | for simd | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002294 // +------------------+-----------------+------------------------------------+
2295 // | parallel for | parallel | * |
2296 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002297 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002298 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002299 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002300 // | parallel for | simd | * |
2301 // | parallel for | sections | + |
2302 // | parallel for | section | + |
2303 // | parallel for | single | + |
2304 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002305 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002306 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002307 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002308 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002309 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002310 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002311 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002312 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002313 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002314 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002315 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002316 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002317 // | parallel for | target parallel | * |
2318 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002319 // | parallel for | target enter | * |
2320 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002321 // | parallel for | target exit | * |
2322 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002323 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002324 // | parallel for | cancellation | |
2325 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002326 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002327 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002328 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002329 // | parallel for | distribute | + |
2330 // | parallel for | distribute | + |
2331 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002332 // | parallel for | distribute | + |
2333 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002334 // | parallel for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002335 // | parallel for | target parallel | + |
2336 // | | for simd | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002337 // +------------------+-----------------+------------------------------------+
2338 // | parallel sections| parallel | * |
2339 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002340 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002341 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002342 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002343 // | parallel sections| simd | * |
2344 // | parallel sections| sections | + |
2345 // | parallel sections| section | * |
2346 // | parallel sections| single | + |
2347 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002348 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002349 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002350 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002351 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002352 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002353 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002354 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002355 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002356 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002357 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002358 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002359 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002360 // | parallel sections| target parallel | * |
2361 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002362 // | parallel sections| target enter | * |
2363 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002364 // | parallel sections| target exit | * |
2365 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002366 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002367 // | parallel sections| cancellation | |
2368 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002369 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002370 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002371 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002372 // | parallel sections| distribute | + |
2373 // | parallel sections| distribute | + |
2374 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002375 // | parallel sections| distribute | + |
2376 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002377 // | parallel sections| distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002378 // | parallel sections| target parallel | + |
2379 // | | for simd | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002380 // +------------------+-----------------+------------------------------------+
2381 // | task | parallel | * |
2382 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002383 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002384 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002385 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002386 // | task | simd | * |
2387 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002388 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002389 // | task | single | + |
2390 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002391 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002392 // | task |parallel sections| * |
2393 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002394 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002395 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002396 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002397 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002398 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002399 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002400 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002401 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002402 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002403 // | task | target parallel | * |
2404 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002405 // | task | target enter | * |
2406 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002407 // | task | target exit | * |
2408 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002409 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002410 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002411 // | | point | ! |
2412 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002413 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002414 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002415 // | task | distribute | + |
2416 // | task | distribute | + |
2417 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002418 // | task | distribute | + |
2419 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002420 // | task | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002421 // | task | target parallel | + |
2422 // | | for simd | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002423 // +------------------+-----------------+------------------------------------+
2424 // | ordered | parallel | * |
2425 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002426 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002427 // | ordered | master | * |
2428 // | ordered | critical | * |
2429 // | ordered | simd | * |
2430 // | ordered | sections | + |
2431 // | ordered | section | + |
2432 // | ordered | single | + |
2433 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002434 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002435 // | ordered |parallel sections| * |
2436 // | ordered | task | * |
2437 // | ordered | taskyield | * |
2438 // | ordered | barrier | + |
2439 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002440 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002441 // | ordered | flush | * |
2442 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002443 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002444 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002445 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002446 // | ordered | target parallel | * |
2447 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002448 // | ordered | target enter | * |
2449 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002450 // | ordered | target exit | * |
2451 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002452 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002453 // | ordered | cancellation | |
2454 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002455 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002456 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002457 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002458 // | ordered | distribute | + |
2459 // | ordered | distribute | + |
2460 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002461 // | ordered | distribute | + |
2462 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002463 // | ordered | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002464 // | ordered | target parallel | + |
2465 // | | for simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002466 // +------------------+-----------------+------------------------------------+
2467 // | atomic | parallel | |
2468 // | atomic | for | |
2469 // | atomic | for simd | |
2470 // | atomic | master | |
2471 // | atomic | critical | |
2472 // | atomic | simd | |
2473 // | atomic | sections | |
2474 // | atomic | section | |
2475 // | atomic | single | |
2476 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002477 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002478 // | atomic |parallel sections| |
2479 // | atomic | task | |
2480 // | atomic | taskyield | |
2481 // | atomic | barrier | |
2482 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002483 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002484 // | atomic | flush | |
2485 // | atomic | ordered | |
2486 // | atomic | atomic | |
2487 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002488 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002489 // | atomic | target parallel | |
2490 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002491 // | atomic | target enter | |
2492 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002493 // | atomic | target exit | |
2494 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002495 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002496 // | atomic | cancellation | |
2497 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002498 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002499 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002500 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002501 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002502 // | atomic | distribute | |
2503 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002504 // | atomic | distribute | |
2505 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002506 // | atomic | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002507 // | atomic | target parallel | |
2508 // | | for simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002509 // +------------------+-----------------+------------------------------------+
2510 // | target | parallel | * |
2511 // | target | for | * |
2512 // | target | for simd | * |
2513 // | target | master | * |
2514 // | target | critical | * |
2515 // | target | simd | * |
2516 // | target | sections | * |
2517 // | target | section | * |
2518 // | target | single | * |
2519 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002520 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002521 // | target |parallel sections| * |
2522 // | target | task | * |
2523 // | target | taskyield | * |
2524 // | target | barrier | * |
2525 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002526 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002527 // | target | flush | * |
2528 // | target | ordered | * |
2529 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002530 // | target | target | |
2531 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002532 // | target | target parallel | |
2533 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002534 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002535 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002536 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002537 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002538 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002539 // | target | cancellation | |
2540 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002541 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002542 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002543 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002544 // | target | distribute | + |
2545 // | target | distribute | + |
2546 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002547 // | target | distribute | + |
2548 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002549 // | target | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002550 // | target | target parallel | |
2551 // | | for simd | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002552 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002553 // | target parallel | parallel | * |
2554 // | target parallel | for | * |
2555 // | target parallel | for simd | * |
2556 // | target parallel | master | * |
2557 // | target parallel | critical | * |
2558 // | target parallel | simd | * |
2559 // | target parallel | sections | * |
2560 // | target parallel | section | * |
2561 // | target parallel | single | * |
2562 // | target parallel | parallel for | * |
2563 // | target parallel |parallel for simd| * |
2564 // | target parallel |parallel sections| * |
2565 // | target parallel | task | * |
2566 // | target parallel | taskyield | * |
2567 // | target parallel | barrier | * |
2568 // | target parallel | taskwait | * |
2569 // | target parallel | taskgroup | * |
2570 // | target parallel | flush | * |
2571 // | target parallel | ordered | * |
2572 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002573 // | target parallel | target | |
2574 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002575 // | target parallel | target parallel | |
2576 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002577 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002578 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002579 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002580 // | | data | |
2581 // | target parallel | teams | |
2582 // | target parallel | cancellation | |
2583 // | | point | ! |
2584 // | target parallel | cancel | ! |
2585 // | target parallel | taskloop | * |
2586 // | target parallel | taskloop simd | * |
2587 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002588 // | target parallel | distribute | |
2589 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002590 // | target parallel | distribute | |
2591 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002592 // | target parallel | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002593 // | target parallel | target parallel | |
2594 // | | for simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002595 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002596 // | target parallel | parallel | * |
2597 // | for | | |
2598 // | target parallel | for | * |
2599 // | for | | |
2600 // | target parallel | for simd | * |
2601 // | for | | |
2602 // | target parallel | master | * |
2603 // | for | | |
2604 // | target parallel | critical | * |
2605 // | for | | |
2606 // | target parallel | simd | * |
2607 // | for | | |
2608 // | target parallel | sections | * |
2609 // | for | | |
2610 // | target parallel | section | * |
2611 // | for | | |
2612 // | target parallel | single | * |
2613 // | for | | |
2614 // | target parallel | parallel for | * |
2615 // | for | | |
2616 // | target parallel |parallel for simd| * |
2617 // | for | | |
2618 // | target parallel |parallel sections| * |
2619 // | for | | |
2620 // | target parallel | task | * |
2621 // | for | | |
2622 // | target parallel | taskyield | * |
2623 // | for | | |
2624 // | target parallel | barrier | * |
2625 // | for | | |
2626 // | target parallel | taskwait | * |
2627 // | for | | |
2628 // | target parallel | taskgroup | * |
2629 // | for | | |
2630 // | target parallel | flush | * |
2631 // | for | | |
2632 // | target parallel | ordered | * |
2633 // | for | | |
2634 // | target parallel | atomic | * |
2635 // | for | | |
2636 // | target parallel | target | |
2637 // | for | | |
2638 // | target parallel | target parallel | |
2639 // | for | | |
2640 // | target parallel | target parallel | |
2641 // | for | for | |
2642 // | target parallel | target enter | |
2643 // | for | data | |
2644 // | target parallel | target exit | |
2645 // | for | data | |
2646 // | target parallel | teams | |
2647 // | for | | |
2648 // | target parallel | cancellation | |
2649 // | for | point | ! |
2650 // | target parallel | cancel | ! |
2651 // | for | | |
2652 // | target parallel | taskloop | * |
2653 // | for | | |
2654 // | target parallel | taskloop simd | * |
2655 // | for | | |
2656 // | target parallel | distribute | |
2657 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002658 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002659 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002660 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002661 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002662 // | target parallel | distribute simd | |
2663 // | for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002664 // | target parallel | target parallel | |
2665 // | for | for simd | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002666 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002667 // | teams | parallel | * |
2668 // | teams | for | + |
2669 // | teams | for simd | + |
2670 // | teams | master | + |
2671 // | teams | critical | + |
2672 // | teams | simd | + |
2673 // | teams | sections | + |
2674 // | teams | section | + |
2675 // | teams | single | + |
2676 // | teams | parallel for | * |
2677 // | teams |parallel for simd| * |
2678 // | teams |parallel sections| * |
2679 // | teams | task | + |
2680 // | teams | taskyield | + |
2681 // | teams | barrier | + |
2682 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002683 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002684 // | teams | flush | + |
2685 // | teams | ordered | + |
2686 // | teams | atomic | + |
2687 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002688 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002689 // | teams | target parallel | + |
2690 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002691 // | teams | target enter | + |
2692 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002693 // | teams | target exit | + |
2694 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002695 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002696 // | teams | cancellation | |
2697 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002698 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002699 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002700 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002701 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002702 // | teams | distribute | ! |
2703 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002704 // | teams | distribute | ! |
2705 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002706 // | teams | distribute simd | ! |
Kelvin Lia579b912016-07-14 02:54:56 +00002707 // | teams | target parallel | + |
2708 // | | for simd | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002709 // +------------------+-----------------+------------------------------------+
2710 // | taskloop | parallel | * |
2711 // | taskloop | for | + |
2712 // | taskloop | for simd | + |
2713 // | taskloop | master | + |
2714 // | taskloop | critical | * |
2715 // | taskloop | simd | * |
2716 // | taskloop | sections | + |
2717 // | taskloop | section | + |
2718 // | taskloop | single | + |
2719 // | taskloop | parallel for | * |
2720 // | taskloop |parallel for simd| * |
2721 // | taskloop |parallel sections| * |
2722 // | taskloop | task | * |
2723 // | taskloop | taskyield | * |
2724 // | taskloop | barrier | + |
2725 // | taskloop | taskwait | * |
2726 // | taskloop | taskgroup | * |
2727 // | taskloop | flush | * |
2728 // | taskloop | ordered | + |
2729 // | taskloop | atomic | * |
2730 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002731 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002732 // | taskloop | target parallel | * |
2733 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002734 // | taskloop | target enter | * |
2735 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002736 // | taskloop | target exit | * |
2737 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002738 // | taskloop | teams | + |
2739 // | taskloop | cancellation | |
2740 // | | point | |
2741 // | taskloop | cancel | |
2742 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002743 // | taskloop | distribute | + |
2744 // | taskloop | distribute | + |
2745 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002746 // | taskloop | distribute | + |
2747 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002748 // | taskloop | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002749 // | taskloop | target parallel | * |
2750 // | | for simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002751 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002752 // | taskloop simd | parallel | |
2753 // | taskloop simd | for | |
2754 // | taskloop simd | for simd | |
2755 // | taskloop simd | master | |
2756 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002757 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002758 // | taskloop simd | sections | |
2759 // | taskloop simd | section | |
2760 // | taskloop simd | single | |
2761 // | taskloop simd | parallel for | |
2762 // | taskloop simd |parallel for simd| |
2763 // | taskloop simd |parallel sections| |
2764 // | taskloop simd | task | |
2765 // | taskloop simd | taskyield | |
2766 // | taskloop simd | barrier | |
2767 // | taskloop simd | taskwait | |
2768 // | taskloop simd | taskgroup | |
2769 // | taskloop simd | flush | |
2770 // | taskloop simd | ordered | + (with simd clause) |
2771 // | taskloop simd | atomic | |
2772 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002773 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002774 // | taskloop simd | target parallel | |
2775 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002776 // | taskloop simd | target enter | |
2777 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002778 // | taskloop simd | target exit | |
2779 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002780 // | taskloop simd | teams | |
2781 // | taskloop simd | cancellation | |
2782 // | | point | |
2783 // | taskloop simd | cancel | |
2784 // | taskloop simd | taskloop | |
2785 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002786 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002787 // | taskloop simd | distribute | |
2788 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002789 // | taskloop simd | distribute | |
2790 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002791 // | taskloop simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002792 // | taskloop simd | target parallel | |
2793 // | | for simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002794 // +------------------+-----------------+------------------------------------+
2795 // | distribute | parallel | * |
2796 // | distribute | for | * |
2797 // | distribute | for simd | * |
2798 // | distribute | master | * |
2799 // | distribute | critical | * |
2800 // | distribute | simd | * |
2801 // | distribute | sections | * |
2802 // | distribute | section | * |
2803 // | distribute | single | * |
2804 // | distribute | parallel for | * |
2805 // | distribute |parallel for simd| * |
2806 // | distribute |parallel sections| * |
2807 // | distribute | task | * |
2808 // | distribute | taskyield | * |
2809 // | distribute | barrier | * |
2810 // | distribute | taskwait | * |
2811 // | distribute | taskgroup | * |
2812 // | distribute | flush | * |
2813 // | distribute | ordered | + |
2814 // | distribute | atomic | * |
2815 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002816 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002817 // | distribute | target parallel | |
2818 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002819 // | distribute | target enter | |
2820 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002821 // | distribute | target exit | |
2822 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002823 // | distribute | teams | |
2824 // | distribute | cancellation | + |
2825 // | | point | |
2826 // | distribute | cancel | + |
2827 // | distribute | taskloop | * |
2828 // | distribute | taskloop simd | * |
2829 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002830 // | distribute | distribute | |
2831 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002832 // | distribute | distribute | |
2833 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002834 // | distribute | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002835 // | distribute | target parallel | |
2836 // | | for simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002837 // +------------------+-----------------+------------------------------------+
2838 // | distribute | parallel | * |
2839 // | parallel for | | |
2840 // | distribute | for | * |
2841 // | parallel for | | |
2842 // | distribute | for simd | * |
2843 // | parallel for | | |
2844 // | distribute | master | * |
2845 // | parallel for | | |
2846 // | distribute | critical | * |
2847 // | parallel for | | |
2848 // | distribute | simd | * |
2849 // | parallel for | | |
2850 // | distribute | sections | * |
2851 // | parallel for | | |
2852 // | distribute | section | * |
2853 // | parallel for | | |
2854 // | distribute | single | * |
2855 // | parallel for | | |
2856 // | distribute | parallel for | * |
2857 // | parallel for | | |
2858 // | distribute |parallel for simd| * |
2859 // | parallel for | | |
2860 // | distribute |parallel sections| * |
2861 // | parallel for | | |
2862 // | distribute | task | * |
2863 // | parallel for | | |
2864 // | parallel for | | |
2865 // | distribute | taskyield | * |
2866 // | parallel for | | |
2867 // | distribute | barrier | * |
2868 // | parallel for | | |
2869 // | distribute | taskwait | * |
2870 // | parallel for | | |
2871 // | distribute | taskgroup | * |
2872 // | parallel for | | |
2873 // | distribute | flush | * |
2874 // | parallel for | | |
2875 // | distribute | ordered | + |
2876 // | parallel for | | |
2877 // | distribute | atomic | * |
2878 // | parallel for | | |
2879 // | distribute | target | |
2880 // | parallel for | | |
2881 // | distribute | target parallel | |
2882 // | parallel for | | |
2883 // | distribute | target parallel | |
2884 // | parallel for | for | |
2885 // | distribute | target enter | |
2886 // | parallel for | data | |
2887 // | distribute | target exit | |
2888 // | parallel for | data | |
2889 // | distribute | teams | |
2890 // | parallel for | | |
2891 // | distribute | cancellation | + |
2892 // | parallel for | point | |
2893 // | distribute | cancel | + |
2894 // | parallel for | | |
2895 // | distribute | taskloop | * |
2896 // | parallel for | | |
2897 // | distribute | taskloop simd | * |
2898 // | parallel for | | |
2899 // | distribute | distribute | |
2900 // | parallel for | | |
2901 // | distribute | distribute | |
2902 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002903 // | distribute | distribute | |
2904 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002905 // | distribute | distribute simd | |
2906 // | parallel for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002907 // | distribute | target parallel | |
2908 // | parallel for | for simd | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002909 // +------------------+-----------------+------------------------------------+
2910 // | distribute | parallel | * |
2911 // | parallel for simd| | |
2912 // | distribute | for | * |
2913 // | parallel for simd| | |
2914 // | distribute | for simd | * |
2915 // | parallel for simd| | |
2916 // | distribute | master | * |
2917 // | parallel for simd| | |
2918 // | distribute | critical | * |
2919 // | parallel for simd| | |
2920 // | distribute | simd | * |
2921 // | parallel for simd| | |
2922 // | distribute | sections | * |
2923 // | parallel for simd| | |
2924 // | distribute | section | * |
2925 // | parallel for simd| | |
2926 // | distribute | single | * |
2927 // | parallel for simd| | |
2928 // | distribute | parallel for | * |
2929 // | parallel for simd| | |
2930 // | distribute |parallel for simd| * |
2931 // | parallel for simd| | |
2932 // | distribute |parallel sections| * |
2933 // | parallel for simd| | |
2934 // | distribute | task | * |
2935 // | parallel for simd| | |
2936 // | distribute | taskyield | * |
2937 // | parallel for simd| | |
2938 // | distribute | barrier | * |
2939 // | parallel for simd| | |
2940 // | distribute | taskwait | * |
2941 // | parallel for simd| | |
2942 // | distribute | taskgroup | * |
2943 // | parallel for simd| | |
2944 // | distribute | flush | * |
2945 // | parallel for simd| | |
2946 // | distribute | ordered | + |
2947 // | parallel for simd| | |
2948 // | distribute | atomic | * |
2949 // | parallel for simd| | |
2950 // | distribute | target | |
2951 // | parallel for simd| | |
2952 // | distribute | target parallel | |
2953 // | parallel for simd| | |
2954 // | distribute | target parallel | |
2955 // | parallel for simd| for | |
2956 // | distribute | target enter | |
2957 // | parallel for simd| data | |
2958 // | distribute | target exit | |
2959 // | parallel for simd| data | |
2960 // | distribute | teams | |
2961 // | parallel for simd| | |
2962 // | distribute | cancellation | + |
2963 // | parallel for simd| point | |
2964 // | distribute | cancel | + |
2965 // | parallel for simd| | |
2966 // | distribute | taskloop | * |
2967 // | parallel for simd| | |
2968 // | distribute | taskloop simd | * |
2969 // | parallel for simd| | |
2970 // | distribute | distribute | |
2971 // | parallel for simd| | |
2972 // | distribute | distribute | * |
2973 // | parallel for simd| parallel for | |
2974 // | distribute | distribute | * |
2975 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002976 // | distribute | distribute simd | * |
2977 // | parallel for simd| | |
Kelvin Lia579b912016-07-14 02:54:56 +00002978 // | distribute | target parallel | |
2979 // | parallel for simd| for simd | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002980 // +------------------+-----------------+------------------------------------+
2981 // | distribute simd | parallel | * |
2982 // | distribute simd | for | * |
2983 // | distribute simd | for simd | * |
2984 // | distribute simd | master | * |
2985 // | distribute simd | critical | * |
2986 // | distribute simd | simd | * |
2987 // | distribute simd | sections | * |
2988 // | distribute simd | section | * |
2989 // | distribute simd | single | * |
2990 // | distribute simd | parallel for | * |
2991 // | distribute simd |parallel for simd| * |
2992 // | distribute simd |parallel sections| * |
2993 // | distribute simd | task | * |
2994 // | distribute simd | taskyield | * |
2995 // | distribute simd | barrier | * |
2996 // | distribute simd | taskwait | * |
2997 // | distribute simd | taskgroup | * |
2998 // | distribute simd | flush | * |
2999 // | distribute simd | ordered | + |
3000 // | distribute simd | atomic | * |
3001 // | distribute simd | target | * |
3002 // | distribute simd | target parallel | * |
3003 // | distribute simd | target parallel | * |
3004 // | | for | |
3005 // | distribute simd | target enter | * |
3006 // | | data | |
3007 // | distribute simd | target exit | * |
3008 // | | data | |
3009 // | distribute simd | teams | * |
3010 // | distribute simd | cancellation | + |
3011 // | | point | |
3012 // | distribute simd | cancel | + |
3013 // | distribute simd | taskloop | * |
3014 // | distribute simd | taskloop simd | * |
3015 // | distribute simd | distribute | |
3016 // | distribute simd | distribute | * |
3017 // | | parallel for | |
3018 // | distribute simd | distribute | * |
3019 // | |parallel for simd| |
3020 // | distribute simd | distribute simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003021 // | distribute simd | target parallel | * |
3022 // | | for simd | |
3023 // +------------------+-----------------+------------------------------------+
3024 // | target parallel | parallel | * |
3025 // | for simd | | |
3026 // | target parallel | for | * |
3027 // | for simd | | |
3028 // | target parallel | for simd | * |
3029 // | for simd | | |
3030 // | target parallel | master | * |
3031 // | for simd | | |
3032 // | target parallel | critical | * |
3033 // | for simd | | |
3034 // | target parallel | simd | ! |
3035 // | for simd | | |
3036 // | target parallel | sections | * |
3037 // | for simd | | |
3038 // | target parallel | section | * |
3039 // | for simd | | |
3040 // | target parallel | single | * |
3041 // | for simd | | |
3042 // | target parallel | parallel for | * |
3043 // | for simd | | |
3044 // | target parallel |parallel for simd| * |
3045 // | for simd | | |
3046 // | target parallel |parallel sections| * |
3047 // | for simd | | |
3048 // | target parallel | task | * |
3049 // | for simd | | |
3050 // | target parallel | taskyield | * |
3051 // | for simd | | |
3052 // | target parallel | barrier | * |
3053 // | for simd | | |
3054 // | target parallel | taskwait | * |
3055 // | for simd | | |
3056 // | target parallel | taskgroup | * |
3057 // | for simd | | |
3058 // | target parallel | flush | * |
3059 // | for simd | | |
3060 // | target parallel | ordered | + (with simd clause) |
3061 // | for simd | | |
3062 // | target parallel | atomic | * |
3063 // | for simd | | |
3064 // | target parallel | target | * |
3065 // | for simd | | |
3066 // | target parallel | target parallel | * |
3067 // | for simd | | |
3068 // | target parallel | target parallel | * |
3069 // | for simd | for | |
3070 // | target parallel | target enter | * |
3071 // | for simd | data | |
3072 // | target parallel | target exit | * |
3073 // | for simd | data | |
3074 // | target parallel | teams | * |
3075 // | for simd | | |
3076 // | target parallel | cancellation | * |
3077 // | for simd | point | |
3078 // | target parallel | cancel | * |
3079 // | for simd | | |
3080 // | target parallel | taskloop | * |
3081 // | for simd | | |
3082 // | target parallel | taskloop simd | * |
3083 // | for simd | | |
3084 // | target parallel | distribute | * |
3085 // | for simd | | |
3086 // | target parallel | distribute | * |
3087 // | for simd | parallel for | |
3088 // | target parallel | distribute | * |
3089 // | for simd |parallel for simd| |
3090 // | target parallel | distribute simd | * |
3091 // | for simd | | |
3092 // | target parallel | target parallel | * |
3093 // | for simd | for simd | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003094 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003095 if (Stack->getCurScope()) {
3096 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003097 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003098 bool NestingProhibited = false;
3099 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003100 enum {
3101 NoRecommend,
3102 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003103 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003104 ShouldBeInTargetRegion,
3105 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003106 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003107 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003108 // OpenMP [2.16, Nesting of Regions]
3109 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003110 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003111 // An ordered construct with the simd clause is the only OpenMP
3112 // construct that can appear in the simd region.
3113 // Allowing a SIMD consruct nested in another SIMD construct is an
3114 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3115 // message.
3116 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3117 ? diag::err_omp_prohibited_region_simd
3118 : diag::warn_omp_nesting_simd);
3119 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003120 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003121 if (ParentRegion == OMPD_atomic) {
3122 // OpenMP [2.16, Nesting of Regions]
3123 // OpenMP constructs may not be nested inside an atomic region.
3124 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3125 return true;
3126 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003127 if (CurrentRegion == OMPD_section) {
3128 // OpenMP [2.7.2, sections Construct, Restrictions]
3129 // Orphaned section directives are prohibited. That is, the section
3130 // directives must appear within the sections construct and must not be
3131 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003132 if (ParentRegion != OMPD_sections &&
3133 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003134 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3135 << (ParentRegion != OMPD_unknown)
3136 << getOpenMPDirectiveName(ParentRegion);
3137 return true;
3138 }
3139 return false;
3140 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003141 // Allow some constructs to be orphaned (they could be used in functions,
3142 // called from OpenMP regions with the required preconditions).
3143 if (ParentRegion == OMPD_unknown)
3144 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003145 if (CurrentRegion == OMPD_cancellation_point ||
3146 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003147 // OpenMP [2.16, Nesting of Regions]
3148 // A cancellation point construct for which construct-type-clause is
3149 // taskgroup must be nested inside a task construct. A cancellation
3150 // point construct for which construct-type-clause is not taskgroup must
3151 // be closely nested inside an OpenMP construct that matches the type
3152 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003153 // A cancel construct for which construct-type-clause is taskgroup must be
3154 // nested inside a task construct. A cancel construct for which
3155 // construct-type-clause is not taskgroup must be closely nested inside an
3156 // OpenMP construct that matches the type specified in
3157 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003158 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003159 !((CancelRegion == OMPD_parallel &&
3160 (ParentRegion == OMPD_parallel ||
3161 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003162 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003163 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3164 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003165 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3166 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003167 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3168 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003169 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003170 // OpenMP [2.16, Nesting of Regions]
3171 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003172 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003173 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003174 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003175 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3176 // OpenMP [2.16, Nesting of Regions]
3177 // A critical region may not be nested (closely or otherwise) inside a
3178 // critical region with the same name. Note that this restriction is not
3179 // sufficient to prevent deadlock.
3180 SourceLocation PreviousCriticalLoc;
3181 bool DeadLock =
3182 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3183 OpenMPDirectiveKind K,
3184 const DeclarationNameInfo &DNI,
3185 SourceLocation Loc)
3186 ->bool {
3187 if (K == OMPD_critical &&
3188 DNI.getName() == CurrentName.getName()) {
3189 PreviousCriticalLoc = Loc;
3190 return true;
3191 } else
3192 return false;
3193 },
3194 false /* skip top directive */);
3195 if (DeadLock) {
3196 SemaRef.Diag(StartLoc,
3197 diag::err_omp_prohibited_region_critical_same_name)
3198 << CurrentName.getName();
3199 if (PreviousCriticalLoc.isValid())
3200 SemaRef.Diag(PreviousCriticalLoc,
3201 diag::note_omp_previous_critical_region);
3202 return true;
3203 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003204 } else if (CurrentRegion == OMPD_barrier) {
3205 // OpenMP [2.16, Nesting of Regions]
3206 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003207 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003208 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3209 isOpenMPTaskingDirective(ParentRegion) ||
3210 ParentRegion == OMPD_master ||
3211 ParentRegion == OMPD_critical ||
3212 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003213 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003214 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003215 // OpenMP [2.16, Nesting of Regions]
3216 // A worksharing region may not be closely nested inside a worksharing,
3217 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003218 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3219 isOpenMPTaskingDirective(ParentRegion) ||
3220 ParentRegion == OMPD_master ||
3221 ParentRegion == OMPD_critical ||
3222 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003223 Recommend = ShouldBeInParallelRegion;
3224 } else if (CurrentRegion == OMPD_ordered) {
3225 // OpenMP [2.16, Nesting of Regions]
3226 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003227 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003228 // An ordered region must be closely nested inside a loop region (or
3229 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003230 // OpenMP [2.8.1,simd Construct, Restrictions]
3231 // An ordered construct with the simd clause is the only OpenMP construct
3232 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003233 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003234 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003235 !(isOpenMPSimdDirective(ParentRegion) ||
3236 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003237 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003238 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3239 // OpenMP [2.16, Nesting of Regions]
3240 // If specified, a teams construct must be contained within a target
3241 // construct.
3242 NestingProhibited = ParentRegion != OMPD_target;
3243 Recommend = ShouldBeInTargetRegion;
3244 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3245 }
3246 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3247 // OpenMP [2.16, Nesting of Regions]
3248 // distribute, parallel, parallel sections, parallel workshare, and the
3249 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3250 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003251 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3252 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003253 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003254 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003255 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3256 // OpenMP 4.5 [2.17 Nesting of Regions]
3257 // The region associated with the distribute construct must be strictly
3258 // nested inside a teams region
3259 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3260 Recommend = ShouldBeInTeamsRegion;
3261 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003262 if (!NestingProhibited &&
3263 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3264 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3265 // OpenMP 4.5 [2.17 Nesting of Regions]
3266 // If a target, target update, target data, target enter data, or
3267 // target exit data construct is encountered during execution of a
3268 // target region, the behavior is unspecified.
3269 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003270 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3271 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003272 if (isOpenMPTargetExecutionDirective(K)) {
3273 OffendingRegion = K;
3274 return true;
3275 } else
3276 return false;
3277 },
3278 false /* don't skip top directive */);
3279 CloseNesting = false;
3280 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003281 if (NestingProhibited) {
3282 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003283 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3284 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00003285 return true;
3286 }
3287 }
3288 return false;
3289}
3290
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003291static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3292 ArrayRef<OMPClause *> Clauses,
3293 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3294 bool ErrorFound = false;
3295 unsigned NamedModifiersNumber = 0;
3296 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3297 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003298 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003299 for (const auto *C : Clauses) {
3300 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3301 // At most one if clause without a directive-name-modifier can appear on
3302 // the directive.
3303 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3304 if (FoundNameModifiers[CurNM]) {
3305 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3306 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3307 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3308 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003309 } else if (CurNM != OMPD_unknown) {
3310 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003311 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003312 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003313 FoundNameModifiers[CurNM] = IC;
3314 if (CurNM == OMPD_unknown)
3315 continue;
3316 // Check if the specified name modifier is allowed for the current
3317 // directive.
3318 // At most one if clause with the particular directive-name-modifier can
3319 // appear on the directive.
3320 bool MatchFound = false;
3321 for (auto NM : AllowedNameModifiers) {
3322 if (CurNM == NM) {
3323 MatchFound = true;
3324 break;
3325 }
3326 }
3327 if (!MatchFound) {
3328 S.Diag(IC->getNameModifierLoc(),
3329 diag::err_omp_wrong_if_directive_name_modifier)
3330 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3331 ErrorFound = true;
3332 }
3333 }
3334 }
3335 // If any if clause on the directive includes a directive-name-modifier then
3336 // all if clauses on the directive must include a directive-name-modifier.
3337 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3338 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3339 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3340 diag::err_omp_no_more_if_clause);
3341 } else {
3342 std::string Values;
3343 std::string Sep(", ");
3344 unsigned AllowedCnt = 0;
3345 unsigned TotalAllowedNum =
3346 AllowedNameModifiers.size() - NamedModifiersNumber;
3347 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3348 ++Cnt) {
3349 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3350 if (!FoundNameModifiers[NM]) {
3351 Values += "'";
3352 Values += getOpenMPDirectiveName(NM);
3353 Values += "'";
3354 if (AllowedCnt + 2 == TotalAllowedNum)
3355 Values += " or ";
3356 else if (AllowedCnt + 1 != TotalAllowedNum)
3357 Values += Sep;
3358 ++AllowedCnt;
3359 }
3360 }
3361 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3362 diag::err_omp_unnamed_if_clause)
3363 << (TotalAllowedNum > 1) << Values;
3364 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003365 for (auto Loc : NameModifierLoc) {
3366 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3367 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003368 ErrorFound = true;
3369 }
3370 return ErrorFound;
3371}
3372
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003373StmtResult Sema::ActOnOpenMPExecutableDirective(
3374 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3375 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3376 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003377 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003378 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3379 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003380 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003381
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003382 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003383 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003384 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003385 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003386 if (AStmt) {
3387 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3388
3389 // Check default data sharing attributes for referenced variables.
3390 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3391 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3392 if (DSAChecker.isErrorFound())
3393 return StmtError();
3394 // Generate list of implicitly defined firstprivate variables.
3395 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003396
3397 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3398 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3399 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3400 SourceLocation(), SourceLocation())) {
3401 ClausesWithImplicit.push_back(Implicit);
3402 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3403 DSAChecker.getImplicitFirstprivate().size();
3404 } else
3405 ErrorFound = true;
3406 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003407 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003408
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003409 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003410 switch (Kind) {
3411 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003412 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3413 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003414 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003415 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003416 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003417 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3418 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003419 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003420 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003421 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3422 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003423 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003424 case OMPD_for_simd:
3425 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3426 EndLoc, VarsWithInheritedDSA);
3427 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003428 case OMPD_sections:
3429 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3430 EndLoc);
3431 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003432 case OMPD_section:
3433 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003434 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003435 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3436 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003437 case OMPD_single:
3438 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3439 EndLoc);
3440 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003441 case OMPD_master:
3442 assert(ClausesWithImplicit.empty() &&
3443 "No clauses are allowed for 'omp master' directive");
3444 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3445 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003446 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003447 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3448 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003449 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003450 case OMPD_parallel_for:
3451 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3452 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003453 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003454 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003455 case OMPD_parallel_for_simd:
3456 Res = ActOnOpenMPParallelForSimdDirective(
3457 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003458 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003459 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003460 case OMPD_parallel_sections:
3461 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3462 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003463 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003464 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003465 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003466 Res =
3467 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003468 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003469 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003470 case OMPD_taskyield:
3471 assert(ClausesWithImplicit.empty() &&
3472 "No clauses are allowed for 'omp taskyield' directive");
3473 assert(AStmt == nullptr &&
3474 "No associated statement allowed for 'omp taskyield' directive");
3475 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3476 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003477 case OMPD_barrier:
3478 assert(ClausesWithImplicit.empty() &&
3479 "No clauses are allowed for 'omp barrier' directive");
3480 assert(AStmt == nullptr &&
3481 "No associated statement allowed for 'omp barrier' directive");
3482 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3483 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003484 case OMPD_taskwait:
3485 assert(ClausesWithImplicit.empty() &&
3486 "No clauses are allowed for 'omp taskwait' directive");
3487 assert(AStmt == nullptr &&
3488 "No associated statement allowed for 'omp taskwait' directive");
3489 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3490 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003491 case OMPD_taskgroup:
3492 assert(ClausesWithImplicit.empty() &&
3493 "No clauses are allowed for 'omp taskgroup' directive");
3494 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3495 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003496 case OMPD_flush:
3497 assert(AStmt == nullptr &&
3498 "No associated statement allowed for 'omp flush' directive");
3499 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3500 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003501 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003502 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3503 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003504 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003505 case OMPD_atomic:
3506 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3507 EndLoc);
3508 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003509 case OMPD_teams:
3510 Res =
3511 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3512 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003513 case OMPD_target:
3514 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3515 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003516 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003517 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003518 case OMPD_target_parallel:
3519 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3520 StartLoc, EndLoc);
3521 AllowedNameModifiers.push_back(OMPD_target);
3522 AllowedNameModifiers.push_back(OMPD_parallel);
3523 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003524 case OMPD_target_parallel_for:
3525 Res = ActOnOpenMPTargetParallelForDirective(
3526 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3527 AllowedNameModifiers.push_back(OMPD_target);
3528 AllowedNameModifiers.push_back(OMPD_parallel);
3529 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003530 case OMPD_cancellation_point:
3531 assert(ClausesWithImplicit.empty() &&
3532 "No clauses are allowed for 'omp cancellation point' directive");
3533 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3534 "cancellation point' directive");
3535 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3536 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003537 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003538 assert(AStmt == nullptr &&
3539 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003540 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3541 CancelRegion);
3542 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003543 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003544 case OMPD_target_data:
3545 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3546 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003547 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003548 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003549 case OMPD_target_enter_data:
3550 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3551 EndLoc);
3552 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3553 break;
Samuel Antao72590762016-01-19 20:04:50 +00003554 case OMPD_target_exit_data:
3555 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3556 EndLoc);
3557 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3558 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003559 case OMPD_taskloop:
3560 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3561 EndLoc, VarsWithInheritedDSA);
3562 AllowedNameModifiers.push_back(OMPD_taskloop);
3563 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003564 case OMPD_taskloop_simd:
3565 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3566 EndLoc, VarsWithInheritedDSA);
3567 AllowedNameModifiers.push_back(OMPD_taskloop);
3568 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003569 case OMPD_distribute:
3570 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3571 EndLoc, VarsWithInheritedDSA);
3572 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003573 case OMPD_target_update:
3574 assert(!AStmt && "Statement is not allowed for target update");
3575 Res =
3576 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3577 AllowedNameModifiers.push_back(OMPD_target_update);
3578 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003579 case OMPD_distribute_parallel_for:
3580 Res = ActOnOpenMPDistributeParallelForDirective(
3581 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3582 AllowedNameModifiers.push_back(OMPD_parallel);
3583 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003584 case OMPD_distribute_parallel_for_simd:
3585 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3586 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3587 AllowedNameModifiers.push_back(OMPD_parallel);
3588 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003589 case OMPD_distribute_simd:
3590 Res = ActOnOpenMPDistributeSimdDirective(
3591 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3592 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003593 case OMPD_target_parallel_for_simd:
3594 Res = ActOnOpenMPTargetParallelForSimdDirective(
3595 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3596 AllowedNameModifiers.push_back(OMPD_target);
3597 AllowedNameModifiers.push_back(OMPD_parallel);
3598 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003599 case OMPD_declare_target:
3600 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003601 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003602 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003603 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003604 llvm_unreachable("OpenMP Directive is not allowed");
3605 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003606 llvm_unreachable("Unknown OpenMP directive");
3607 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003608
Alexey Bataev4acb8592014-07-07 13:01:15 +00003609 for (auto P : VarsWithInheritedDSA) {
3610 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3611 << P.first << P.second->getSourceRange();
3612 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003613 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3614
3615 if (!AllowedNameModifiers.empty())
3616 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3617 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003618
Alexey Bataeved09d242014-05-28 05:53:51 +00003619 if (ErrorFound)
3620 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003621 return Res;
3622}
3623
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003624Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3625 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003626 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003627 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3628 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003629 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003630 assert(Linears.size() == LinModifiers.size());
3631 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003632 if (!DG || DG.get().isNull())
3633 return DeclGroupPtrTy();
3634
3635 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003636 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003637 return DG;
3638 }
3639 auto *ADecl = DG.get().getSingleDecl();
3640 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3641 ADecl = FTD->getTemplatedDecl();
3642
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003643 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3644 if (!FD) {
3645 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003646 return DeclGroupPtrTy();
3647 }
3648
Alexey Bataev2af33e32016-04-07 12:45:37 +00003649 // OpenMP [2.8.2, declare simd construct, Description]
3650 // The parameter of the simdlen clause must be a constant positive integer
3651 // expression.
3652 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003653 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003654 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003655 // OpenMP [2.8.2, declare simd construct, Description]
3656 // The special this pointer can be used as if was one of the arguments to the
3657 // function in any of the linear, aligned, or uniform clauses.
3658 // The uniform clause declares one or more arguments to have an invariant
3659 // value for all concurrent invocations of the function in the execution of a
3660 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003661 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3662 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003663 for (auto *E : Uniforms) {
3664 E = E->IgnoreParenImpCasts();
3665 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3666 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3667 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3668 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003669 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3670 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003671 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003672 }
3673 if (isa<CXXThisExpr>(E)) {
3674 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003675 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003676 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003677 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3678 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003679 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003680 // OpenMP [2.8.2, declare simd construct, Description]
3681 // The aligned clause declares that the object to which each list item points
3682 // is aligned to the number of bytes expressed in the optional parameter of
3683 // the aligned clause.
3684 // The special this pointer can be used as if was one of the arguments to the
3685 // function in any of the linear, aligned, or uniform clauses.
3686 // The type of list items appearing in the aligned clause must be array,
3687 // pointer, reference to array, or reference to pointer.
3688 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3689 Expr *AlignedThis = nullptr;
3690 for (auto *E : Aligneds) {
3691 E = E->IgnoreParenImpCasts();
3692 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3693 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3694 auto *CanonPVD = PVD->getCanonicalDecl();
3695 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3696 FD->getParamDecl(PVD->getFunctionScopeIndex())
3697 ->getCanonicalDecl() == CanonPVD) {
3698 // OpenMP [2.8.1, simd construct, Restrictions]
3699 // A list-item cannot appear in more than one aligned clause.
3700 if (AlignedArgs.count(CanonPVD) > 0) {
3701 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3702 << 1 << E->getSourceRange();
3703 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3704 diag::note_omp_explicit_dsa)
3705 << getOpenMPClauseName(OMPC_aligned);
3706 continue;
3707 }
3708 AlignedArgs[CanonPVD] = E;
3709 QualType QTy = PVD->getType()
3710 .getNonReferenceType()
3711 .getUnqualifiedType()
3712 .getCanonicalType();
3713 const Type *Ty = QTy.getTypePtrOrNull();
3714 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3715 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3716 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3717 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3718 }
3719 continue;
3720 }
3721 }
3722 if (isa<CXXThisExpr>(E)) {
3723 if (AlignedThis) {
3724 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3725 << 2 << E->getSourceRange();
3726 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3727 << getOpenMPClauseName(OMPC_aligned);
3728 }
3729 AlignedThis = E;
3730 continue;
3731 }
3732 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3733 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3734 }
3735 // The optional parameter of the aligned clause, alignment, must be a constant
3736 // positive integer expression. If no optional parameter is specified,
3737 // implementation-defined default alignments for SIMD instructions on the
3738 // target platforms are assumed.
3739 SmallVector<Expr *, 4> NewAligns;
3740 for (auto *E : Alignments) {
3741 ExprResult Align;
3742 if (E)
3743 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3744 NewAligns.push_back(Align.get());
3745 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003746 // OpenMP [2.8.2, declare simd construct, Description]
3747 // The linear clause declares one or more list items to be private to a SIMD
3748 // lane and to have a linear relationship with respect to the iteration space
3749 // of a loop.
3750 // The special this pointer can be used as if was one of the arguments to the
3751 // function in any of the linear, aligned, or uniform clauses.
3752 // When a linear-step expression is specified in a linear clause it must be
3753 // either a constant integer expression or an integer-typed parameter that is
3754 // specified in a uniform clause on the directive.
3755 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3756 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3757 auto MI = LinModifiers.begin();
3758 for (auto *E : Linears) {
3759 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3760 ++MI;
3761 E = E->IgnoreParenImpCasts();
3762 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3763 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3764 auto *CanonPVD = PVD->getCanonicalDecl();
3765 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3766 FD->getParamDecl(PVD->getFunctionScopeIndex())
3767 ->getCanonicalDecl() == CanonPVD) {
3768 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3769 // A list-item cannot appear in more than one linear clause.
3770 if (LinearArgs.count(CanonPVD) > 0) {
3771 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3772 << getOpenMPClauseName(OMPC_linear)
3773 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3774 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3775 diag::note_omp_explicit_dsa)
3776 << getOpenMPClauseName(OMPC_linear);
3777 continue;
3778 }
3779 // Each argument can appear in at most one uniform or linear clause.
3780 if (UniformedArgs.count(CanonPVD) > 0) {
3781 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3782 << getOpenMPClauseName(OMPC_linear)
3783 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3784 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3785 diag::note_omp_explicit_dsa)
3786 << getOpenMPClauseName(OMPC_uniform);
3787 continue;
3788 }
3789 LinearArgs[CanonPVD] = E;
3790 if (E->isValueDependent() || E->isTypeDependent() ||
3791 E->isInstantiationDependent() ||
3792 E->containsUnexpandedParameterPack())
3793 continue;
3794 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3795 PVD->getOriginalType());
3796 continue;
3797 }
3798 }
3799 if (isa<CXXThisExpr>(E)) {
3800 if (UniformedLinearThis) {
3801 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3802 << getOpenMPClauseName(OMPC_linear)
3803 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3804 << E->getSourceRange();
3805 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3806 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3807 : OMPC_linear);
3808 continue;
3809 }
3810 UniformedLinearThis = E;
3811 if (E->isValueDependent() || E->isTypeDependent() ||
3812 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3813 continue;
3814 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3815 E->getType());
3816 continue;
3817 }
3818 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3819 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3820 }
3821 Expr *Step = nullptr;
3822 Expr *NewStep = nullptr;
3823 SmallVector<Expr *, 4> NewSteps;
3824 for (auto *E : Steps) {
3825 // Skip the same step expression, it was checked already.
3826 if (Step == E || !E) {
3827 NewSteps.push_back(E ? NewStep : nullptr);
3828 continue;
3829 }
3830 Step = E;
3831 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3832 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3833 auto *CanonPVD = PVD->getCanonicalDecl();
3834 if (UniformedArgs.count(CanonPVD) == 0) {
3835 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3836 << Step->getSourceRange();
3837 } else if (E->isValueDependent() || E->isTypeDependent() ||
3838 E->isInstantiationDependent() ||
3839 E->containsUnexpandedParameterPack() ||
3840 CanonPVD->getType()->hasIntegerRepresentation())
3841 NewSteps.push_back(Step);
3842 else {
3843 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3844 << Step->getSourceRange();
3845 }
3846 continue;
3847 }
3848 NewStep = Step;
3849 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3850 !Step->isInstantiationDependent() &&
3851 !Step->containsUnexpandedParameterPack()) {
3852 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3853 .get();
3854 if (NewStep)
3855 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3856 }
3857 NewSteps.push_back(NewStep);
3858 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003859 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3860 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003861 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003862 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3863 const_cast<Expr **>(Linears.data()), Linears.size(),
3864 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3865 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003866 ADecl->addAttr(NewAttr);
3867 return ConvertDeclToDeclGroup(ADecl);
3868}
3869
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003870StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3871 Stmt *AStmt,
3872 SourceLocation StartLoc,
3873 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003874 if (!AStmt)
3875 return StmtError();
3876
Alexey Bataev9959db52014-05-06 10:08:46 +00003877 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3878 // 1.2.2 OpenMP Language Terminology
3879 // Structured block - An executable statement with a single entry at the
3880 // top and a single exit at the bottom.
3881 // The point of exit cannot be a branch out of the structured block.
3882 // longjmp() and throw() must not violate the entry/exit criteria.
3883 CS->getCapturedDecl()->setNothrow();
3884
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003885 getCurFunction()->setHasBranchProtectedScope();
3886
Alexey Bataev25e5b442015-09-15 12:52:43 +00003887 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3888 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003889}
3890
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003891namespace {
3892/// \brief Helper class for checking canonical form of the OpenMP loops and
3893/// extracting iteration space of each loop in the loop nest, that will be used
3894/// for IR generation.
3895class OpenMPIterationSpaceChecker {
3896 /// \brief Reference to Sema.
3897 Sema &SemaRef;
3898 /// \brief A location for diagnostics (when there is no some better location).
3899 SourceLocation DefaultLoc;
3900 /// \brief A location for diagnostics (when increment is not compatible).
3901 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003902 /// \brief A source location for referring to loop init later.
3903 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003904 /// \brief A source location for referring to condition later.
3905 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003906 /// \brief A source location for referring to increment later.
3907 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003908 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003909 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003910 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003911 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003912 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003913 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003914 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003915 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003916 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003917 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 /// \brief This flag is true when condition is one of:
3919 /// Var < UB
3920 /// Var <= UB
3921 /// UB > Var
3922 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003923 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003924 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003925 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003926 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003927 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003928
3929public:
3930 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003931 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003932 /// \brief Check init-expr for canonical loop form and save loop counter
3933 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003934 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003935 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3936 /// for less/greater and for strict/non-strict comparison.
3937 bool CheckCond(Expr *S);
3938 /// \brief Check incr-expr for canonical loop form and return true if it
3939 /// does not conform, otherwise save loop step (#Step).
3940 bool CheckInc(Expr *S);
3941 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003942 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003943 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003944 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003945 /// \brief Source range of the loop init.
3946 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3947 /// \brief Source range of the loop condition.
3948 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3949 /// \brief Source range of the loop increment.
3950 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3951 /// \brief True if the step should be subtracted.
3952 bool ShouldSubtractStep() const { return SubtractStep; }
3953 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003954 Expr *
3955 BuildNumIterations(Scope *S, const bool LimitedType,
3956 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003957 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003958 Expr *BuildPreCond(Scope *S, Expr *Cond,
3959 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003960 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003961 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3962 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003963 /// \brief Build reference expression to the private counter be used for
3964 /// codegen.
3965 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003966 /// \brief Build initization of the counter be used for codegen.
3967 Expr *BuildCounterInit() const;
3968 /// \brief Build step of the counter be used for codegen.
3969 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003970 /// \brief Return true if any expression is dependent.
3971 bool Dependent() const;
3972
3973private:
3974 /// \brief Check the right-hand side of an assignment in the increment
3975 /// expression.
3976 bool CheckIncRHS(Expr *RHS);
3977 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003978 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003979 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003980 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003981 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003982 /// \brief Helper to set loop increment.
3983 bool SetStep(Expr *NewStep, bool Subtract);
3984};
3985
3986bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003987 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003988 assert(!LB && !UB && !Step);
3989 return false;
3990 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003991 return LCDecl->getType()->isDependentType() ||
3992 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3993 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003994}
3995
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003996static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003997 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3998 E = ExprTemp->getSubExpr();
3999
4000 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4001 E = MTE->GetTemporaryExpr();
4002
4003 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4004 E = Binder->getSubExpr();
4005
4006 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4007 E = ICE->getSubExprAsWritten();
4008 return E->IgnoreParens();
4009}
4010
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004011bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4012 Expr *NewLCRefExpr,
4013 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004014 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004015 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004016 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004017 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004018 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004019 LCDecl = getCanonicalDecl(NewLCDecl);
4020 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004021 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4022 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004023 if ((Ctor->isCopyOrMoveConstructor() ||
4024 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4025 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004026 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004027 LB = NewLB;
4028 return false;
4029}
4030
4031bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004032 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004033 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004034 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4035 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004036 if (!NewUB)
4037 return true;
4038 UB = NewUB;
4039 TestIsLessOp = LessOp;
4040 TestIsStrictOp = StrictOp;
4041 ConditionSrcRange = SR;
4042 ConditionLoc = SL;
4043 return false;
4044}
4045
4046bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4047 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004048 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004049 if (!NewStep)
4050 return true;
4051 if (!NewStep->isValueDependent()) {
4052 // Check that the step is integer expression.
4053 SourceLocation StepLoc = NewStep->getLocStart();
4054 ExprResult Val =
4055 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4056 if (Val.isInvalid())
4057 return true;
4058 NewStep = Val.get();
4059
4060 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4061 // If test-expr is of form var relational-op b and relational-op is < or
4062 // <= then incr-expr must cause var to increase on each iteration of the
4063 // loop. If test-expr is of form var relational-op b and relational-op is
4064 // > or >= then incr-expr must cause var to decrease on each iteration of
4065 // the loop.
4066 // If test-expr is of form b relational-op var and relational-op is < or
4067 // <= then incr-expr must cause var to decrease on each iteration of the
4068 // loop. If test-expr is of form b relational-op var and relational-op is
4069 // > or >= then incr-expr must cause var to increase on each iteration of
4070 // the loop.
4071 llvm::APSInt Result;
4072 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4073 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4074 bool IsConstNeg =
4075 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004076 bool IsConstPos =
4077 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004078 bool IsConstZero = IsConstant && !Result.getBoolValue();
4079 if (UB && (IsConstZero ||
4080 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004081 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004082 SemaRef.Diag(NewStep->getExprLoc(),
4083 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004084 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004085 SemaRef.Diag(ConditionLoc,
4086 diag::note_omp_loop_cond_requres_compatible_incr)
4087 << TestIsLessOp << ConditionSrcRange;
4088 return true;
4089 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004090 if (TestIsLessOp == Subtract) {
4091 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4092 NewStep).get();
4093 Subtract = !Subtract;
4094 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004095 }
4096
4097 Step = NewStep;
4098 SubtractStep = Subtract;
4099 return false;
4100}
4101
Alexey Bataev9c821032015-04-30 04:23:23 +00004102bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004103 // Check init-expr for canonical loop form and save loop counter
4104 // variable - #Var and its initialization value - #LB.
4105 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4106 // var = lb
4107 // integer-type var = lb
4108 // random-access-iterator-type var = lb
4109 // pointer-type var = lb
4110 //
4111 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004112 if (EmitDiags) {
4113 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4114 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004115 return true;
4116 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004117 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4118 if (!ExprTemp->cleanupsHaveSideEffects())
4119 S = ExprTemp->getSubExpr();
4120
Alexander Musmana5f070a2014-10-01 06:03:56 +00004121 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004122 if (Expr *E = dyn_cast<Expr>(S))
4123 S = E->IgnoreParens();
4124 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004125 if (BO->getOpcode() == BO_Assign) {
4126 auto *LHS = BO->getLHS()->IgnoreParens();
4127 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4128 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4129 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4130 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4131 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4132 }
4133 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4134 if (ME->isArrow() &&
4135 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4136 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4137 }
4138 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004139 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4140 if (DS->isSingleDecl()) {
4141 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004142 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004143 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004144 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004145 SemaRef.Diag(S->getLocStart(),
4146 diag::ext_omp_loop_not_canonical_init)
4147 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004148 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004149 }
4150 }
4151 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004152 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4153 if (CE->getOperator() == OO_Equal) {
4154 auto *LHS = CE->getArg(0);
4155 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4156 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4157 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4158 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4159 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4160 }
4161 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4162 if (ME->isArrow() &&
4163 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4164 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4165 }
4166 }
4167 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004168
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004169 if (Dependent() || SemaRef.CurContext->isDependentContext())
4170 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004171 if (EmitDiags) {
4172 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4173 << S->getSourceRange();
4174 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004175 return true;
4176}
4177
Alexey Bataev23b69422014-06-18 07:08:49 +00004178/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004179/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004180static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004181 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004182 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004183 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004184 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4185 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004186 if ((Ctor->isCopyOrMoveConstructor() ||
4187 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4188 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004189 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004190 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4191 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4192 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4193 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4194 return getCanonicalDecl(ME->getMemberDecl());
4195 return getCanonicalDecl(VD);
4196 }
4197 }
4198 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4199 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4200 return getCanonicalDecl(ME->getMemberDecl());
4201 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004202}
4203
4204bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4205 // Check test-expr for canonical form, save upper-bound UB, flags for
4206 // less/greater and for strict/non-strict comparison.
4207 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4208 // var relational-op b
4209 // b relational-op var
4210 //
4211 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004212 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004213 return true;
4214 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004215 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004216 SourceLocation CondLoc = S->getLocStart();
4217 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4218 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004219 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004220 return SetUB(BO->getRHS(),
4221 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4222 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4223 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004224 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004225 return SetUB(BO->getLHS(),
4226 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4227 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4228 BO->getSourceRange(), BO->getOperatorLoc());
4229 }
4230 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4231 if (CE->getNumArgs() == 2) {
4232 auto Op = CE->getOperator();
4233 switch (Op) {
4234 case OO_Greater:
4235 case OO_GreaterEqual:
4236 case OO_Less:
4237 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004238 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004239 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4240 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4241 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004242 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004243 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4244 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4245 CE->getOperatorLoc());
4246 break;
4247 default:
4248 break;
4249 }
4250 }
4251 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004252 if (Dependent() || SemaRef.CurContext->isDependentContext())
4253 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004254 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004255 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004256 return true;
4257}
4258
4259bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4260 // RHS of canonical loop form increment can be:
4261 // var + incr
4262 // incr + var
4263 // var - incr
4264 //
4265 RHS = RHS->IgnoreParenImpCasts();
4266 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4267 if (BO->isAdditiveOp()) {
4268 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004269 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004270 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004271 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004272 return SetStep(BO->getLHS(), false);
4273 }
4274 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4275 bool IsAdd = CE->getOperator() == OO_Plus;
4276 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004277 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004278 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004279 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004280 return SetStep(CE->getArg(0), false);
4281 }
4282 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004283 if (Dependent() || SemaRef.CurContext->isDependentContext())
4284 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004285 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004286 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004287 return true;
4288}
4289
4290bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4291 // Check incr-expr for canonical loop form and return true if it
4292 // does not conform.
4293 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4294 // ++var
4295 // var++
4296 // --var
4297 // var--
4298 // var += incr
4299 // var -= incr
4300 // var = var + incr
4301 // var = incr + var
4302 // var = var - incr
4303 //
4304 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004305 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004306 return true;
4307 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004308 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4309 if (!ExprTemp->cleanupsHaveSideEffects())
4310 S = ExprTemp->getSubExpr();
4311
Alexander Musmana5f070a2014-10-01 06:03:56 +00004312 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004313 S = S->IgnoreParens();
4314 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004315 if (UO->isIncrementDecrementOp() &&
4316 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004317 return SetStep(
4318 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4319 (UO->isDecrementOp() ? -1 : 1)).get(),
4320 false);
4321 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4322 switch (BO->getOpcode()) {
4323 case BO_AddAssign:
4324 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004325 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004326 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4327 break;
4328 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004329 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004330 return CheckIncRHS(BO->getRHS());
4331 break;
4332 default:
4333 break;
4334 }
4335 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4336 switch (CE->getOperator()) {
4337 case OO_PlusPlus:
4338 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004339 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004340 return SetStep(
4341 SemaRef.ActOnIntegerConstant(
4342 CE->getLocStart(),
4343 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4344 false);
4345 break;
4346 case OO_PlusEqual:
4347 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004348 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004349 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4350 break;
4351 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004352 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004353 return CheckIncRHS(CE->getArg(1));
4354 break;
4355 default:
4356 break;
4357 }
4358 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004359 if (Dependent() || SemaRef.CurContext->isDependentContext())
4360 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004361 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004362 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004363 return true;
4364}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004365
Alexey Bataev5a3af132016-03-29 08:58:54 +00004366static ExprResult
4367tryBuildCapture(Sema &SemaRef, Expr *Capture,
4368 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004369 if (SemaRef.CurContext->isDependentContext())
4370 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004371 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4372 return SemaRef.PerformImplicitConversion(
4373 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4374 /*AllowExplicit=*/true);
4375 auto I = Captures.find(Capture);
4376 if (I != Captures.end())
4377 return buildCapture(SemaRef, Capture, I->second);
4378 DeclRefExpr *Ref = nullptr;
4379 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4380 Captures[Capture] = Ref;
4381 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004382}
4383
Alexander Musmana5f070a2014-10-01 06:03:56 +00004384/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004385Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4386 Scope *S, const bool LimitedType,
4387 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004388 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004389 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004390 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004391 SemaRef.getLangOpts().CPlusPlus) {
4392 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004393 auto *UBExpr = TestIsLessOp ? UB : LB;
4394 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004395 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4396 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004397 if (!Upper || !Lower)
4398 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004399
4400 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4401
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004402 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004403 // BuildBinOp already emitted error, this one is to point user to upper
4404 // and lower bound, and to tell what is passed to 'operator-'.
4405 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4406 << Upper->getSourceRange() << Lower->getSourceRange();
4407 return nullptr;
4408 }
4409 }
4410
4411 if (!Diff.isUsable())
4412 return nullptr;
4413
4414 // Upper - Lower [- 1]
4415 if (TestIsStrictOp)
4416 Diff = SemaRef.BuildBinOp(
4417 S, DefaultLoc, BO_Sub, Diff.get(),
4418 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4419 if (!Diff.isUsable())
4420 return nullptr;
4421
4422 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004423 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4424 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004425 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004426 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004427 if (!Diff.isUsable())
4428 return nullptr;
4429
4430 // Parentheses (for dumping/debugging purposes only).
4431 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4432 if (!Diff.isUsable())
4433 return nullptr;
4434
4435 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004436 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004437 if (!Diff.isUsable())
4438 return nullptr;
4439
Alexander Musman174b3ca2014-10-06 11:16:29 +00004440 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004441 QualType Type = Diff.get()->getType();
4442 auto &C = SemaRef.Context;
4443 bool UseVarType = VarType->hasIntegerRepresentation() &&
4444 C.getTypeSize(Type) > C.getTypeSize(VarType);
4445 if (!Type->isIntegerType() || UseVarType) {
4446 unsigned NewSize =
4447 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4448 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4449 : Type->hasSignedIntegerRepresentation();
4450 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004451 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4452 Diff = SemaRef.PerformImplicitConversion(
4453 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4454 if (!Diff.isUsable())
4455 return nullptr;
4456 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004457 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004458 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004459 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4460 if (NewSize != C.getTypeSize(Type)) {
4461 if (NewSize < C.getTypeSize(Type)) {
4462 assert(NewSize == 64 && "incorrect loop var size");
4463 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4464 << InitSrcRange << ConditionSrcRange;
4465 }
4466 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004467 NewSize, Type->hasSignedIntegerRepresentation() ||
4468 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004469 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4470 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4471 Sema::AA_Converting, true);
4472 if (!Diff.isUsable())
4473 return nullptr;
4474 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004475 }
4476 }
4477
Alexander Musmana5f070a2014-10-01 06:03:56 +00004478 return Diff.get();
4479}
4480
Alexey Bataev5a3af132016-03-29 08:58:54 +00004481Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4482 Scope *S, Expr *Cond,
4483 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004484 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4485 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4486 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004487
Alexey Bataev5a3af132016-03-29 08:58:54 +00004488 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4489 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4490 if (!NewLB.isUsable() || !NewUB.isUsable())
4491 return nullptr;
4492
Alexey Bataev62dbb972015-04-22 11:59:37 +00004493 auto CondExpr = SemaRef.BuildBinOp(
4494 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4495 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004496 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004497 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004498 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4499 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004500 CondExpr = SemaRef.PerformImplicitConversion(
4501 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4502 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004503 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004504 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4505 // Otherwise use original loop conditon and evaluate it in runtime.
4506 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4507}
4508
Alexander Musmana5f070a2014-10-01 06:03:56 +00004509/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004510DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004511 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004512 auto *VD = dyn_cast<VarDecl>(LCDecl);
4513 if (!VD) {
4514 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4515 auto *Ref = buildDeclRefExpr(
4516 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004517 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4518 // If the loop control decl is explicitly marked as private, do not mark it
4519 // as captured again.
4520 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4521 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004522 return Ref;
4523 }
4524 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004525 DefaultLoc);
4526}
4527
4528Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004529 if (LCDecl && !LCDecl->isInvalidDecl()) {
4530 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004531 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004532 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4533 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004534 if (PrivateVar->isInvalidDecl())
4535 return nullptr;
4536 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4537 }
4538 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004539}
4540
4541/// \brief Build initization of the counter be used for codegen.
4542Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4543
4544/// \brief Build step of the counter be used for codegen.
4545Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4546
4547/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004548struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004549 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004550 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004551 /// \brief This expression calculates the number of iterations in the loop.
4552 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004553 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004554 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004555 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004556 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004557 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004558 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004559 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004560 /// \brief This is step for the #CounterVar used to generate its update:
4561 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004562 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004563 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004564 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004565 /// \brief Source range of the loop init.
4566 SourceRange InitSrcRange;
4567 /// \brief Source range of the loop condition.
4568 SourceRange CondSrcRange;
4569 /// \brief Source range of the loop increment.
4570 SourceRange IncSrcRange;
4571};
4572
Alexey Bataev23b69422014-06-18 07:08:49 +00004573} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004574
Alexey Bataev9c821032015-04-30 04:23:23 +00004575void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4576 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4577 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004578 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4579 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004580 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4581 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004582 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4583 if (auto *D = ISC.GetLoopDecl()) {
4584 auto *VD = dyn_cast<VarDecl>(D);
4585 if (!VD) {
4586 if (auto *Private = IsOpenMPCapturedDecl(D))
4587 VD = Private;
4588 else {
4589 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4590 /*WithInit=*/false);
4591 VD = cast<VarDecl>(Ref->getDecl());
4592 }
4593 }
4594 DSAStack->addLoopControlVariable(D, VD);
4595 }
4596 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004597 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004598 }
4599}
4600
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004601/// \brief Called on a for stmt to check and extract its iteration space
4602/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004603static bool CheckOpenMPIterationSpace(
4604 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4605 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004606 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004607 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004608 LoopIterationSpace &ResultIterSpace,
4609 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004610 // OpenMP [2.6, Canonical Loop Form]
4611 // for (init-expr; test-expr; incr-expr) structured-block
4612 auto For = dyn_cast_or_null<ForStmt>(S);
4613 if (!For) {
4614 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004615 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4616 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4617 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4618 if (NestedLoopCount > 1) {
4619 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4620 SemaRef.Diag(DSA.getConstructLoc(),
4621 diag::note_omp_collapse_ordered_expr)
4622 << 2 << CollapseLoopCountExpr->getSourceRange()
4623 << OrderedLoopCountExpr->getSourceRange();
4624 else if (CollapseLoopCountExpr)
4625 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4626 diag::note_omp_collapse_ordered_expr)
4627 << 0 << CollapseLoopCountExpr->getSourceRange();
4628 else
4629 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4630 diag::note_omp_collapse_ordered_expr)
4631 << 1 << OrderedLoopCountExpr->getSourceRange();
4632 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004633 return true;
4634 }
4635 assert(For->getBody());
4636
4637 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4638
4639 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004640 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004641 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004642 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004643
4644 bool HasErrors = false;
4645
4646 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004647 if (auto *LCDecl = ISC.GetLoopDecl()) {
4648 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004649
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004650 // OpenMP [2.6, Canonical Loop Form]
4651 // Var is one of the following:
4652 // A variable of signed or unsigned integer type.
4653 // For C++, a variable of a random access iterator type.
4654 // For C, a variable of a pointer type.
4655 auto VarType = LCDecl->getType().getNonReferenceType();
4656 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4657 !VarType->isPointerType() &&
4658 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4659 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4660 << SemaRef.getLangOpts().CPlusPlus;
4661 HasErrors = true;
4662 }
4663
4664 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4665 // a Construct
4666 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4667 // parallel for construct is (are) private.
4668 // The loop iteration variable in the associated for-loop of a simd
4669 // construct with just one associated for-loop is linear with a
4670 // constant-linear-step that is the increment of the associated for-loop.
4671 // Exclude loop var from the list of variables with implicitly defined data
4672 // sharing attributes.
4673 VarsWithImplicitDSA.erase(LCDecl);
4674
4675 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4676 // in a Construct, C/C++].
4677 // The loop iteration variable in the associated for-loop of a simd
4678 // construct with just one associated for-loop may be listed in a linear
4679 // clause with a constant-linear-step that is the increment of the
4680 // associated for-loop.
4681 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4682 // parallel for construct may be listed in a private or lastprivate clause.
4683 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4684 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4685 // declared in the loop and it is predetermined as a private.
4686 auto PredeterminedCKind =
4687 isOpenMPSimdDirective(DKind)
4688 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4689 : OMPC_private;
4690 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4691 DVar.CKind != PredeterminedCKind) ||
4692 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4693 isOpenMPDistributeDirective(DKind)) &&
4694 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4695 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4696 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4697 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4698 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4699 << getOpenMPClauseName(PredeterminedCKind);
4700 if (DVar.RefExpr == nullptr)
4701 DVar.CKind = PredeterminedCKind;
4702 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4703 HasErrors = true;
4704 } else if (LoopDeclRefExpr != nullptr) {
4705 // Make the loop iteration variable private (for worksharing constructs),
4706 // linear (for simd directives with the only one associated loop) or
4707 // lastprivate (for simd directives with several collapsed or ordered
4708 // loops).
4709 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004710 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4711 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004712 /*FromParent=*/false);
4713 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4714 }
4715
4716 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4717
4718 // Check test-expr.
4719 HasErrors |= ISC.CheckCond(For->getCond());
4720
4721 // Check incr-expr.
4722 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004723 }
4724
Alexander Musmana5f070a2014-10-01 06:03:56 +00004725 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004726 return HasErrors;
4727
Alexander Musmana5f070a2014-10-01 06:03:56 +00004728 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004729 ResultIterSpace.PreCond =
4730 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004731 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004732 DSA.getCurScope(),
4733 (isOpenMPWorksharingDirective(DKind) ||
4734 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4735 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004736 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004737 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004738 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4739 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4740 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4741 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4742 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4743 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4744
Alexey Bataev62dbb972015-04-22 11:59:37 +00004745 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4746 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004747 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004748 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004749 ResultIterSpace.CounterInit == nullptr ||
4750 ResultIterSpace.CounterStep == nullptr);
4751
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004752 return HasErrors;
4753}
4754
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004755/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004756static ExprResult
4757BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4758 ExprResult Start,
4759 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004760 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004761 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4762 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004763 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004764 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004765 VarRef.get()->getType())) {
4766 NewStart = SemaRef.PerformImplicitConversion(
4767 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4768 /*AllowExplicit=*/true);
4769 if (!NewStart.isUsable())
4770 return ExprError();
4771 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004772
4773 auto Init =
4774 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4775 return Init;
4776}
4777
Alexander Musmana5f070a2014-10-01 06:03:56 +00004778/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004779static ExprResult
4780BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4781 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4782 ExprResult Step, bool Subtract,
4783 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004784 // Add parentheses (for debugging purposes only).
4785 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4786 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4787 !Step.isUsable())
4788 return ExprError();
4789
Alexey Bataev5a3af132016-03-29 08:58:54 +00004790 ExprResult NewStep = Step;
4791 if (Captures)
4792 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004793 if (NewStep.isInvalid())
4794 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004795 ExprResult Update =
4796 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004797 if (!Update.isUsable())
4798 return ExprError();
4799
Alexey Bataevc0214e02016-02-16 12:13:49 +00004800 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4801 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004802 ExprResult NewStart = Start;
4803 if (Captures)
4804 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004805 if (NewStart.isInvalid())
4806 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004807
Alexey Bataevc0214e02016-02-16 12:13:49 +00004808 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4809 ExprResult SavedUpdate = Update;
4810 ExprResult UpdateVal;
4811 if (VarRef.get()->getType()->isOverloadableType() ||
4812 NewStart.get()->getType()->isOverloadableType() ||
4813 Update.get()->getType()->isOverloadableType()) {
4814 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4815 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4816 Update =
4817 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4818 if (Update.isUsable()) {
4819 UpdateVal =
4820 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4821 VarRef.get(), SavedUpdate.get());
4822 if (UpdateVal.isUsable()) {
4823 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4824 UpdateVal.get());
4825 }
4826 }
4827 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4828 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004829
Alexey Bataevc0214e02016-02-16 12:13:49 +00004830 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4831 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4832 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4833 NewStart.get(), SavedUpdate.get());
4834 if (!Update.isUsable())
4835 return ExprError();
4836
Alexey Bataev11481f52016-02-17 10:29:05 +00004837 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4838 VarRef.get()->getType())) {
4839 Update = SemaRef.PerformImplicitConversion(
4840 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4841 if (!Update.isUsable())
4842 return ExprError();
4843 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004844
4845 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4846 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004847 return Update;
4848}
4849
4850/// \brief Convert integer expression \a E to make it have at least \a Bits
4851/// bits.
4852static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4853 Sema &SemaRef) {
4854 if (E == nullptr)
4855 return ExprError();
4856 auto &C = SemaRef.Context;
4857 QualType OldType = E->getType();
4858 unsigned HasBits = C.getTypeSize(OldType);
4859 if (HasBits >= Bits)
4860 return ExprResult(E);
4861 // OK to convert to signed, because new type has more bits than old.
4862 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4863 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4864 true);
4865}
4866
4867/// \brief Check if the given expression \a E is a constant integer that fits
4868/// into \a Bits bits.
4869static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4870 if (E == nullptr)
4871 return false;
4872 llvm::APSInt Result;
4873 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4874 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4875 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004876}
4877
Alexey Bataev5a3af132016-03-29 08:58:54 +00004878/// Build preinits statement for the given declarations.
4879static Stmt *buildPreInits(ASTContext &Context,
4880 SmallVectorImpl<Decl *> &PreInits) {
4881 if (!PreInits.empty()) {
4882 return new (Context) DeclStmt(
4883 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4884 SourceLocation(), SourceLocation());
4885 }
4886 return nullptr;
4887}
4888
4889/// Build preinits statement for the given declarations.
4890static Stmt *buildPreInits(ASTContext &Context,
4891 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4892 if (!Captures.empty()) {
4893 SmallVector<Decl *, 16> PreInits;
4894 for (auto &Pair : Captures)
4895 PreInits.push_back(Pair.second->getDecl());
4896 return buildPreInits(Context, PreInits);
4897 }
4898 return nullptr;
4899}
4900
4901/// Build postupdate expression for the given list of postupdates expressions.
4902static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4903 Expr *PostUpdate = nullptr;
4904 if (!PostUpdates.empty()) {
4905 for (auto *E : PostUpdates) {
4906 Expr *ConvE = S.BuildCStyleCastExpr(
4907 E->getExprLoc(),
4908 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4909 E->getExprLoc(), E)
4910 .get();
4911 PostUpdate = PostUpdate
4912 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4913 PostUpdate, ConvE)
4914 .get()
4915 : ConvE;
4916 }
4917 }
4918 return PostUpdate;
4919}
4920
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004921/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004922/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4923/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004924static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004925CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4926 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4927 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004928 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004929 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004930 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004931 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004932 // Found 'collapse' clause - calculate collapse number.
4933 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004934 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004935 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004936 }
4937 if (OrderedLoopCountExpr) {
4938 // Found 'ordered' clause - calculate collapse number.
4939 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004940 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4941 if (Result.getLimitedValue() < NestedLoopCount) {
4942 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4943 diag::err_omp_wrong_ordered_loop_count)
4944 << OrderedLoopCountExpr->getSourceRange();
4945 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4946 diag::note_collapse_loop_count)
4947 << CollapseLoopCountExpr->getSourceRange();
4948 }
4949 NestedLoopCount = Result.getLimitedValue();
4950 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004951 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004952 // This is helper routine for loop directives (e.g., 'for', 'simd',
4953 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004954 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004955 SmallVector<LoopIterationSpace, 4> IterSpaces;
4956 IterSpaces.resize(NestedLoopCount);
4957 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004958 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004959 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004960 NestedLoopCount, CollapseLoopCountExpr,
4961 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004962 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004963 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004964 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004965 // OpenMP [2.8.1, simd construct, Restrictions]
4966 // All loops associated with the construct must be perfectly nested; that
4967 // is, there must be no intervening code nor any OpenMP directive between
4968 // any two loops.
4969 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004970 }
4971
Alexander Musmana5f070a2014-10-01 06:03:56 +00004972 Built.clear(/* size */ NestedLoopCount);
4973
4974 if (SemaRef.CurContext->isDependentContext())
4975 return NestedLoopCount;
4976
4977 // An example of what is generated for the following code:
4978 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004979 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004980 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004981 // for (k = 0; k < NK; ++k)
4982 // for (j = J0; j < NJ; j+=2) {
4983 // <loop body>
4984 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004985 //
4986 // We generate the code below.
4987 // Note: the loop body may be outlined in CodeGen.
4988 // Note: some counters may be C++ classes, operator- is used to find number of
4989 // iterations and operator+= to calculate counter value.
4990 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4991 // or i64 is currently supported).
4992 //
4993 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4994 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4995 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4996 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4997 // // similar updates for vars in clauses (e.g. 'linear')
4998 // <loop body (using local i and j)>
4999 // }
5000 // i = NI; // assign final values of counters
5001 // j = NJ;
5002 //
5003
5004 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5005 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005006 // Precondition tests if there is at least one iteration (all conditions are
5007 // true).
5008 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005009 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005010 ExprResult LastIteration32 = WidenIterationCount(
5011 32 /* Bits */, SemaRef.PerformImplicitConversion(
5012 N0->IgnoreImpCasts(), N0->getType(),
5013 Sema::AA_Converting, /*AllowExplicit=*/true)
5014 .get(),
5015 SemaRef);
5016 ExprResult LastIteration64 = WidenIterationCount(
5017 64 /* Bits */, SemaRef.PerformImplicitConversion(
5018 N0->IgnoreImpCasts(), N0->getType(),
5019 Sema::AA_Converting, /*AllowExplicit=*/true)
5020 .get(),
5021 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005022
5023 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5024 return NestedLoopCount;
5025
5026 auto &C = SemaRef.Context;
5027 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5028
5029 Scope *CurScope = DSA.getCurScope();
5030 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005031 if (PreCond.isUsable()) {
5032 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5033 PreCond.get(), IterSpaces[Cnt].PreCond);
5034 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005035 auto N = IterSpaces[Cnt].NumIterations;
5036 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5037 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005038 LastIteration32 = SemaRef.BuildBinOp(
5039 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5040 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5041 Sema::AA_Converting,
5042 /*AllowExplicit=*/true)
5043 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005044 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005045 LastIteration64 = SemaRef.BuildBinOp(
5046 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5047 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5048 Sema::AA_Converting,
5049 /*AllowExplicit=*/true)
5050 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005051 }
5052
5053 // Choose either the 32-bit or 64-bit version.
5054 ExprResult LastIteration = LastIteration64;
5055 if (LastIteration32.isUsable() &&
5056 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5057 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5058 FitsInto(
5059 32 /* Bits */,
5060 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5061 LastIteration64.get(), SemaRef)))
5062 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005063 QualType VType = LastIteration.get()->getType();
5064 QualType RealVType = VType;
5065 QualType StrideVType = VType;
5066 if (isOpenMPTaskLoopDirective(DKind)) {
5067 VType =
5068 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5069 StrideVType =
5070 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5071 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005072
5073 if (!LastIteration.isUsable())
5074 return 0;
5075
5076 // Save the number of iterations.
5077 ExprResult NumIterations = LastIteration;
5078 {
5079 LastIteration = SemaRef.BuildBinOp(
5080 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5081 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5082 if (!LastIteration.isUsable())
5083 return 0;
5084 }
5085
5086 // Calculate the last iteration number beforehand instead of doing this on
5087 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5088 llvm::APSInt Result;
5089 bool IsConstant =
5090 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5091 ExprResult CalcLastIteration;
5092 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005093 ExprResult SaveRef =
5094 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005095 LastIteration = SaveRef;
5096
5097 // Prepare SaveRef + 1.
5098 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005099 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005100 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5101 if (!NumIterations.isUsable())
5102 return 0;
5103 }
5104
5105 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5106
Alexander Musmanc6388682014-12-15 07:07:06 +00005107 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005108 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005109 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5110 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005111 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005112 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5113 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005114 SemaRef.AddInitializerToDecl(
5115 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5116 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5117
5118 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005119 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5120 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005121 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5122 /*DirectInit*/ false,
5123 /*TypeMayContainAuto*/ false);
5124
5125 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5126 // This will be used to implement clause 'lastprivate'.
5127 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005128 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5129 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005130 SemaRef.AddInitializerToDecl(
5131 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5132 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5133
5134 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005135 VarDecl *STDecl =
5136 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5137 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005138 SemaRef.AddInitializerToDecl(
5139 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5140 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5141
5142 // Build expression: UB = min(UB, LastIteration)
5143 // It is nesessary for CodeGen of directives with static scheduling.
5144 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5145 UB.get(), LastIteration.get());
5146 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5147 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5148 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5149 CondOp.get());
5150 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005151
5152 // If we have a combined directive that combines 'distribute', 'for' or
5153 // 'simd' we need to be able to access the bounds of the schedule of the
5154 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5155 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5156 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5157 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5158
5159 // We expect to have at least 2 more parameters than the 'parallel'
5160 // directive does - the lower and upper bounds of the previous schedule.
5161 assert(CD->getNumParams() >= 4 &&
5162 "Unexpected number of parameters in loop combined directive");
5163
5164 // Set the proper type for the bounds given what we learned from the
5165 // enclosed loops.
5166 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5167 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5168
5169 // Previous lower and upper bounds are obtained from the region
5170 // parameters.
5171 PrevLB =
5172 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5173 PrevUB =
5174 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5175 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005176 }
5177
5178 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005179 ExprResult IV;
5180 ExprResult Init;
5181 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005182 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5183 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005184 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005185 isOpenMPTaskLoopDirective(DKind) ||
5186 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005187 ? LB.get()
5188 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5189 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5190 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005191 }
5192
Alexander Musmanc6388682014-12-15 07:07:06 +00005193 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005194 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005195 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005196 (isOpenMPWorksharingDirective(DKind) ||
5197 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005198 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5199 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5200 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005201
5202 // Loop increment (IV = IV + 1)
5203 SourceLocation IncLoc;
5204 ExprResult Inc =
5205 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5206 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5207 if (!Inc.isUsable())
5208 return 0;
5209 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005210 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5211 if (!Inc.isUsable())
5212 return 0;
5213
5214 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5215 // Used for directives with static scheduling.
5216 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005217 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5218 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005219 // LB + ST
5220 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5221 if (!NextLB.isUsable())
5222 return 0;
5223 // LB = LB + ST
5224 NextLB =
5225 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5226 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5227 if (!NextLB.isUsable())
5228 return 0;
5229 // UB + ST
5230 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5231 if (!NextUB.isUsable())
5232 return 0;
5233 // UB = UB + ST
5234 NextUB =
5235 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5236 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5237 if (!NextUB.isUsable())
5238 return 0;
5239 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005240
5241 // Build updates and final values of the loop counters.
5242 bool HasErrors = false;
5243 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005244 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005245 Built.Updates.resize(NestedLoopCount);
5246 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005247 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005248 {
5249 ExprResult Div;
5250 // Go from inner nested loop to outer.
5251 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5252 LoopIterationSpace &IS = IterSpaces[Cnt];
5253 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5254 // Build: Iter = (IV / Div) % IS.NumIters
5255 // where Div is product of previous iterations' IS.NumIters.
5256 ExprResult Iter;
5257 if (Div.isUsable()) {
5258 Iter =
5259 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5260 } else {
5261 Iter = IV;
5262 assert((Cnt == (int)NestedLoopCount - 1) &&
5263 "unusable div expected on first iteration only");
5264 }
5265
5266 if (Cnt != 0 && Iter.isUsable())
5267 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5268 IS.NumIterations);
5269 if (!Iter.isUsable()) {
5270 HasErrors = true;
5271 break;
5272 }
5273
Alexey Bataev39f915b82015-05-08 10:41:21 +00005274 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005275 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5276 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5277 IS.CounterVar->getExprLoc(),
5278 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005279 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005280 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005281 if (!Init.isUsable()) {
5282 HasErrors = true;
5283 break;
5284 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005285 ExprResult Update = BuildCounterUpdate(
5286 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5287 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005288 if (!Update.isUsable()) {
5289 HasErrors = true;
5290 break;
5291 }
5292
5293 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5294 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005295 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005296 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005297 if (!Final.isUsable()) {
5298 HasErrors = true;
5299 break;
5300 }
5301
5302 // Build Div for the next iteration: Div <- Div * IS.NumIters
5303 if (Cnt != 0) {
5304 if (Div.isUnset())
5305 Div = IS.NumIterations;
5306 else
5307 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5308 IS.NumIterations);
5309
5310 // Add parentheses (for debugging purposes only).
5311 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005312 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005313 if (!Div.isUsable()) {
5314 HasErrors = true;
5315 break;
5316 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005317 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005318 }
5319 if (!Update.isUsable() || !Final.isUsable()) {
5320 HasErrors = true;
5321 break;
5322 }
5323 // Save results
5324 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005325 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005326 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005327 Built.Updates[Cnt] = Update.get();
5328 Built.Finals[Cnt] = Final.get();
5329 }
5330 }
5331
5332 if (HasErrors)
5333 return 0;
5334
5335 // Save results
5336 Built.IterationVarRef = IV.get();
5337 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005338 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005339 Built.CalcLastIteration =
5340 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005341 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005342 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005343 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005344 Built.Init = Init.get();
5345 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005346 Built.LB = LB.get();
5347 Built.UB = UB.get();
5348 Built.IL = IL.get();
5349 Built.ST = ST.get();
5350 Built.EUB = EUB.get();
5351 Built.NLB = NextLB.get();
5352 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005353 Built.PrevLB = PrevLB.get();
5354 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005355
Alexey Bataev8b427062016-05-25 12:36:08 +00005356 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5357 // Fill data for doacross depend clauses.
5358 for (auto Pair : DSA.getDoacrossDependClauses()) {
5359 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5360 Pair.first->setCounterValue(CounterVal);
5361 else {
5362 if (NestedLoopCount != Pair.second.size() ||
5363 NestedLoopCount != LoopMultipliers.size() + 1) {
5364 // Erroneous case - clause has some problems.
5365 Pair.first->setCounterValue(CounterVal);
5366 continue;
5367 }
5368 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5369 auto I = Pair.second.rbegin();
5370 auto IS = IterSpaces.rbegin();
5371 auto ILM = LoopMultipliers.rbegin();
5372 Expr *UpCounterVal = CounterVal;
5373 Expr *Multiplier = nullptr;
5374 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5375 if (I->first) {
5376 assert(IS->CounterStep);
5377 Expr *NormalizedOffset =
5378 SemaRef
5379 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5380 I->first, IS->CounterStep)
5381 .get();
5382 if (Multiplier) {
5383 NormalizedOffset =
5384 SemaRef
5385 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5386 NormalizedOffset, Multiplier)
5387 .get();
5388 }
5389 assert(I->second == OO_Plus || I->second == OO_Minus);
5390 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5391 UpCounterVal =
5392 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5393 UpCounterVal, NormalizedOffset).get();
5394 }
5395 Multiplier = *ILM;
5396 ++I;
5397 ++IS;
5398 ++ILM;
5399 }
5400 Pair.first->setCounterValue(UpCounterVal);
5401 }
5402 }
5403
Alexey Bataevabfc0692014-06-25 06:52:00 +00005404 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005405}
5406
Alexey Bataev10e775f2015-07-30 11:36:16 +00005407static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005408 auto CollapseClauses =
5409 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5410 if (CollapseClauses.begin() != CollapseClauses.end())
5411 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005412 return nullptr;
5413}
5414
Alexey Bataev10e775f2015-07-30 11:36:16 +00005415static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005416 auto OrderedClauses =
5417 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5418 if (OrderedClauses.begin() != OrderedClauses.end())
5419 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005420 return nullptr;
5421}
5422
Kelvin Lic5609492016-07-15 04:39:07 +00005423static bool checkSimdlenSafelenSpecified(Sema &S,
5424 const ArrayRef<OMPClause *> Clauses) {
5425 OMPSafelenClause *Safelen = nullptr;
5426 OMPSimdlenClause *Simdlen = nullptr;
5427
5428 for (auto *Clause : Clauses) {
5429 if (Clause->getClauseKind() == OMPC_safelen)
5430 Safelen = cast<OMPSafelenClause>(Clause);
5431 else if (Clause->getClauseKind() == OMPC_simdlen)
5432 Simdlen = cast<OMPSimdlenClause>(Clause);
5433 if (Safelen && Simdlen)
5434 break;
5435 }
5436
5437 if (Simdlen && Safelen) {
5438 llvm::APSInt SimdlenRes, SafelenRes;
5439 auto SimdlenLength = Simdlen->getSimdlen();
5440 auto SafelenLength = Safelen->getSafelen();
5441 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5442 SimdlenLength->isInstantiationDependent() ||
5443 SimdlenLength->containsUnexpandedParameterPack())
5444 return false;
5445 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5446 SafelenLength->isInstantiationDependent() ||
5447 SafelenLength->containsUnexpandedParameterPack())
5448 return false;
5449 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5450 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5451 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5452 // If both simdlen and safelen clauses are specified, the value of the
5453 // simdlen parameter must be less than or equal to the value of the safelen
5454 // parameter.
5455 if (SimdlenRes > SafelenRes) {
5456 S.Diag(SimdlenLength->getExprLoc(),
5457 diag::err_omp_wrong_simdlen_safelen_values)
5458 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5459 return true;
5460 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005461 }
5462 return false;
5463}
5464
Alexey Bataev4acb8592014-07-07 13:01:15 +00005465StmtResult Sema::ActOnOpenMPSimdDirective(
5466 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5467 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005468 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005469 if (!AStmt)
5470 return StmtError();
5471
5472 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005473 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005474 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5475 // define the nested loops number.
5476 unsigned NestedLoopCount = CheckOpenMPLoop(
5477 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5478 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005479 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005480 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005481
Alexander Musmana5f070a2014-10-01 06:03:56 +00005482 assert((CurContext->isDependentContext() || B.builtAll()) &&
5483 "omp simd loop exprs were not built");
5484
Alexander Musman3276a272015-03-21 10:12:56 +00005485 if (!CurContext->isDependentContext()) {
5486 // Finalize the clauses that need pre-built expressions for CodeGen.
5487 for (auto C : Clauses) {
5488 if (auto LC = dyn_cast<OMPLinearClause>(C))
5489 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005490 B.NumIterations, *this, CurScope,
5491 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005492 return StmtError();
5493 }
5494 }
5495
Kelvin Lic5609492016-07-15 04:39:07 +00005496 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005497 return StmtError();
5498
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005499 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005500 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5501 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005502}
5503
Alexey Bataev4acb8592014-07-07 13:01:15 +00005504StmtResult Sema::ActOnOpenMPForDirective(
5505 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5506 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005507 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005508 if (!AStmt)
5509 return StmtError();
5510
5511 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005512 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005513 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5514 // define the nested loops number.
5515 unsigned NestedLoopCount = CheckOpenMPLoop(
5516 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5517 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005518 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005519 return StmtError();
5520
Alexander Musmana5f070a2014-10-01 06:03:56 +00005521 assert((CurContext->isDependentContext() || B.builtAll()) &&
5522 "omp for loop exprs were not built");
5523
Alexey Bataev54acd402015-08-04 11:18:19 +00005524 if (!CurContext->isDependentContext()) {
5525 // Finalize the clauses that need pre-built expressions for CodeGen.
5526 for (auto C : Clauses) {
5527 if (auto LC = dyn_cast<OMPLinearClause>(C))
5528 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005529 B.NumIterations, *this, CurScope,
5530 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005531 return StmtError();
5532 }
5533 }
5534
Alexey Bataevf29276e2014-06-18 04:14:57 +00005535 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005536 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005537 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005538}
5539
Alexander Musmanf82886e2014-09-18 05:12:34 +00005540StmtResult Sema::ActOnOpenMPForSimdDirective(
5541 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5542 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005543 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005544 if (!AStmt)
5545 return StmtError();
5546
5547 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005548 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005549 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5550 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005551 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005552 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5553 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5554 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005555 if (NestedLoopCount == 0)
5556 return StmtError();
5557
Alexander Musmanc6388682014-12-15 07:07:06 +00005558 assert((CurContext->isDependentContext() || B.builtAll()) &&
5559 "omp for simd loop exprs were not built");
5560
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005561 if (!CurContext->isDependentContext()) {
5562 // Finalize the clauses that need pre-built expressions for CodeGen.
5563 for (auto C : Clauses) {
5564 if (auto LC = dyn_cast<OMPLinearClause>(C))
5565 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005566 B.NumIterations, *this, CurScope,
5567 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005568 return StmtError();
5569 }
5570 }
5571
Kelvin Lic5609492016-07-15 04:39:07 +00005572 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005573 return StmtError();
5574
Alexander Musmanf82886e2014-09-18 05:12:34 +00005575 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005576 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5577 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005578}
5579
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005580StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5581 Stmt *AStmt,
5582 SourceLocation StartLoc,
5583 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005584 if (!AStmt)
5585 return StmtError();
5586
5587 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005588 auto BaseStmt = AStmt;
5589 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5590 BaseStmt = CS->getCapturedStmt();
5591 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5592 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005593 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005594 return StmtError();
5595 // All associated statements must be '#pragma omp section' except for
5596 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005597 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005598 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5599 if (SectionStmt)
5600 Diag(SectionStmt->getLocStart(),
5601 diag::err_omp_sections_substmt_not_section);
5602 return StmtError();
5603 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005604 cast<OMPSectionDirective>(SectionStmt)
5605 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005606 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005607 } else {
5608 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5609 return StmtError();
5610 }
5611
5612 getCurFunction()->setHasBranchProtectedScope();
5613
Alexey Bataev25e5b442015-09-15 12:52:43 +00005614 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5615 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005616}
5617
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005618StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5619 SourceLocation StartLoc,
5620 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005621 if (!AStmt)
5622 return StmtError();
5623
5624 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005625
5626 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005627 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005628
Alexey Bataev25e5b442015-09-15 12:52:43 +00005629 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5630 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005631}
5632
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005633StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5634 Stmt *AStmt,
5635 SourceLocation StartLoc,
5636 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005637 if (!AStmt)
5638 return StmtError();
5639
5640 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005641
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005642 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005643
Alexey Bataev3255bf32015-01-19 05:20:46 +00005644 // OpenMP [2.7.3, single Construct, Restrictions]
5645 // The copyprivate clause must not be used with the nowait clause.
5646 OMPClause *Nowait = nullptr;
5647 OMPClause *Copyprivate = nullptr;
5648 for (auto *Clause : Clauses) {
5649 if (Clause->getClauseKind() == OMPC_nowait)
5650 Nowait = Clause;
5651 else if (Clause->getClauseKind() == OMPC_copyprivate)
5652 Copyprivate = Clause;
5653 if (Copyprivate && Nowait) {
5654 Diag(Copyprivate->getLocStart(),
5655 diag::err_omp_single_copyprivate_with_nowait);
5656 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5657 return StmtError();
5658 }
5659 }
5660
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005661 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5662}
5663
Alexander Musman80c22892014-07-17 08:54:58 +00005664StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5665 SourceLocation StartLoc,
5666 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005667 if (!AStmt)
5668 return StmtError();
5669
5670 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005671
5672 getCurFunction()->setHasBranchProtectedScope();
5673
5674 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5675}
5676
Alexey Bataev28c75412015-12-15 08:19:24 +00005677StmtResult Sema::ActOnOpenMPCriticalDirective(
5678 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5679 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005680 if (!AStmt)
5681 return StmtError();
5682
5683 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005684
Alexey Bataev28c75412015-12-15 08:19:24 +00005685 bool ErrorFound = false;
5686 llvm::APSInt Hint;
5687 SourceLocation HintLoc;
5688 bool DependentHint = false;
5689 for (auto *C : Clauses) {
5690 if (C->getClauseKind() == OMPC_hint) {
5691 if (!DirName.getName()) {
5692 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5693 ErrorFound = true;
5694 }
5695 Expr *E = cast<OMPHintClause>(C)->getHint();
5696 if (E->isTypeDependent() || E->isValueDependent() ||
5697 E->isInstantiationDependent())
5698 DependentHint = true;
5699 else {
5700 Hint = E->EvaluateKnownConstInt(Context);
5701 HintLoc = C->getLocStart();
5702 }
5703 }
5704 }
5705 if (ErrorFound)
5706 return StmtError();
5707 auto Pair = DSAStack->getCriticalWithHint(DirName);
5708 if (Pair.first && DirName.getName() && !DependentHint) {
5709 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5710 Diag(StartLoc, diag::err_omp_critical_with_hint);
5711 if (HintLoc.isValid()) {
5712 Diag(HintLoc, diag::note_omp_critical_hint_here)
5713 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5714 } else
5715 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5716 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5717 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5718 << 1
5719 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5720 /*Radix=*/10, /*Signed=*/false);
5721 } else
5722 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5723 }
5724 }
5725
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005726 getCurFunction()->setHasBranchProtectedScope();
5727
Alexey Bataev28c75412015-12-15 08:19:24 +00005728 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5729 Clauses, AStmt);
5730 if (!Pair.first && DirName.getName() && !DependentHint)
5731 DSAStack->addCriticalWithHint(Dir, Hint);
5732 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005733}
5734
Alexey Bataev4acb8592014-07-07 13:01:15 +00005735StmtResult Sema::ActOnOpenMPParallelForDirective(
5736 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5737 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005738 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005739 if (!AStmt)
5740 return StmtError();
5741
Alexey Bataev4acb8592014-07-07 13:01:15 +00005742 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5743 // 1.2.2 OpenMP Language Terminology
5744 // Structured block - An executable statement with a single entry at the
5745 // top and a single exit at the bottom.
5746 // The point of exit cannot be a branch out of the structured block.
5747 // longjmp() and throw() must not violate the entry/exit criteria.
5748 CS->getCapturedDecl()->setNothrow();
5749
Alexander Musmanc6388682014-12-15 07:07:06 +00005750 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005751 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5752 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005753 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005754 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5755 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5756 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005757 if (NestedLoopCount == 0)
5758 return StmtError();
5759
Alexander Musmana5f070a2014-10-01 06:03:56 +00005760 assert((CurContext->isDependentContext() || B.builtAll()) &&
5761 "omp parallel for loop exprs were not built");
5762
Alexey Bataev54acd402015-08-04 11:18:19 +00005763 if (!CurContext->isDependentContext()) {
5764 // Finalize the clauses that need pre-built expressions for CodeGen.
5765 for (auto C : Clauses) {
5766 if (auto LC = dyn_cast<OMPLinearClause>(C))
5767 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005768 B.NumIterations, *this, CurScope,
5769 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005770 return StmtError();
5771 }
5772 }
5773
Alexey Bataev4acb8592014-07-07 13:01:15 +00005774 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005775 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005776 NestedLoopCount, Clauses, AStmt, B,
5777 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005778}
5779
Alexander Musmane4e893b2014-09-23 09:33:00 +00005780StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5781 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5782 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005783 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005784 if (!AStmt)
5785 return StmtError();
5786
Alexander Musmane4e893b2014-09-23 09:33:00 +00005787 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5788 // 1.2.2 OpenMP Language Terminology
5789 // Structured block - An executable statement with a single entry at the
5790 // top and a single exit at the bottom.
5791 // The point of exit cannot be a branch out of the structured block.
5792 // longjmp() and throw() must not violate the entry/exit criteria.
5793 CS->getCapturedDecl()->setNothrow();
5794
Alexander Musmanc6388682014-12-15 07:07:06 +00005795 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005796 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5797 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005798 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005799 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5800 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5801 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005802 if (NestedLoopCount == 0)
5803 return StmtError();
5804
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005805 if (!CurContext->isDependentContext()) {
5806 // Finalize the clauses that need pre-built expressions for CodeGen.
5807 for (auto C : Clauses) {
5808 if (auto LC = dyn_cast<OMPLinearClause>(C))
5809 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005810 B.NumIterations, *this, CurScope,
5811 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005812 return StmtError();
5813 }
5814 }
5815
Kelvin Lic5609492016-07-15 04:39:07 +00005816 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005817 return StmtError();
5818
Alexander Musmane4e893b2014-09-23 09:33:00 +00005819 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005820 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005821 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005822}
5823
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005824StmtResult
5825Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5826 Stmt *AStmt, SourceLocation StartLoc,
5827 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005828 if (!AStmt)
5829 return StmtError();
5830
5831 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005832 auto BaseStmt = AStmt;
5833 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5834 BaseStmt = CS->getCapturedStmt();
5835 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5836 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005837 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005838 return StmtError();
5839 // All associated statements must be '#pragma omp section' except for
5840 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005841 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005842 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5843 if (SectionStmt)
5844 Diag(SectionStmt->getLocStart(),
5845 diag::err_omp_parallel_sections_substmt_not_section);
5846 return StmtError();
5847 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005848 cast<OMPSectionDirective>(SectionStmt)
5849 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005850 }
5851 } else {
5852 Diag(AStmt->getLocStart(),
5853 diag::err_omp_parallel_sections_not_compound_stmt);
5854 return StmtError();
5855 }
5856
5857 getCurFunction()->setHasBranchProtectedScope();
5858
Alexey Bataev25e5b442015-09-15 12:52:43 +00005859 return OMPParallelSectionsDirective::Create(
5860 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005861}
5862
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005863StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5864 Stmt *AStmt, SourceLocation StartLoc,
5865 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005866 if (!AStmt)
5867 return StmtError();
5868
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005869 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5870 // 1.2.2 OpenMP Language Terminology
5871 // Structured block - An executable statement with a single entry at the
5872 // top and a single exit at the bottom.
5873 // The point of exit cannot be a branch out of the structured block.
5874 // longjmp() and throw() must not violate the entry/exit criteria.
5875 CS->getCapturedDecl()->setNothrow();
5876
5877 getCurFunction()->setHasBranchProtectedScope();
5878
Alexey Bataev25e5b442015-09-15 12:52:43 +00005879 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5880 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005881}
5882
Alexey Bataev68446b72014-07-18 07:47:19 +00005883StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5884 SourceLocation EndLoc) {
5885 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5886}
5887
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005888StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5889 SourceLocation EndLoc) {
5890 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5891}
5892
Alexey Bataev2df347a2014-07-18 10:17:07 +00005893StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5894 SourceLocation EndLoc) {
5895 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5896}
5897
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005898StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5899 SourceLocation StartLoc,
5900 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005901 if (!AStmt)
5902 return StmtError();
5903
5904 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005905
5906 getCurFunction()->setHasBranchProtectedScope();
5907
5908 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5909}
5910
Alexey Bataev6125da92014-07-21 11:26:11 +00005911StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5912 SourceLocation StartLoc,
5913 SourceLocation EndLoc) {
5914 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5915 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5916}
5917
Alexey Bataev346265e2015-09-25 10:37:12 +00005918StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5919 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005920 SourceLocation StartLoc,
5921 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005922 OMPClause *DependFound = nullptr;
5923 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005924 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005925 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005926 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005927 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005928 for (auto *C : Clauses) {
5929 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5930 DependFound = C;
5931 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5932 if (DependSourceClause) {
5933 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5934 << getOpenMPDirectiveName(OMPD_ordered)
5935 << getOpenMPClauseName(OMPC_depend) << 2;
5936 ErrorFound = true;
5937 } else
5938 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005939 if (DependSinkClause) {
5940 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5941 << 0;
5942 ErrorFound = true;
5943 }
5944 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5945 if (DependSourceClause) {
5946 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5947 << 1;
5948 ErrorFound = true;
5949 }
5950 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005951 }
5952 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005953 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005954 else if (C->getClauseKind() == OMPC_simd)
5955 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005956 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005957 if (!ErrorFound && !SC &&
5958 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005959 // OpenMP [2.8.1,simd Construct, Restrictions]
5960 // An ordered construct with the simd clause is the only OpenMP construct
5961 // that can appear in the simd region.
5962 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005963 ErrorFound = true;
5964 } else if (DependFound && (TC || SC)) {
5965 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5966 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5967 ErrorFound = true;
5968 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5969 Diag(DependFound->getLocStart(),
5970 diag::err_omp_ordered_directive_without_param);
5971 ErrorFound = true;
5972 } else if (TC || Clauses.empty()) {
5973 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5974 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5975 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5976 << (TC != nullptr);
5977 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5978 ErrorFound = true;
5979 }
5980 }
5981 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005982 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005983
5984 if (AStmt) {
5985 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5986
5987 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005988 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005989
5990 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005991}
5992
Alexey Bataev1d160b12015-03-13 12:27:31 +00005993namespace {
5994/// \brief Helper class for checking expression in 'omp atomic [update]'
5995/// construct.
5996class OpenMPAtomicUpdateChecker {
5997 /// \brief Error results for atomic update expressions.
5998 enum ExprAnalysisErrorCode {
5999 /// \brief A statement is not an expression statement.
6000 NotAnExpression,
6001 /// \brief Expression is not builtin binary or unary operation.
6002 NotABinaryOrUnaryExpression,
6003 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6004 NotAnUnaryIncDecExpression,
6005 /// \brief An expression is not of scalar type.
6006 NotAScalarType,
6007 /// \brief A binary operation is not an assignment operation.
6008 NotAnAssignmentOp,
6009 /// \brief RHS part of the binary operation is not a binary expression.
6010 NotABinaryExpression,
6011 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6012 /// expression.
6013 NotABinaryOperator,
6014 /// \brief RHS binary operation does not have reference to the updated LHS
6015 /// part.
6016 NotAnUpdateExpression,
6017 /// \brief No errors is found.
6018 NoError
6019 };
6020 /// \brief Reference to Sema.
6021 Sema &SemaRef;
6022 /// \brief A location for note diagnostics (when error is found).
6023 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006024 /// \brief 'x' lvalue part of the source atomic expression.
6025 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006026 /// \brief 'expr' rvalue part of the source atomic expression.
6027 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006028 /// \brief Helper expression of the form
6029 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6030 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6031 Expr *UpdateExpr;
6032 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6033 /// important for non-associative operations.
6034 bool IsXLHSInRHSPart;
6035 BinaryOperatorKind Op;
6036 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006037 /// \brief true if the source expression is a postfix unary operation, false
6038 /// if it is a prefix unary operation.
6039 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006040
6041public:
6042 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006043 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006044 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006045 /// \brief Check specified statement that it is suitable for 'atomic update'
6046 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006047 /// expression. If DiagId and NoteId == 0, then only check is performed
6048 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006049 /// \param DiagId Diagnostic which should be emitted if error is found.
6050 /// \param NoteId Diagnostic note for the main error message.
6051 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006052 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006053 /// \brief Return the 'x' lvalue part of the source atomic expression.
6054 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006055 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6056 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006057 /// \brief Return the update expression used in calculation of the updated
6058 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6059 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6060 Expr *getUpdateExpr() const { return UpdateExpr; }
6061 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6062 /// false otherwise.
6063 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6064
Alexey Bataevb78ca832015-04-01 03:33:17 +00006065 /// \brief true if the source expression is a postfix unary operation, false
6066 /// if it is a prefix unary operation.
6067 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6068
Alexey Bataev1d160b12015-03-13 12:27:31 +00006069private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006070 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6071 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006072};
6073} // namespace
6074
6075bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6076 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6077 ExprAnalysisErrorCode ErrorFound = NoError;
6078 SourceLocation ErrorLoc, NoteLoc;
6079 SourceRange ErrorRange, NoteRange;
6080 // Allowed constructs are:
6081 // x = x binop expr;
6082 // x = expr binop x;
6083 if (AtomicBinOp->getOpcode() == BO_Assign) {
6084 X = AtomicBinOp->getLHS();
6085 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6086 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6087 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6088 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6089 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006090 Op = AtomicInnerBinOp->getOpcode();
6091 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006092 auto *LHS = AtomicInnerBinOp->getLHS();
6093 auto *RHS = AtomicInnerBinOp->getRHS();
6094 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6095 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6096 /*Canonical=*/true);
6097 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6098 /*Canonical=*/true);
6099 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6100 /*Canonical=*/true);
6101 if (XId == LHSId) {
6102 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006103 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006104 } else if (XId == RHSId) {
6105 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006106 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006107 } else {
6108 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6109 ErrorRange = AtomicInnerBinOp->getSourceRange();
6110 NoteLoc = X->getExprLoc();
6111 NoteRange = X->getSourceRange();
6112 ErrorFound = NotAnUpdateExpression;
6113 }
6114 } else {
6115 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6116 ErrorRange = AtomicInnerBinOp->getSourceRange();
6117 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6118 NoteRange = SourceRange(NoteLoc, NoteLoc);
6119 ErrorFound = NotABinaryOperator;
6120 }
6121 } else {
6122 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6123 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6124 ErrorFound = NotABinaryExpression;
6125 }
6126 } else {
6127 ErrorLoc = AtomicBinOp->getExprLoc();
6128 ErrorRange = AtomicBinOp->getSourceRange();
6129 NoteLoc = AtomicBinOp->getOperatorLoc();
6130 NoteRange = SourceRange(NoteLoc, NoteLoc);
6131 ErrorFound = NotAnAssignmentOp;
6132 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006133 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006134 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6135 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6136 return true;
6137 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006138 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006139 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006140}
6141
6142bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6143 unsigned NoteId) {
6144 ExprAnalysisErrorCode ErrorFound = NoError;
6145 SourceLocation ErrorLoc, NoteLoc;
6146 SourceRange ErrorRange, NoteRange;
6147 // Allowed constructs are:
6148 // x++;
6149 // x--;
6150 // ++x;
6151 // --x;
6152 // x binop= expr;
6153 // x = x binop expr;
6154 // x = expr binop x;
6155 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6156 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6157 if (AtomicBody->getType()->isScalarType() ||
6158 AtomicBody->isInstantiationDependent()) {
6159 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6160 AtomicBody->IgnoreParenImpCasts())) {
6161 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006162 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006163 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006164 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006165 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006166 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006167 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006168 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6169 AtomicBody->IgnoreParenImpCasts())) {
6170 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006171 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6172 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006173 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006174 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6175 // Check for Unary Operation
6176 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006177 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006178 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6179 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006180 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006181 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6182 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006183 } else {
6184 ErrorFound = NotAnUnaryIncDecExpression;
6185 ErrorLoc = AtomicUnaryOp->getExprLoc();
6186 ErrorRange = AtomicUnaryOp->getSourceRange();
6187 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6188 NoteRange = SourceRange(NoteLoc, NoteLoc);
6189 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006190 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006191 ErrorFound = NotABinaryOrUnaryExpression;
6192 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6193 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6194 }
6195 } else {
6196 ErrorFound = NotAScalarType;
6197 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6198 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6199 }
6200 } else {
6201 ErrorFound = NotAnExpression;
6202 NoteLoc = ErrorLoc = S->getLocStart();
6203 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6204 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006205 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006206 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6207 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6208 return true;
6209 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006210 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006211 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006212 // Build an update expression of form 'OpaqueValueExpr(x) binop
6213 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6214 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6215 auto *OVEX = new (SemaRef.getASTContext())
6216 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6217 auto *OVEExpr = new (SemaRef.getASTContext())
6218 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6219 auto Update =
6220 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6221 IsXLHSInRHSPart ? OVEExpr : OVEX);
6222 if (Update.isInvalid())
6223 return true;
6224 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6225 Sema::AA_Casting);
6226 if (Update.isInvalid())
6227 return true;
6228 UpdateExpr = Update.get();
6229 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006230 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006231}
6232
Alexey Bataev0162e452014-07-22 10:10:35 +00006233StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6234 Stmt *AStmt,
6235 SourceLocation StartLoc,
6236 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006237 if (!AStmt)
6238 return StmtError();
6239
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006240 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006241 // 1.2.2 OpenMP Language Terminology
6242 // Structured block - An executable statement with a single entry at the
6243 // top and a single exit at the bottom.
6244 // The point of exit cannot be a branch out of the structured block.
6245 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006246 OpenMPClauseKind AtomicKind = OMPC_unknown;
6247 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006248 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006249 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006250 C->getClauseKind() == OMPC_update ||
6251 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006252 if (AtomicKind != OMPC_unknown) {
6253 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6254 << SourceRange(C->getLocStart(), C->getLocEnd());
6255 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6256 << getOpenMPClauseName(AtomicKind);
6257 } else {
6258 AtomicKind = C->getClauseKind();
6259 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006260 }
6261 }
6262 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006263
Alexey Bataev459dec02014-07-24 06:46:57 +00006264 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006265 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6266 Body = EWC->getSubExpr();
6267
Alexey Bataev62cec442014-11-18 10:14:22 +00006268 Expr *X = nullptr;
6269 Expr *V = nullptr;
6270 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006271 Expr *UE = nullptr;
6272 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006273 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006274 // OpenMP [2.12.6, atomic Construct]
6275 // In the next expressions:
6276 // * x and v (as applicable) are both l-value expressions with scalar type.
6277 // * During the execution of an atomic region, multiple syntactic
6278 // occurrences of x must designate the same storage location.
6279 // * Neither of v and expr (as applicable) may access the storage location
6280 // designated by x.
6281 // * Neither of x and expr (as applicable) may access the storage location
6282 // designated by v.
6283 // * expr is an expression with scalar type.
6284 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6285 // * binop, binop=, ++, and -- are not overloaded operators.
6286 // * The expression x binop expr must be numerically equivalent to x binop
6287 // (expr). This requirement is satisfied if the operators in expr have
6288 // precedence greater than binop, or by using parentheses around expr or
6289 // subexpressions of expr.
6290 // * The expression expr binop x must be numerically equivalent to (expr)
6291 // binop x. This requirement is satisfied if the operators in expr have
6292 // precedence equal to or greater than binop, or by using parentheses around
6293 // expr or subexpressions of expr.
6294 // * For forms that allow multiple occurrences of x, the number of times
6295 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006296 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006297 enum {
6298 NotAnExpression,
6299 NotAnAssignmentOp,
6300 NotAScalarType,
6301 NotAnLValue,
6302 NoError
6303 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006304 SourceLocation ErrorLoc, NoteLoc;
6305 SourceRange ErrorRange, NoteRange;
6306 // If clause is read:
6307 // v = x;
6308 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6309 auto AtomicBinOp =
6310 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6311 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6312 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6313 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6314 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6315 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6316 if (!X->isLValue() || !V->isLValue()) {
6317 auto NotLValueExpr = X->isLValue() ? V : X;
6318 ErrorFound = NotAnLValue;
6319 ErrorLoc = AtomicBinOp->getExprLoc();
6320 ErrorRange = AtomicBinOp->getSourceRange();
6321 NoteLoc = NotLValueExpr->getExprLoc();
6322 NoteRange = NotLValueExpr->getSourceRange();
6323 }
6324 } else if (!X->isInstantiationDependent() ||
6325 !V->isInstantiationDependent()) {
6326 auto NotScalarExpr =
6327 (X->isInstantiationDependent() || X->getType()->isScalarType())
6328 ? V
6329 : X;
6330 ErrorFound = NotAScalarType;
6331 ErrorLoc = AtomicBinOp->getExprLoc();
6332 ErrorRange = AtomicBinOp->getSourceRange();
6333 NoteLoc = NotScalarExpr->getExprLoc();
6334 NoteRange = NotScalarExpr->getSourceRange();
6335 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006336 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006337 ErrorFound = NotAnAssignmentOp;
6338 ErrorLoc = AtomicBody->getExprLoc();
6339 ErrorRange = AtomicBody->getSourceRange();
6340 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6341 : AtomicBody->getExprLoc();
6342 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6343 : AtomicBody->getSourceRange();
6344 }
6345 } else {
6346 ErrorFound = NotAnExpression;
6347 NoteLoc = ErrorLoc = Body->getLocStart();
6348 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006349 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006350 if (ErrorFound != NoError) {
6351 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6352 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006353 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6354 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006355 return StmtError();
6356 } else if (CurContext->isDependentContext())
6357 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006358 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006359 enum {
6360 NotAnExpression,
6361 NotAnAssignmentOp,
6362 NotAScalarType,
6363 NotAnLValue,
6364 NoError
6365 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006366 SourceLocation ErrorLoc, NoteLoc;
6367 SourceRange ErrorRange, NoteRange;
6368 // If clause is write:
6369 // x = expr;
6370 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6371 auto AtomicBinOp =
6372 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6373 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006374 X = AtomicBinOp->getLHS();
6375 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006376 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6377 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6378 if (!X->isLValue()) {
6379 ErrorFound = NotAnLValue;
6380 ErrorLoc = AtomicBinOp->getExprLoc();
6381 ErrorRange = AtomicBinOp->getSourceRange();
6382 NoteLoc = X->getExprLoc();
6383 NoteRange = X->getSourceRange();
6384 }
6385 } else if (!X->isInstantiationDependent() ||
6386 !E->isInstantiationDependent()) {
6387 auto NotScalarExpr =
6388 (X->isInstantiationDependent() || X->getType()->isScalarType())
6389 ? E
6390 : X;
6391 ErrorFound = NotAScalarType;
6392 ErrorLoc = AtomicBinOp->getExprLoc();
6393 ErrorRange = AtomicBinOp->getSourceRange();
6394 NoteLoc = NotScalarExpr->getExprLoc();
6395 NoteRange = NotScalarExpr->getSourceRange();
6396 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006397 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006398 ErrorFound = NotAnAssignmentOp;
6399 ErrorLoc = AtomicBody->getExprLoc();
6400 ErrorRange = AtomicBody->getSourceRange();
6401 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6402 : AtomicBody->getExprLoc();
6403 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6404 : AtomicBody->getSourceRange();
6405 }
6406 } else {
6407 ErrorFound = NotAnExpression;
6408 NoteLoc = ErrorLoc = Body->getLocStart();
6409 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006410 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006411 if (ErrorFound != NoError) {
6412 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6413 << ErrorRange;
6414 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6415 << NoteRange;
6416 return StmtError();
6417 } else if (CurContext->isDependentContext())
6418 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006419 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006420 // If clause is update:
6421 // x++;
6422 // x--;
6423 // ++x;
6424 // --x;
6425 // x binop= expr;
6426 // x = x binop expr;
6427 // x = expr binop x;
6428 OpenMPAtomicUpdateChecker Checker(*this);
6429 if (Checker.checkStatement(
6430 Body, (AtomicKind == OMPC_update)
6431 ? diag::err_omp_atomic_update_not_expression_statement
6432 : diag::err_omp_atomic_not_expression_statement,
6433 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006434 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006435 if (!CurContext->isDependentContext()) {
6436 E = Checker.getExpr();
6437 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006438 UE = Checker.getUpdateExpr();
6439 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006440 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006441 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006442 enum {
6443 NotAnAssignmentOp,
6444 NotACompoundStatement,
6445 NotTwoSubstatements,
6446 NotASpecificExpression,
6447 NoError
6448 } ErrorFound = NoError;
6449 SourceLocation ErrorLoc, NoteLoc;
6450 SourceRange ErrorRange, NoteRange;
6451 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6452 // If clause is a capture:
6453 // v = x++;
6454 // v = x--;
6455 // v = ++x;
6456 // v = --x;
6457 // v = x binop= expr;
6458 // v = x = x binop expr;
6459 // v = x = expr binop x;
6460 auto *AtomicBinOp =
6461 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6462 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6463 V = AtomicBinOp->getLHS();
6464 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6465 OpenMPAtomicUpdateChecker Checker(*this);
6466 if (Checker.checkStatement(
6467 Body, diag::err_omp_atomic_capture_not_expression_statement,
6468 diag::note_omp_atomic_update))
6469 return StmtError();
6470 E = Checker.getExpr();
6471 X = Checker.getX();
6472 UE = Checker.getUpdateExpr();
6473 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6474 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006475 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006476 ErrorLoc = AtomicBody->getExprLoc();
6477 ErrorRange = AtomicBody->getSourceRange();
6478 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6479 : AtomicBody->getExprLoc();
6480 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6481 : AtomicBody->getSourceRange();
6482 ErrorFound = NotAnAssignmentOp;
6483 }
6484 if (ErrorFound != NoError) {
6485 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6486 << ErrorRange;
6487 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6488 return StmtError();
6489 } else if (CurContext->isDependentContext()) {
6490 UE = V = E = X = nullptr;
6491 }
6492 } else {
6493 // If clause is a capture:
6494 // { v = x; x = expr; }
6495 // { v = x; x++; }
6496 // { v = x; x--; }
6497 // { v = x; ++x; }
6498 // { v = x; --x; }
6499 // { v = x; x binop= expr; }
6500 // { v = x; x = x binop expr; }
6501 // { v = x; x = expr binop x; }
6502 // { x++; v = x; }
6503 // { x--; v = x; }
6504 // { ++x; v = x; }
6505 // { --x; v = x; }
6506 // { x binop= expr; v = x; }
6507 // { x = x binop expr; v = x; }
6508 // { x = expr binop x; v = x; }
6509 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6510 // Check that this is { expr1; expr2; }
6511 if (CS->size() == 2) {
6512 auto *First = CS->body_front();
6513 auto *Second = CS->body_back();
6514 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6515 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6516 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6517 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6518 // Need to find what subexpression is 'v' and what is 'x'.
6519 OpenMPAtomicUpdateChecker Checker(*this);
6520 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6521 BinaryOperator *BinOp = nullptr;
6522 if (IsUpdateExprFound) {
6523 BinOp = dyn_cast<BinaryOperator>(First);
6524 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6525 }
6526 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6527 // { v = x; x++; }
6528 // { v = x; x--; }
6529 // { v = x; ++x; }
6530 // { v = x; --x; }
6531 // { v = x; x binop= expr; }
6532 // { v = x; x = x binop expr; }
6533 // { v = x; x = expr binop x; }
6534 // Check that the first expression has form v = x.
6535 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6536 llvm::FoldingSetNodeID XId, PossibleXId;
6537 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6538 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6539 IsUpdateExprFound = XId == PossibleXId;
6540 if (IsUpdateExprFound) {
6541 V = BinOp->getLHS();
6542 X = Checker.getX();
6543 E = Checker.getExpr();
6544 UE = Checker.getUpdateExpr();
6545 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006546 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006547 }
6548 }
6549 if (!IsUpdateExprFound) {
6550 IsUpdateExprFound = !Checker.checkStatement(First);
6551 BinOp = nullptr;
6552 if (IsUpdateExprFound) {
6553 BinOp = dyn_cast<BinaryOperator>(Second);
6554 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6555 }
6556 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6557 // { x++; v = x; }
6558 // { x--; v = x; }
6559 // { ++x; v = x; }
6560 // { --x; v = x; }
6561 // { x binop= expr; v = x; }
6562 // { x = x binop expr; v = x; }
6563 // { x = expr binop x; v = x; }
6564 // Check that the second expression has form v = x.
6565 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6566 llvm::FoldingSetNodeID XId, PossibleXId;
6567 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6568 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6569 IsUpdateExprFound = XId == PossibleXId;
6570 if (IsUpdateExprFound) {
6571 V = BinOp->getLHS();
6572 X = Checker.getX();
6573 E = Checker.getExpr();
6574 UE = Checker.getUpdateExpr();
6575 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006576 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006577 }
6578 }
6579 }
6580 if (!IsUpdateExprFound) {
6581 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006582 auto *FirstExpr = dyn_cast<Expr>(First);
6583 auto *SecondExpr = dyn_cast<Expr>(Second);
6584 if (!FirstExpr || !SecondExpr ||
6585 !(FirstExpr->isInstantiationDependent() ||
6586 SecondExpr->isInstantiationDependent())) {
6587 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6588 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006589 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006590 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6591 : First->getLocStart();
6592 NoteRange = ErrorRange = FirstBinOp
6593 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006594 : SourceRange(ErrorLoc, ErrorLoc);
6595 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006596 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6597 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6598 ErrorFound = NotAnAssignmentOp;
6599 NoteLoc = ErrorLoc = SecondBinOp
6600 ? SecondBinOp->getOperatorLoc()
6601 : Second->getLocStart();
6602 NoteRange = ErrorRange =
6603 SecondBinOp ? SecondBinOp->getSourceRange()
6604 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006605 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006606 auto *PossibleXRHSInFirst =
6607 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6608 auto *PossibleXLHSInSecond =
6609 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6610 llvm::FoldingSetNodeID X1Id, X2Id;
6611 PossibleXRHSInFirst->Profile(X1Id, Context,
6612 /*Canonical=*/true);
6613 PossibleXLHSInSecond->Profile(X2Id, Context,
6614 /*Canonical=*/true);
6615 IsUpdateExprFound = X1Id == X2Id;
6616 if (IsUpdateExprFound) {
6617 V = FirstBinOp->getLHS();
6618 X = SecondBinOp->getLHS();
6619 E = SecondBinOp->getRHS();
6620 UE = nullptr;
6621 IsXLHSInRHSPart = false;
6622 IsPostfixUpdate = true;
6623 } else {
6624 ErrorFound = NotASpecificExpression;
6625 ErrorLoc = FirstBinOp->getExprLoc();
6626 ErrorRange = FirstBinOp->getSourceRange();
6627 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6628 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6629 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006630 }
6631 }
6632 }
6633 }
6634 } else {
6635 NoteLoc = ErrorLoc = Body->getLocStart();
6636 NoteRange = ErrorRange =
6637 SourceRange(Body->getLocStart(), Body->getLocStart());
6638 ErrorFound = NotTwoSubstatements;
6639 }
6640 } else {
6641 NoteLoc = ErrorLoc = Body->getLocStart();
6642 NoteRange = ErrorRange =
6643 SourceRange(Body->getLocStart(), Body->getLocStart());
6644 ErrorFound = NotACompoundStatement;
6645 }
6646 if (ErrorFound != NoError) {
6647 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6648 << ErrorRange;
6649 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6650 return StmtError();
6651 } else if (CurContext->isDependentContext()) {
6652 UE = V = E = X = nullptr;
6653 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006654 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006655 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006656
6657 getCurFunction()->setHasBranchProtectedScope();
6658
Alexey Bataev62cec442014-11-18 10:14:22 +00006659 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006660 X, V, E, UE, IsXLHSInRHSPart,
6661 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006662}
6663
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006664StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6665 Stmt *AStmt,
6666 SourceLocation StartLoc,
6667 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006668 if (!AStmt)
6669 return StmtError();
6670
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006671 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6672 // 1.2.2 OpenMP Language Terminology
6673 // Structured block - An executable statement with a single entry at the
6674 // top and a single exit at the bottom.
6675 // The point of exit cannot be a branch out of the structured block.
6676 // longjmp() and throw() must not violate the entry/exit criteria.
6677 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006678
Alexey Bataev13314bf2014-10-09 04:18:56 +00006679 // OpenMP [2.16, Nesting of Regions]
6680 // If specified, a teams construct must be contained within a target
6681 // construct. That target construct must contain no statements or directives
6682 // outside of the teams construct.
6683 if (DSAStack->hasInnerTeamsRegion()) {
6684 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6685 bool OMPTeamsFound = true;
6686 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6687 auto I = CS->body_begin();
6688 while (I != CS->body_end()) {
6689 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6690 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6691 OMPTeamsFound = false;
6692 break;
6693 }
6694 ++I;
6695 }
6696 assert(I != CS->body_end() && "Not found statement");
6697 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006698 } else {
6699 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6700 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006701 }
6702 if (!OMPTeamsFound) {
6703 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6704 Diag(DSAStack->getInnerTeamsRegionLoc(),
6705 diag::note_omp_nested_teams_construct_here);
6706 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6707 << isa<OMPExecutableDirective>(S);
6708 return StmtError();
6709 }
6710 }
6711
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006712 getCurFunction()->setHasBranchProtectedScope();
6713
6714 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6715}
6716
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006717StmtResult
6718Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6719 Stmt *AStmt, SourceLocation StartLoc,
6720 SourceLocation EndLoc) {
6721 if (!AStmt)
6722 return StmtError();
6723
6724 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6725 // 1.2.2 OpenMP Language Terminology
6726 // Structured block - An executable statement with a single entry at the
6727 // top and a single exit at the bottom.
6728 // The point of exit cannot be a branch out of the structured block.
6729 // longjmp() and throw() must not violate the entry/exit criteria.
6730 CS->getCapturedDecl()->setNothrow();
6731
6732 getCurFunction()->setHasBranchProtectedScope();
6733
6734 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6735 AStmt);
6736}
6737
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006738StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6739 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6740 SourceLocation EndLoc,
6741 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6742 if (!AStmt)
6743 return StmtError();
6744
6745 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6746 // 1.2.2 OpenMP Language Terminology
6747 // Structured block - An executable statement with a single entry at the
6748 // top and a single exit at the bottom.
6749 // The point of exit cannot be a branch out of the structured block.
6750 // longjmp() and throw() must not violate the entry/exit criteria.
6751 CS->getCapturedDecl()->setNothrow();
6752
6753 OMPLoopDirective::HelperExprs B;
6754 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6755 // define the nested loops number.
6756 unsigned NestedLoopCount =
6757 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6758 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6759 VarsWithImplicitDSA, B);
6760 if (NestedLoopCount == 0)
6761 return StmtError();
6762
6763 assert((CurContext->isDependentContext() || B.builtAll()) &&
6764 "omp target parallel for loop exprs were not built");
6765
6766 if (!CurContext->isDependentContext()) {
6767 // Finalize the clauses that need pre-built expressions for CodeGen.
6768 for (auto C : Clauses) {
6769 if (auto LC = dyn_cast<OMPLinearClause>(C))
6770 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006771 B.NumIterations, *this, CurScope,
6772 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006773 return StmtError();
6774 }
6775 }
6776
6777 getCurFunction()->setHasBranchProtectedScope();
6778 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6779 NestedLoopCount, Clauses, AStmt,
6780 B, DSAStack->isCancelRegion());
6781}
6782
Samuel Antaodf67fc42016-01-19 19:15:56 +00006783/// \brief Check for existence of a map clause in the list of clauses.
6784static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6785 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6786 I != E; ++I) {
6787 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6788 return true;
6789 }
6790 }
6791
6792 return false;
6793}
6794
Michael Wong65f367f2015-07-21 13:44:28 +00006795StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6796 Stmt *AStmt,
6797 SourceLocation StartLoc,
6798 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006799 if (!AStmt)
6800 return StmtError();
6801
6802 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6803
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006804 // OpenMP [2.10.1, Restrictions, p. 97]
6805 // At least one map clause must appear on the directive.
6806 if (!HasMapClause(Clauses)) {
6807 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6808 getOpenMPDirectiveName(OMPD_target_data);
6809 return StmtError();
6810 }
6811
Michael Wong65f367f2015-07-21 13:44:28 +00006812 getCurFunction()->setHasBranchProtectedScope();
6813
6814 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6815 AStmt);
6816}
6817
Samuel Antaodf67fc42016-01-19 19:15:56 +00006818StmtResult
6819Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6820 SourceLocation StartLoc,
6821 SourceLocation EndLoc) {
6822 // OpenMP [2.10.2, Restrictions, p. 99]
6823 // At least one map clause must appear on the directive.
6824 if (!HasMapClause(Clauses)) {
6825 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6826 << getOpenMPDirectiveName(OMPD_target_enter_data);
6827 return StmtError();
6828 }
6829
6830 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6831 Clauses);
6832}
6833
Samuel Antao72590762016-01-19 20:04:50 +00006834StmtResult
6835Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6836 SourceLocation StartLoc,
6837 SourceLocation EndLoc) {
6838 // OpenMP [2.10.3, Restrictions, p. 102]
6839 // At least one map clause must appear on the directive.
6840 if (!HasMapClause(Clauses)) {
6841 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6842 << getOpenMPDirectiveName(OMPD_target_exit_data);
6843 return StmtError();
6844 }
6845
6846 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6847}
6848
Samuel Antao686c70c2016-05-26 17:30:50 +00006849StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6850 SourceLocation StartLoc,
6851 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006852 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00006853 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00006854 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00006855 seenMotionClause = true;
6856 }
Samuel Antao686c70c2016-05-26 17:30:50 +00006857 if (!seenMotionClause) {
6858 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6859 return StmtError();
6860 }
6861 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6862}
6863
Alexey Bataev13314bf2014-10-09 04:18:56 +00006864StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6865 Stmt *AStmt, SourceLocation StartLoc,
6866 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006867 if (!AStmt)
6868 return StmtError();
6869
Alexey Bataev13314bf2014-10-09 04:18:56 +00006870 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6871 // 1.2.2 OpenMP Language Terminology
6872 // Structured block - An executable statement with a single entry at the
6873 // top and a single exit at the bottom.
6874 // The point of exit cannot be a branch out of the structured block.
6875 // longjmp() and throw() must not violate the entry/exit criteria.
6876 CS->getCapturedDecl()->setNothrow();
6877
6878 getCurFunction()->setHasBranchProtectedScope();
6879
6880 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6881}
6882
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006883StmtResult
6884Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6885 SourceLocation EndLoc,
6886 OpenMPDirectiveKind CancelRegion) {
6887 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6888 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6889 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6890 << getOpenMPDirectiveName(CancelRegion);
6891 return StmtError();
6892 }
6893 if (DSAStack->isParentNowaitRegion()) {
6894 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6895 return StmtError();
6896 }
6897 if (DSAStack->isParentOrderedRegion()) {
6898 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6899 return StmtError();
6900 }
6901 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6902 CancelRegion);
6903}
6904
Alexey Bataev87933c72015-09-18 08:07:34 +00006905StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6906 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006907 SourceLocation EndLoc,
6908 OpenMPDirectiveKind CancelRegion) {
6909 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6910 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6911 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6912 << getOpenMPDirectiveName(CancelRegion);
6913 return StmtError();
6914 }
6915 if (DSAStack->isParentNowaitRegion()) {
6916 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6917 return StmtError();
6918 }
6919 if (DSAStack->isParentOrderedRegion()) {
6920 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6921 return StmtError();
6922 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006923 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006924 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6925 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006926}
6927
Alexey Bataev382967a2015-12-08 12:06:20 +00006928static bool checkGrainsizeNumTasksClauses(Sema &S,
6929 ArrayRef<OMPClause *> Clauses) {
6930 OMPClause *PrevClause = nullptr;
6931 bool ErrorFound = false;
6932 for (auto *C : Clauses) {
6933 if (C->getClauseKind() == OMPC_grainsize ||
6934 C->getClauseKind() == OMPC_num_tasks) {
6935 if (!PrevClause)
6936 PrevClause = C;
6937 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6938 S.Diag(C->getLocStart(),
6939 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6940 << getOpenMPClauseName(C->getClauseKind())
6941 << getOpenMPClauseName(PrevClause->getClauseKind());
6942 S.Diag(PrevClause->getLocStart(),
6943 diag::note_omp_previous_grainsize_num_tasks)
6944 << getOpenMPClauseName(PrevClause->getClauseKind());
6945 ErrorFound = true;
6946 }
6947 }
6948 }
6949 return ErrorFound;
6950}
6951
Alexey Bataev49f6e782015-12-01 04:18:41 +00006952StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6953 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6954 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006955 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006956 if (!AStmt)
6957 return StmtError();
6958
6959 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6960 OMPLoopDirective::HelperExprs B;
6961 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6962 // define the nested loops number.
6963 unsigned NestedLoopCount =
6964 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006965 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006966 VarsWithImplicitDSA, B);
6967 if (NestedLoopCount == 0)
6968 return StmtError();
6969
6970 assert((CurContext->isDependentContext() || B.builtAll()) &&
6971 "omp for loop exprs were not built");
6972
Alexey Bataev382967a2015-12-08 12:06:20 +00006973 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6974 // The grainsize clause and num_tasks clause are mutually exclusive and may
6975 // not appear on the same taskloop directive.
6976 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6977 return StmtError();
6978
Alexey Bataev49f6e782015-12-01 04:18:41 +00006979 getCurFunction()->setHasBranchProtectedScope();
6980 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6981 NestedLoopCount, Clauses, AStmt, B);
6982}
6983
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006984StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6985 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6986 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006987 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006988 if (!AStmt)
6989 return StmtError();
6990
6991 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6992 OMPLoopDirective::HelperExprs B;
6993 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6994 // define the nested loops number.
6995 unsigned NestedLoopCount =
6996 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6997 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6998 VarsWithImplicitDSA, B);
6999 if (NestedLoopCount == 0)
7000 return StmtError();
7001
7002 assert((CurContext->isDependentContext() || B.builtAll()) &&
7003 "omp for loop exprs were not built");
7004
Alexey Bataev5a3af132016-03-29 08:58:54 +00007005 if (!CurContext->isDependentContext()) {
7006 // Finalize the clauses that need pre-built expressions for CodeGen.
7007 for (auto C : Clauses) {
7008 if (auto LC = dyn_cast<OMPLinearClause>(C))
7009 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007010 B.NumIterations, *this, CurScope,
7011 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007012 return StmtError();
7013 }
7014 }
7015
Alexey Bataev382967a2015-12-08 12:06:20 +00007016 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7017 // The grainsize clause and num_tasks clause are mutually exclusive and may
7018 // not appear on the same taskloop directive.
7019 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7020 return StmtError();
7021
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007022 getCurFunction()->setHasBranchProtectedScope();
7023 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7024 NestedLoopCount, Clauses, AStmt, B);
7025}
7026
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007027StmtResult Sema::ActOnOpenMPDistributeDirective(
7028 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7029 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007030 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007031 if (!AStmt)
7032 return StmtError();
7033
7034 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7035 OMPLoopDirective::HelperExprs B;
7036 // In presence of clause 'collapse' with number of loops, it will
7037 // define the nested loops number.
7038 unsigned NestedLoopCount =
7039 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7040 nullptr /*ordered not a clause on distribute*/, AStmt,
7041 *this, *DSAStack, VarsWithImplicitDSA, B);
7042 if (NestedLoopCount == 0)
7043 return StmtError();
7044
7045 assert((CurContext->isDependentContext() || B.builtAll()) &&
7046 "omp for loop exprs were not built");
7047
7048 getCurFunction()->setHasBranchProtectedScope();
7049 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7050 NestedLoopCount, Clauses, AStmt, B);
7051}
7052
Carlo Bertolli9925f152016-06-27 14:55:37 +00007053StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7054 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7055 SourceLocation EndLoc,
7056 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7057 if (!AStmt)
7058 return StmtError();
7059
7060 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7061 // 1.2.2 OpenMP Language Terminology
7062 // Structured block - An executable statement with a single entry at the
7063 // top and a single exit at the bottom.
7064 // The point of exit cannot be a branch out of the structured block.
7065 // longjmp() and throw() must not violate the entry/exit criteria.
7066 CS->getCapturedDecl()->setNothrow();
7067
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 = CheckOpenMPLoop(
7072 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7073 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7074 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 OMPDistributeParallelForDirective::Create(
7083 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7084}
7085
Kelvin Li4a39add2016-07-05 05:00:15 +00007086StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
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_simd, 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
Kelvin Lic5609492016-07-15 04:39:07 +00007114 if (checkSimdlenSafelenSpecified(*this, Clauses))
7115 return StmtError();
7116
Kelvin Li4a39add2016-07-05 05:00:15 +00007117 getCurFunction()->setHasBranchProtectedScope();
7118 return OMPDistributeParallelForSimdDirective::Create(
7119 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7120}
7121
Kelvin Li787f3fc2016-07-06 04:45:38 +00007122StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7123 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7124 SourceLocation EndLoc,
7125 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7126 if (!AStmt)
7127 return StmtError();
7128
7129 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7130 // 1.2.2 OpenMP Language Terminology
7131 // Structured block - An executable statement with a single entry at the
7132 // top and a single exit at the bottom.
7133 // The point of exit cannot be a branch out of the structured block.
7134 // longjmp() and throw() must not violate the entry/exit criteria.
7135 CS->getCapturedDecl()->setNothrow();
7136
7137 OMPLoopDirective::HelperExprs B;
7138 // In presence of clause 'collapse' with number of loops, it will
7139 // define the nested loops number.
7140 unsigned NestedLoopCount =
7141 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7142 nullptr /*ordered not a clause on distribute*/, AStmt,
7143 *this, *DSAStack, VarsWithImplicitDSA, B);
7144 if (NestedLoopCount == 0)
7145 return StmtError();
7146
7147 assert((CurContext->isDependentContext() || B.builtAll()) &&
7148 "omp for loop exprs were not built");
7149
Kelvin Lic5609492016-07-15 04:39:07 +00007150 if (checkSimdlenSafelenSpecified(*this, Clauses))
7151 return StmtError();
7152
Kelvin Li787f3fc2016-07-06 04:45:38 +00007153 getCurFunction()->setHasBranchProtectedScope();
7154 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7155 NestedLoopCount, Clauses, AStmt, B);
7156}
7157
Kelvin Lia579b912016-07-14 02:54:56 +00007158StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7159 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7160 SourceLocation EndLoc,
7161 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7162 if (!AStmt)
7163 return StmtError();
7164
7165 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7166 // 1.2.2 OpenMP Language Terminology
7167 // Structured block - An executable statement with a single entry at the
7168 // top and a single exit at the bottom.
7169 // The point of exit cannot be a branch out of the structured block.
7170 // longjmp() and throw() must not violate the entry/exit criteria.
7171 CS->getCapturedDecl()->setNothrow();
7172
7173 OMPLoopDirective::HelperExprs B;
7174 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7175 // define the nested loops number.
7176 unsigned NestedLoopCount = CheckOpenMPLoop(
7177 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7178 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7179 VarsWithImplicitDSA, B);
7180 if (NestedLoopCount == 0)
7181 return StmtError();
7182
7183 assert((CurContext->isDependentContext() || B.builtAll()) &&
7184 "omp target parallel for simd loop exprs were not built");
7185
7186 if (!CurContext->isDependentContext()) {
7187 // Finalize the clauses that need pre-built expressions for CodeGen.
7188 for (auto C : Clauses) {
7189 if (auto LC = dyn_cast<OMPLinearClause>(C))
7190 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7191 B.NumIterations, *this, CurScope,
7192 DSAStack))
7193 return StmtError();
7194 }
7195 }
Kelvin Lic5609492016-07-15 04:39:07 +00007196 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007197 return StmtError();
7198
7199 getCurFunction()->setHasBranchProtectedScope();
7200 return OMPTargetParallelForSimdDirective::Create(
7201 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7202}
7203
Alexey Bataeved09d242014-05-28 05:53:51 +00007204OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007205 SourceLocation StartLoc,
7206 SourceLocation LParenLoc,
7207 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007208 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007209 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007210 case OMPC_final:
7211 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7212 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007213 case OMPC_num_threads:
7214 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7215 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007216 case OMPC_safelen:
7217 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7218 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007219 case OMPC_simdlen:
7220 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7221 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007222 case OMPC_collapse:
7223 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7224 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007225 case OMPC_ordered:
7226 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7227 break;
Michael Wonge710d542015-08-07 16:16:36 +00007228 case OMPC_device:
7229 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7230 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007231 case OMPC_num_teams:
7232 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7233 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007234 case OMPC_thread_limit:
7235 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7236 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007237 case OMPC_priority:
7238 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7239 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007240 case OMPC_grainsize:
7241 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7242 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007243 case OMPC_num_tasks:
7244 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7245 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007246 case OMPC_hint:
7247 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7248 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007249 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007250 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007251 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007252 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007253 case OMPC_private:
7254 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007255 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007256 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007257 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007258 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007259 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007260 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007261 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007262 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007263 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007264 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007265 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007266 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007267 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007268 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007269 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007270 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007271 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007272 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007273 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007274 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007275 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007276 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007277 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007278 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007279 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007280 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007281 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007282 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007283 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007284 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007285 llvm_unreachable("Clause is not allowed.");
7286 }
7287 return Res;
7288}
7289
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007290OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7291 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007292 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007293 SourceLocation NameModifierLoc,
7294 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007295 SourceLocation EndLoc) {
7296 Expr *ValExpr = Condition;
7297 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7298 !Condition->isInstantiationDependent() &&
7299 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007300 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007301 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007302 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007303
Richard Smith03a4aa32016-06-23 19:02:52 +00007304 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007305 }
7306
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007307 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7308 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007309}
7310
Alexey Bataev3778b602014-07-17 07:32:53 +00007311OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7312 SourceLocation StartLoc,
7313 SourceLocation LParenLoc,
7314 SourceLocation EndLoc) {
7315 Expr *ValExpr = Condition;
7316 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7317 !Condition->isInstantiationDependent() &&
7318 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007319 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007320 if (Val.isInvalid())
7321 return nullptr;
7322
Richard Smith03a4aa32016-06-23 19:02:52 +00007323 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007324 }
7325
7326 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7327}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007328ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7329 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007330 if (!Op)
7331 return ExprError();
7332
7333 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7334 public:
7335 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007336 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007337 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7338 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007339 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7340 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007341 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7342 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007343 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7344 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007345 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7346 QualType T,
7347 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007348 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7349 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007350 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7351 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007352 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007353 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007354 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007355 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7356 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007357 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7358 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007359 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7360 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007361 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007362 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007363 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007364 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7365 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007366 llvm_unreachable("conversion functions are permitted");
7367 }
7368 } ConvertDiagnoser;
7369 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7370}
7371
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007372static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007373 OpenMPClauseKind CKind,
7374 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007375 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7376 !ValExpr->isInstantiationDependent()) {
7377 SourceLocation Loc = ValExpr->getExprLoc();
7378 ExprResult Value =
7379 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7380 if (Value.isInvalid())
7381 return false;
7382
7383 ValExpr = Value.get();
7384 // The expression must evaluate to a non-negative integer value.
7385 llvm::APSInt Result;
7386 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007387 Result.isSigned() &&
7388 !((!StrictlyPositive && Result.isNonNegative()) ||
7389 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007390 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007391 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7392 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007393 return false;
7394 }
7395 }
7396 return true;
7397}
7398
Alexey Bataev568a8332014-03-06 06:15:19 +00007399OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7400 SourceLocation StartLoc,
7401 SourceLocation LParenLoc,
7402 SourceLocation EndLoc) {
7403 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007404
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007405 // OpenMP [2.5, Restrictions]
7406 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007407 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7408 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007409 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007410
Alexey Bataeved09d242014-05-28 05:53:51 +00007411 return new (Context)
7412 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007413}
7414
Alexey Bataev62c87d22014-03-21 04:51:18 +00007415ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007416 OpenMPClauseKind CKind,
7417 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007418 if (!E)
7419 return ExprError();
7420 if (E->isValueDependent() || E->isTypeDependent() ||
7421 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007422 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007423 llvm::APSInt Result;
7424 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7425 if (ICE.isInvalid())
7426 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007427 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7428 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007429 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007430 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7431 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007432 return ExprError();
7433 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007434 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7435 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7436 << E->getSourceRange();
7437 return ExprError();
7438 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007439 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7440 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007441 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007442 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007443 return ICE;
7444}
7445
7446OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7447 SourceLocation LParenLoc,
7448 SourceLocation EndLoc) {
7449 // OpenMP [2.8.1, simd construct, Description]
7450 // The parameter of the safelen clause must be a constant
7451 // positive integer expression.
7452 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7453 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007454 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007455 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007456 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007457}
7458
Alexey Bataev66b15b52015-08-21 11:14:16 +00007459OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7460 SourceLocation LParenLoc,
7461 SourceLocation EndLoc) {
7462 // OpenMP [2.8.1, simd construct, Description]
7463 // The parameter of the simdlen clause must be a constant
7464 // positive integer expression.
7465 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7466 if (Simdlen.isInvalid())
7467 return nullptr;
7468 return new (Context)
7469 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7470}
7471
Alexander Musman64d33f12014-06-04 07:53:32 +00007472OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7473 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007474 SourceLocation LParenLoc,
7475 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007476 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007477 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007478 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007479 // The parameter of the collapse clause must be a constant
7480 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007481 ExprResult NumForLoopsResult =
7482 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7483 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007484 return nullptr;
7485 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007486 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007487}
7488
Alexey Bataev10e775f2015-07-30 11:36:16 +00007489OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7490 SourceLocation EndLoc,
7491 SourceLocation LParenLoc,
7492 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007493 // OpenMP [2.7.1, loop construct, Description]
7494 // OpenMP [2.8.1, simd construct, Description]
7495 // OpenMP [2.9.6, distribute construct, Description]
7496 // The parameter of the ordered clause must be a constant
7497 // positive integer expression if any.
7498 if (NumForLoops && LParenLoc.isValid()) {
7499 ExprResult NumForLoopsResult =
7500 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7501 if (NumForLoopsResult.isInvalid())
7502 return nullptr;
7503 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007504 } else
7505 NumForLoops = nullptr;
7506 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007507 return new (Context)
7508 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7509}
7510
Alexey Bataeved09d242014-05-28 05:53:51 +00007511OMPClause *Sema::ActOnOpenMPSimpleClause(
7512 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7513 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007514 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007515 switch (Kind) {
7516 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007517 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007518 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7519 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007520 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007521 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007522 Res = ActOnOpenMPProcBindClause(
7523 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7524 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007525 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007526 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007527 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007528 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007529 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007530 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007531 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007532 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007533 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007534 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007535 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007536 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007537 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007538 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007539 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007540 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007541 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007542 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007543 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007544 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007545 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007546 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007547 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007548 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007549 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007550 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007551 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007552 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007553 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007554 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007555 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007556 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007557 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007558 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007559 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007560 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007561 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007562 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007563 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007564 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007565 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007566 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007567 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007568 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007569 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007570 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007571 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007572 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007573 llvm_unreachable("Clause is not allowed.");
7574 }
7575 return Res;
7576}
7577
Alexey Bataev6402bca2015-12-28 07:25:51 +00007578static std::string
7579getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7580 ArrayRef<unsigned> Exclude = llvm::None) {
7581 std::string Values;
7582 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7583 unsigned Skipped = Exclude.size();
7584 auto S = Exclude.begin(), E = Exclude.end();
7585 for (unsigned i = First; i < Last; ++i) {
7586 if (std::find(S, E, i) != E) {
7587 --Skipped;
7588 continue;
7589 }
7590 Values += "'";
7591 Values += getOpenMPSimpleClauseTypeName(K, i);
7592 Values += "'";
7593 if (i == Bound - Skipped)
7594 Values += " or ";
7595 else if (i != Bound + 1 - Skipped)
7596 Values += ", ";
7597 }
7598 return Values;
7599}
7600
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007601OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7602 SourceLocation KindKwLoc,
7603 SourceLocation StartLoc,
7604 SourceLocation LParenLoc,
7605 SourceLocation EndLoc) {
7606 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007607 static_assert(OMPC_DEFAULT_unknown > 0,
7608 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007609 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007610 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7611 /*Last=*/OMPC_DEFAULT_unknown)
7612 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007613 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007614 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007615 switch (Kind) {
7616 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007617 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007618 break;
7619 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007620 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007621 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007622 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007623 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007624 break;
7625 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007626 return new (Context)
7627 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007628}
7629
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007630OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7631 SourceLocation KindKwLoc,
7632 SourceLocation StartLoc,
7633 SourceLocation LParenLoc,
7634 SourceLocation EndLoc) {
7635 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007636 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007637 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7638 /*Last=*/OMPC_PROC_BIND_unknown)
7639 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007640 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007641 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007642 return new (Context)
7643 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007644}
7645
Alexey Bataev56dafe82014-06-20 07:16:17 +00007646OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007647 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007648 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007649 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007650 SourceLocation EndLoc) {
7651 OMPClause *Res = nullptr;
7652 switch (Kind) {
7653 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007654 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7655 assert(Argument.size() == NumberOfElements &&
7656 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007657 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007658 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7659 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7660 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7661 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7662 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007663 break;
7664 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007665 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7666 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7667 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7668 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007669 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007670 case OMPC_dist_schedule:
7671 Res = ActOnOpenMPDistScheduleClause(
7672 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7673 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7674 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007675 case OMPC_defaultmap:
7676 enum { Modifier, DefaultmapKind };
7677 Res = ActOnOpenMPDefaultmapClause(
7678 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7679 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7680 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7681 ArgumentLoc[DefaultmapKind], EndLoc);
7682 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007683 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007684 case OMPC_num_threads:
7685 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007686 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007687 case OMPC_collapse:
7688 case OMPC_default:
7689 case OMPC_proc_bind:
7690 case OMPC_private:
7691 case OMPC_firstprivate:
7692 case OMPC_lastprivate:
7693 case OMPC_shared:
7694 case OMPC_reduction:
7695 case OMPC_linear:
7696 case OMPC_aligned:
7697 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007698 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007699 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007700 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007701 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007702 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007703 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007704 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007705 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007706 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007707 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007708 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007709 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007710 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007711 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007712 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007713 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007714 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007715 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007716 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007717 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007718 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007719 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007720 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007721 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007722 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007723 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007724 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007725 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007726 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007727 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007728 llvm_unreachable("Clause is not allowed.");
7729 }
7730 return Res;
7731}
7732
Alexey Bataev6402bca2015-12-28 07:25:51 +00007733static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7734 OpenMPScheduleClauseModifier M2,
7735 SourceLocation M1Loc, SourceLocation M2Loc) {
7736 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7737 SmallVector<unsigned, 2> Excluded;
7738 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7739 Excluded.push_back(M2);
7740 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7741 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7742 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7743 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7744 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7745 << getListOfPossibleValues(OMPC_schedule,
7746 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7747 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7748 Excluded)
7749 << getOpenMPClauseName(OMPC_schedule);
7750 return true;
7751 }
7752 return false;
7753}
7754
Alexey Bataev56dafe82014-06-20 07:16:17 +00007755OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007756 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007757 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007758 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7759 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7760 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7761 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7762 return nullptr;
7763 // OpenMP, 2.7.1, Loop Construct, Restrictions
7764 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7765 // but not both.
7766 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7767 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7768 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7769 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7770 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7771 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7772 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7773 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7774 return nullptr;
7775 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007776 if (Kind == OMPC_SCHEDULE_unknown) {
7777 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007778 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7779 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7780 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7781 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7782 Exclude);
7783 } else {
7784 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7785 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007786 }
7787 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7788 << Values << getOpenMPClauseName(OMPC_schedule);
7789 return nullptr;
7790 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007791 // OpenMP, 2.7.1, Loop Construct, Restrictions
7792 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7793 // schedule(guided).
7794 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7795 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7796 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7797 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7798 diag::err_omp_schedule_nonmonotonic_static);
7799 return nullptr;
7800 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007801 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007802 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007803 if (ChunkSize) {
7804 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7805 !ChunkSize->isInstantiationDependent() &&
7806 !ChunkSize->containsUnexpandedParameterPack()) {
7807 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7808 ExprResult Val =
7809 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7810 if (Val.isInvalid())
7811 return nullptr;
7812
7813 ValExpr = Val.get();
7814
7815 // OpenMP [2.7.1, Restrictions]
7816 // chunk_size must be a loop invariant integer expression with a positive
7817 // value.
7818 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007819 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7820 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7821 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007822 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007823 return nullptr;
7824 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007825 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7826 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007827 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7828 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7829 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007830 }
7831 }
7832 }
7833
Alexey Bataev6402bca2015-12-28 07:25:51 +00007834 return new (Context)
7835 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007836 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007837}
7838
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007839OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7840 SourceLocation StartLoc,
7841 SourceLocation EndLoc) {
7842 OMPClause *Res = nullptr;
7843 switch (Kind) {
7844 case OMPC_ordered:
7845 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7846 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007847 case OMPC_nowait:
7848 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7849 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007850 case OMPC_untied:
7851 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7852 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007853 case OMPC_mergeable:
7854 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7855 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007856 case OMPC_read:
7857 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7858 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007859 case OMPC_write:
7860 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7861 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007862 case OMPC_update:
7863 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7864 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007865 case OMPC_capture:
7866 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7867 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007868 case OMPC_seq_cst:
7869 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7870 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007871 case OMPC_threads:
7872 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7873 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007874 case OMPC_simd:
7875 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7876 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007877 case OMPC_nogroup:
7878 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7879 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007880 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007881 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007882 case OMPC_num_threads:
7883 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007884 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007885 case OMPC_collapse:
7886 case OMPC_schedule:
7887 case OMPC_private:
7888 case OMPC_firstprivate:
7889 case OMPC_lastprivate:
7890 case OMPC_shared:
7891 case OMPC_reduction:
7892 case OMPC_linear:
7893 case OMPC_aligned:
7894 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007895 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007896 case OMPC_default:
7897 case OMPC_proc_bind:
7898 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007899 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007900 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007901 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007902 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007903 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007904 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007905 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007906 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007907 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007908 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007909 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007910 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007911 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007912 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007913 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007914 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007915 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007916 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007917 llvm_unreachable("Clause is not allowed.");
7918 }
7919 return Res;
7920}
7921
Alexey Bataev236070f2014-06-20 11:19:47 +00007922OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7923 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007924 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007925 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7926}
7927
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007928OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7929 SourceLocation EndLoc) {
7930 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7931}
7932
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007933OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7934 SourceLocation EndLoc) {
7935 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7936}
7937
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007938OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7939 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007940 return new (Context) OMPReadClause(StartLoc, EndLoc);
7941}
7942
Alexey Bataevdea47612014-07-23 07:46:59 +00007943OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7944 SourceLocation EndLoc) {
7945 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7946}
7947
Alexey Bataev67a4f222014-07-23 10:25:33 +00007948OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7949 SourceLocation EndLoc) {
7950 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7951}
7952
Alexey Bataev459dec02014-07-24 06:46:57 +00007953OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7954 SourceLocation EndLoc) {
7955 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7956}
7957
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007958OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7959 SourceLocation EndLoc) {
7960 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7961}
7962
Alexey Bataev346265e2015-09-25 10:37:12 +00007963OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7964 SourceLocation EndLoc) {
7965 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7966}
7967
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007968OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7969 SourceLocation EndLoc) {
7970 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7971}
7972
Alexey Bataevb825de12015-12-07 10:51:44 +00007973OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7974 SourceLocation EndLoc) {
7975 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7976}
7977
Alexey Bataevc5e02582014-06-16 07:08:35 +00007978OMPClause *Sema::ActOnOpenMPVarListClause(
7979 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7980 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7981 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007982 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007983 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7984 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7985 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007986 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007987 switch (Kind) {
7988 case OMPC_private:
7989 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7990 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007991 case OMPC_firstprivate:
7992 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7993 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007994 case OMPC_lastprivate:
7995 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7996 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007997 case OMPC_shared:
7998 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7999 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008000 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008001 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8002 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008003 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008004 case OMPC_linear:
8005 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008006 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008007 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008008 case OMPC_aligned:
8009 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8010 ColonLoc, EndLoc);
8011 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008012 case OMPC_copyin:
8013 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8014 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008015 case OMPC_copyprivate:
8016 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8017 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008018 case OMPC_flush:
8019 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8020 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008021 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008022 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8023 StartLoc, LParenLoc, EndLoc);
8024 break;
8025 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008026 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8027 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8028 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008029 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008030 case OMPC_to:
8031 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8032 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008033 case OMPC_from:
8034 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8035 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008036 case OMPC_use_device_ptr:
8037 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8038 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008039 case OMPC_is_device_ptr:
8040 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8041 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008042 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008043 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008044 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008045 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008046 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008047 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008048 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008049 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008050 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008051 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008052 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008053 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008054 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008055 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008056 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008057 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008058 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008059 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008060 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008061 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008062 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008063 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008064 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008065 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008066 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008067 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008068 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008069 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008070 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008071 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008072 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008073 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008074 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008075 llvm_unreachable("Clause is not allowed.");
8076 }
8077 return Res;
8078}
8079
Alexey Bataev90c228f2016-02-08 09:29:13 +00008080ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008081 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008082 ExprResult Res = BuildDeclRefExpr(
8083 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8084 if (!Res.isUsable())
8085 return ExprError();
8086 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8087 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8088 if (!Res.isUsable())
8089 return ExprError();
8090 }
8091 if (VK != VK_LValue && Res.get()->isGLValue()) {
8092 Res = DefaultLvalueConversion(Res.get());
8093 if (!Res.isUsable())
8094 return ExprError();
8095 }
8096 return Res;
8097}
8098
Alexey Bataev60da77e2016-02-29 05:54:20 +00008099static std::pair<ValueDecl *, bool>
8100getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8101 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008102 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8103 RefExpr->containsUnexpandedParameterPack())
8104 return std::make_pair(nullptr, true);
8105
Alexey Bataevd985eda2016-02-10 11:29:16 +00008106 // OpenMP [3.1, C/C++]
8107 // A list item is a variable name.
8108 // OpenMP [2.9.3.3, Restrictions, p.1]
8109 // A variable that is part of another variable (as an array or
8110 // structure element) cannot appear in a private clause.
8111 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008112 enum {
8113 NoArrayExpr = -1,
8114 ArraySubscript = 0,
8115 OMPArraySection = 1
8116 } IsArrayExpr = NoArrayExpr;
8117 if (AllowArraySection) {
8118 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8119 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8120 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8121 Base = TempASE->getBase()->IgnoreParenImpCasts();
8122 RefExpr = Base;
8123 IsArrayExpr = ArraySubscript;
8124 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8125 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8126 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8127 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8128 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8129 Base = TempASE->getBase()->IgnoreParenImpCasts();
8130 RefExpr = Base;
8131 IsArrayExpr = OMPArraySection;
8132 }
8133 }
8134 ELoc = RefExpr->getExprLoc();
8135 ERange = RefExpr->getSourceRange();
8136 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008137 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8138 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8139 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8140 (S.getCurrentThisType().isNull() || !ME ||
8141 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8142 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008143 if (IsArrayExpr != NoArrayExpr)
8144 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8145 << ERange;
8146 else {
8147 S.Diag(ELoc,
8148 AllowArraySection
8149 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8150 : diag::err_omp_expected_var_name_member_expr)
8151 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8152 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008153 return std::make_pair(nullptr, false);
8154 }
8155 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8156}
8157
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008158OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8159 SourceLocation StartLoc,
8160 SourceLocation LParenLoc,
8161 SourceLocation EndLoc) {
8162 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008163 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008164 for (auto &RefExpr : VarList) {
8165 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008166 SourceLocation ELoc;
8167 SourceRange ERange;
8168 Expr *SimpleRefExpr = RefExpr;
8169 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008170 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008171 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008172 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008173 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008174 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008175 ValueDecl *D = Res.first;
8176 if (!D)
8177 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008178
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008179 QualType Type = D->getType();
8180 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008181
8182 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8183 // A variable that appears in a private clause must not have an incomplete
8184 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008185 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008186 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008187 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008188
Alexey Bataev758e55e2013-09-06 18:03:48 +00008189 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8190 // in a Construct]
8191 // Variables with the predetermined data-sharing attributes may not be
8192 // listed in data-sharing attributes clauses, except for the cases
8193 // listed below. For these exceptions only, listing a predetermined
8194 // variable in a data-sharing attribute clause is allowed and overrides
8195 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008196 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008197 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008198 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8199 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008200 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008201 continue;
8202 }
8203
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008204 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008205 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008206 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008207 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8208 << getOpenMPClauseName(OMPC_private) << Type
8209 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8210 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008211 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008212 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008213 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008214 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008215 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008216 continue;
8217 }
8218
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008219 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8220 // A list item cannot appear in both a map clause and a data-sharing
8221 // attribute clause on the same construct
8222 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008223 if (DSAStack->checkMappableExprComponentListsForDecl(
8224 VD, /* CurrentRegionOnly = */ true,
8225 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8226 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008227 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8228 << getOpenMPClauseName(OMPC_private)
8229 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8230 ReportOriginalDSA(*this, DSAStack, D, DVar);
8231 continue;
8232 }
8233 }
8234
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008235 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8236 // A variable of class type (or array thereof) that appears in a private
8237 // clause requires an accessible, unambiguous default constructor for the
8238 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008239 // Generate helper private variable and initialize it with the default
8240 // value. The address of the original variable is replaced by the address of
8241 // the new private variable in CodeGen. This new variable is not added to
8242 // IdResolver, so the code in the OpenMP region uses original variable for
8243 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008244 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008245 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8246 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008247 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008248 if (VDPrivate->isInvalidDecl())
8249 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008250 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008251 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008252
Alexey Bataev90c228f2016-02-08 09:29:13 +00008253 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008254 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008255 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008256 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008257 Vars.push_back((VD || CurContext->isDependentContext())
8258 ? RefExpr->IgnoreParens()
8259 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008260 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008261 }
8262
Alexey Bataeved09d242014-05-28 05:53:51 +00008263 if (Vars.empty())
8264 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008265
Alexey Bataev03b340a2014-10-21 03:16:40 +00008266 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8267 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008268}
8269
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008270namespace {
8271class DiagsUninitializedSeveretyRAII {
8272private:
8273 DiagnosticsEngine &Diags;
8274 SourceLocation SavedLoc;
8275 bool IsIgnored;
8276
8277public:
8278 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8279 bool IsIgnored)
8280 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8281 if (!IsIgnored) {
8282 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8283 /*Map*/ diag::Severity::Ignored, Loc);
8284 }
8285 }
8286 ~DiagsUninitializedSeveretyRAII() {
8287 if (!IsIgnored)
8288 Diags.popMappings(SavedLoc);
8289 }
8290};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008291}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008292
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008293OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8294 SourceLocation StartLoc,
8295 SourceLocation LParenLoc,
8296 SourceLocation EndLoc) {
8297 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008298 SmallVector<Expr *, 8> PrivateCopies;
8299 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008300 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008301 bool IsImplicitClause =
8302 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8303 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8304
Alexey Bataeved09d242014-05-28 05:53:51 +00008305 for (auto &RefExpr : VarList) {
8306 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008307 SourceLocation ELoc;
8308 SourceRange ERange;
8309 Expr *SimpleRefExpr = RefExpr;
8310 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008311 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008312 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008313 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008314 PrivateCopies.push_back(nullptr);
8315 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008316 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008317 ValueDecl *D = Res.first;
8318 if (!D)
8319 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008320
Alexey Bataev60da77e2016-02-29 05:54:20 +00008321 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008322 QualType Type = D->getType();
8323 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008324
8325 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8326 // A variable that appears in a private clause must not have an incomplete
8327 // type or a reference type.
8328 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008329 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008330 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008331 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008332
8333 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8334 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008335 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008336 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008337 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008338
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008339 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008340 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008341 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008342 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008343 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008344 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008345 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8346 // A list item that specifies a given variable may not appear in more
8347 // than one clause on the same directive, except that a variable may be
8348 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008349 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008350 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008351 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008352 << getOpenMPClauseName(DVar.CKind)
8353 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008354 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008355 continue;
8356 }
8357
8358 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8359 // in a Construct]
8360 // Variables with the predetermined data-sharing attributes may not be
8361 // listed in data-sharing attributes clauses, except for the cases
8362 // listed below. For these exceptions only, listing a predetermined
8363 // variable in a data-sharing attribute clause is allowed and overrides
8364 // the variable's predetermined data-sharing attributes.
8365 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8366 // in a Construct, C/C++, p.2]
8367 // Variables with const-qualified type having no mutable member may be
8368 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008369 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008370 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8371 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008372 << getOpenMPClauseName(DVar.CKind)
8373 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008374 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008375 continue;
8376 }
8377
Alexey Bataevf29276e2014-06-18 04:14:57 +00008378 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008379 // OpenMP [2.9.3.4, Restrictions, p.2]
8380 // A list item that is private within a parallel region must not appear
8381 // in a firstprivate clause on a worksharing construct if any of the
8382 // worksharing regions arising from the worksharing construct ever bind
8383 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008384 if (isOpenMPWorksharingDirective(CurrDir) &&
8385 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008386 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008387 if (DVar.CKind != OMPC_shared &&
8388 (isOpenMPParallelDirective(DVar.DKind) ||
8389 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008390 Diag(ELoc, diag::err_omp_required_access)
8391 << getOpenMPClauseName(OMPC_firstprivate)
8392 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008393 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008394 continue;
8395 }
8396 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008397 // OpenMP [2.9.3.4, Restrictions, p.3]
8398 // A list item that appears in a reduction clause of a parallel construct
8399 // must not appear in a firstprivate clause on a worksharing or task
8400 // construct if any of the worksharing or task regions arising from the
8401 // worksharing or task construct ever bind to any of the parallel regions
8402 // arising from the parallel construct.
8403 // OpenMP [2.9.3.4, Restrictions, p.4]
8404 // A list item that appears in a reduction clause in worksharing
8405 // construct must not appear in a firstprivate clause in a task construct
8406 // encountered during execution of any of the worksharing regions arising
8407 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008408 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008409 DVar = DSAStack->hasInnermostDSA(
8410 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8411 [](OpenMPDirectiveKind K) -> bool {
8412 return isOpenMPParallelDirective(K) ||
8413 isOpenMPWorksharingDirective(K);
8414 },
8415 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008416 if (DVar.CKind == OMPC_reduction &&
8417 (isOpenMPParallelDirective(DVar.DKind) ||
8418 isOpenMPWorksharingDirective(DVar.DKind))) {
8419 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8420 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008421 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008422 continue;
8423 }
8424 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008425
8426 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8427 // A list item that is private within a teams region must not appear in a
8428 // firstprivate clause on a distribute construct if any of the distribute
8429 // regions arising from the distribute construct ever bind to any of the
8430 // teams regions arising from the teams construct.
8431 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8432 // A list item that appears in a reduction clause of a teams construct
8433 // must not appear in a firstprivate clause on a distribute construct if
8434 // any of the distribute regions arising from the distribute construct
8435 // ever bind to any of the teams regions arising from the teams construct.
8436 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8437 // A list item may appear in a firstprivate or lastprivate clause but not
8438 // both.
8439 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008440 DVar = DSAStack->hasInnermostDSA(
8441 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8442 [](OpenMPDirectiveKind K) -> bool {
8443 return isOpenMPTeamsDirective(K);
8444 },
8445 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008446 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8447 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008448 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008449 continue;
8450 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008451 DVar = DSAStack->hasInnermostDSA(
8452 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8453 [](OpenMPDirectiveKind K) -> bool {
8454 return isOpenMPTeamsDirective(K);
8455 },
8456 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008457 if (DVar.CKind == OMPC_reduction &&
8458 isOpenMPTeamsDirective(DVar.DKind)) {
8459 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008460 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008461 continue;
8462 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008463 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008464 if (DVar.CKind == OMPC_lastprivate) {
8465 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008466 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008467 continue;
8468 }
8469 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008470 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8471 // A list item cannot appear in both a map clause and a data-sharing
8472 // attribute clause on the same construct
8473 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008474 if (DSAStack->checkMappableExprComponentListsForDecl(
8475 VD, /* CurrentRegionOnly = */ true,
8476 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8477 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008478 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8479 << getOpenMPClauseName(OMPC_firstprivate)
8480 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8481 ReportOriginalDSA(*this, DSAStack, D, DVar);
8482 continue;
8483 }
8484 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008485 }
8486
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008487 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008488 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008489 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008490 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8491 << getOpenMPClauseName(OMPC_firstprivate) << Type
8492 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8493 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008494 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008495 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008496 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008497 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008498 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008499 continue;
8500 }
8501
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008502 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008503 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8504 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008505 // Generate helper private variable and initialize it with the value of the
8506 // original variable. The address of the original variable is replaced by
8507 // the address of the new private variable in the CodeGen. This new variable
8508 // is not added to IdResolver, so the code in the OpenMP region uses
8509 // original variable for proper diagnostics and variable capturing.
8510 Expr *VDInitRefExpr = nullptr;
8511 // For arrays generate initializer for single element and replace it by the
8512 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008513 if (Type->isArrayType()) {
8514 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008515 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008516 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008517 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008518 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008519 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008520 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008521 InitializedEntity Entity =
8522 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008523 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8524
8525 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8526 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8527 if (Result.isInvalid())
8528 VDPrivate->setInvalidDecl();
8529 else
8530 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008531 // Remove temp variable declaration.
8532 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008533 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008534 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8535 ".firstprivate.temp");
8536 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8537 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008538 AddInitializerToDecl(VDPrivate,
8539 DefaultLvalueConversion(VDInitRefExpr).get(),
8540 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008541 }
8542 if (VDPrivate->isInvalidDecl()) {
8543 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008544 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008545 diag::note_omp_task_predetermined_firstprivate_here);
8546 }
8547 continue;
8548 }
8549 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008550 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008551 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8552 RefExpr->getExprLoc());
8553 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008554 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008555 if (TopDVar.CKind == OMPC_lastprivate)
8556 Ref = TopDVar.PrivateCopy;
8557 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008558 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008559 if (!IsOpenMPCapturedDecl(D))
8560 ExprCaptures.push_back(Ref->getDecl());
8561 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008562 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008563 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008564 Vars.push_back((VD || CurContext->isDependentContext())
8565 ? RefExpr->IgnoreParens()
8566 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008567 PrivateCopies.push_back(VDPrivateRefExpr);
8568 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008569 }
8570
Alexey Bataeved09d242014-05-28 05:53:51 +00008571 if (Vars.empty())
8572 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008573
8574 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008575 Vars, PrivateCopies, Inits,
8576 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008577}
8578
Alexander Musman1bb328c2014-06-04 13:06:39 +00008579OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8580 SourceLocation StartLoc,
8581 SourceLocation LParenLoc,
8582 SourceLocation EndLoc) {
8583 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008584 SmallVector<Expr *, 8> SrcExprs;
8585 SmallVector<Expr *, 8> DstExprs;
8586 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008587 SmallVector<Decl *, 4> ExprCaptures;
8588 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008589 for (auto &RefExpr : VarList) {
8590 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008591 SourceLocation ELoc;
8592 SourceRange ERange;
8593 Expr *SimpleRefExpr = RefExpr;
8594 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008595 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008596 // It will be analyzed later.
8597 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008598 SrcExprs.push_back(nullptr);
8599 DstExprs.push_back(nullptr);
8600 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008601 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008602 ValueDecl *D = Res.first;
8603 if (!D)
8604 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008605
Alexey Bataev74caaf22016-02-20 04:09:36 +00008606 QualType Type = D->getType();
8607 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008608
8609 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8610 // A variable that appears in a lastprivate clause must not have an
8611 // incomplete type or a reference type.
8612 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008613 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008614 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008615 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008616
8617 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8618 // in a Construct]
8619 // Variables with the predetermined data-sharing attributes may not be
8620 // listed in data-sharing attributes clauses, except for the cases
8621 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008622 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008623 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8624 DVar.CKind != OMPC_firstprivate &&
8625 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8626 Diag(ELoc, diag::err_omp_wrong_dsa)
8627 << getOpenMPClauseName(DVar.CKind)
8628 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008629 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008630 continue;
8631 }
8632
Alexey Bataevf29276e2014-06-18 04:14:57 +00008633 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8634 // OpenMP [2.14.3.5, Restrictions, p.2]
8635 // A list item that is private within a parallel region, or that appears in
8636 // the reduction clause of a parallel construct, must not appear in a
8637 // lastprivate clause on a worksharing construct if any of the corresponding
8638 // worksharing regions ever binds to any of the corresponding parallel
8639 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008640 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008641 if (isOpenMPWorksharingDirective(CurrDir) &&
8642 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008643 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008644 if (DVar.CKind != OMPC_shared) {
8645 Diag(ELoc, diag::err_omp_required_access)
8646 << getOpenMPClauseName(OMPC_lastprivate)
8647 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008648 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008649 continue;
8650 }
8651 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008652
8653 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8654 // A list item may appear in a firstprivate or lastprivate clause but not
8655 // both.
8656 if (CurrDir == OMPD_distribute) {
8657 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8658 if (DVar.CKind == OMPC_firstprivate) {
8659 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8660 ReportOriginalDSA(*this, DSAStack, D, DVar);
8661 continue;
8662 }
8663 }
8664
Alexander Musman1bb328c2014-06-04 13:06:39 +00008665 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008666 // A variable of class type (or array thereof) that appears in a
8667 // lastprivate clause requires an accessible, unambiguous default
8668 // constructor for the class type, unless the list item is also specified
8669 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008670 // A variable of class type (or array thereof) that appears in a
8671 // lastprivate clause requires an accessible, unambiguous copy assignment
8672 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008673 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008674 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008675 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008676 D->hasAttrs() ? &D->getAttrs() : nullptr);
8677 auto *PseudoSrcExpr =
8678 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008679 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008680 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008681 D->hasAttrs() ? &D->getAttrs() : nullptr);
8682 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008683 // For arrays generate assignment operation for single element and replace
8684 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008685 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008686 PseudoDstExpr, PseudoSrcExpr);
8687 if (AssignmentOp.isInvalid())
8688 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008689 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008690 /*DiscardedValue=*/true);
8691 if (AssignmentOp.isInvalid())
8692 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008693
Alexey Bataev74caaf22016-02-20 04:09:36 +00008694 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008695 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008696 if (TopDVar.CKind == OMPC_firstprivate)
8697 Ref = TopDVar.PrivateCopy;
8698 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008699 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008700 if (!IsOpenMPCapturedDecl(D))
8701 ExprCaptures.push_back(Ref->getDecl());
8702 }
8703 if (TopDVar.CKind == OMPC_firstprivate ||
8704 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008705 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008706 ExprResult RefRes = DefaultLvalueConversion(Ref);
8707 if (!RefRes.isUsable())
8708 continue;
8709 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008710 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8711 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008712 if (!PostUpdateRes.isUsable())
8713 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008714 ExprPostUpdates.push_back(
8715 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008716 }
8717 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008718 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008719 Vars.push_back((VD || CurContext->isDependentContext())
8720 ? RefExpr->IgnoreParens()
8721 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008722 SrcExprs.push_back(PseudoSrcExpr);
8723 DstExprs.push_back(PseudoDstExpr);
8724 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008725 }
8726
8727 if (Vars.empty())
8728 return nullptr;
8729
8730 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008731 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008732 buildPreInits(Context, ExprCaptures),
8733 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008734}
8735
Alexey Bataev758e55e2013-09-06 18:03:48 +00008736OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8737 SourceLocation StartLoc,
8738 SourceLocation LParenLoc,
8739 SourceLocation EndLoc) {
8740 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008741 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008742 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008743 SourceLocation ELoc;
8744 SourceRange ERange;
8745 Expr *SimpleRefExpr = RefExpr;
8746 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008747 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008748 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008749 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008750 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008751 ValueDecl *D = Res.first;
8752 if (!D)
8753 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008754
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008755 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008756 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8757 // in a Construct]
8758 // Variables with the predetermined data-sharing attributes may not be
8759 // listed in data-sharing attributes clauses, except for the cases
8760 // listed below. For these exceptions only, listing a predetermined
8761 // variable in a data-sharing attribute clause is allowed and overrides
8762 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008763 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008764 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8765 DVar.RefExpr) {
8766 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8767 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008768 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008769 continue;
8770 }
8771
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008772 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008773 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008774 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008775 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008776 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8777 ? RefExpr->IgnoreParens()
8778 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008779 }
8780
Alexey Bataeved09d242014-05-28 05:53:51 +00008781 if (Vars.empty())
8782 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008783
8784 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8785}
8786
Alexey Bataevc5e02582014-06-16 07:08:35 +00008787namespace {
8788class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8789 DSAStackTy *Stack;
8790
8791public:
8792 bool VisitDeclRefExpr(DeclRefExpr *E) {
8793 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008794 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008795 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8796 return false;
8797 if (DVar.CKind != OMPC_unknown)
8798 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008799 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8800 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8801 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008802 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008803 return true;
8804 return false;
8805 }
8806 return false;
8807 }
8808 bool VisitStmt(Stmt *S) {
8809 for (auto Child : S->children()) {
8810 if (Child && Visit(Child))
8811 return true;
8812 }
8813 return false;
8814 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008815 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008816};
Alexey Bataev23b69422014-06-18 07:08:49 +00008817} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008818
Alexey Bataev60da77e2016-02-29 05:54:20 +00008819namespace {
8820// Transform MemberExpression for specified FieldDecl of current class to
8821// DeclRefExpr to specified OMPCapturedExprDecl.
8822class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8823 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8824 ValueDecl *Field;
8825 DeclRefExpr *CapturedExpr;
8826
8827public:
8828 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8829 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8830
8831 ExprResult TransformMemberExpr(MemberExpr *E) {
8832 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8833 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008834 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008835 return CapturedExpr;
8836 }
8837 return BaseTransform::TransformMemberExpr(E);
8838 }
8839 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8840};
8841} // namespace
8842
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008843template <typename T>
8844static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8845 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8846 for (auto &Set : Lookups) {
8847 for (auto *D : Set) {
8848 if (auto Res = Gen(cast<ValueDecl>(D)))
8849 return Res;
8850 }
8851 }
8852 return T();
8853}
8854
8855static ExprResult
8856buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8857 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8858 const DeclarationNameInfo &ReductionId, QualType Ty,
8859 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8860 if (ReductionIdScopeSpec.isInvalid())
8861 return ExprError();
8862 SmallVector<UnresolvedSet<8>, 4> Lookups;
8863 if (S) {
8864 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8865 Lookup.suppressDiagnostics();
8866 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8867 auto *D = Lookup.getRepresentativeDecl();
8868 do {
8869 S = S->getParent();
8870 } while (S && !S->isDeclScope(D));
8871 if (S)
8872 S = S->getParent();
8873 Lookups.push_back(UnresolvedSet<8>());
8874 Lookups.back().append(Lookup.begin(), Lookup.end());
8875 Lookup.clear();
8876 }
8877 } else if (auto *ULE =
8878 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8879 Lookups.push_back(UnresolvedSet<8>());
8880 Decl *PrevD = nullptr;
8881 for(auto *D : ULE->decls()) {
8882 if (D == PrevD)
8883 Lookups.push_back(UnresolvedSet<8>());
8884 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8885 Lookups.back().addDecl(DRD);
8886 PrevD = D;
8887 }
8888 }
8889 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8890 Ty->containsUnexpandedParameterPack() ||
8891 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8892 return !D->isInvalidDecl() &&
8893 (D->getType()->isDependentType() ||
8894 D->getType()->isInstantiationDependentType() ||
8895 D->getType()->containsUnexpandedParameterPack());
8896 })) {
8897 UnresolvedSet<8> ResSet;
8898 for (auto &Set : Lookups) {
8899 ResSet.append(Set.begin(), Set.end());
8900 // The last item marks the end of all declarations at the specified scope.
8901 ResSet.addDecl(Set[Set.size() - 1]);
8902 }
8903 return UnresolvedLookupExpr::Create(
8904 SemaRef.Context, /*NamingClass=*/nullptr,
8905 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8906 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8907 }
8908 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8909 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8910 if (!D->isInvalidDecl() &&
8911 SemaRef.Context.hasSameType(D->getType(), Ty))
8912 return D;
8913 return nullptr;
8914 }))
8915 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8916 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8917 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8918 if (!D->isInvalidDecl() &&
8919 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8920 !Ty.isMoreQualifiedThan(D->getType()))
8921 return D;
8922 return nullptr;
8923 })) {
8924 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8925 /*DetectVirtual=*/false);
8926 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8927 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8928 VD->getType().getUnqualifiedType()))) {
8929 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8930 /*DiagID=*/0) !=
8931 Sema::AR_inaccessible) {
8932 SemaRef.BuildBasePathArray(Paths, BasePath);
8933 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8934 }
8935 }
8936 }
8937 }
8938 if (ReductionIdScopeSpec.isSet()) {
8939 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8940 return ExprError();
8941 }
8942 return ExprEmpty();
8943}
8944
Alexey Bataevc5e02582014-06-16 07:08:35 +00008945OMPClause *Sema::ActOnOpenMPReductionClause(
8946 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8947 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008948 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8949 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008950 auto DN = ReductionId.getName();
8951 auto OOK = DN.getCXXOverloadedOperator();
8952 BinaryOperatorKind BOK = BO_Comma;
8953
8954 // OpenMP [2.14.3.6, reduction clause]
8955 // C
8956 // reduction-identifier is either an identifier or one of the following
8957 // operators: +, -, *, &, |, ^, && and ||
8958 // C++
8959 // reduction-identifier is either an id-expression or one of the following
8960 // operators: +, -, *, &, |, ^, && and ||
8961 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8962 switch (OOK) {
8963 case OO_Plus:
8964 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008965 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008966 break;
8967 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008968 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008969 break;
8970 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008971 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008972 break;
8973 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008974 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008975 break;
8976 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008977 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008978 break;
8979 case OO_AmpAmp:
8980 BOK = BO_LAnd;
8981 break;
8982 case OO_PipePipe:
8983 BOK = BO_LOr;
8984 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008985 case OO_New:
8986 case OO_Delete:
8987 case OO_Array_New:
8988 case OO_Array_Delete:
8989 case OO_Slash:
8990 case OO_Percent:
8991 case OO_Tilde:
8992 case OO_Exclaim:
8993 case OO_Equal:
8994 case OO_Less:
8995 case OO_Greater:
8996 case OO_LessEqual:
8997 case OO_GreaterEqual:
8998 case OO_PlusEqual:
8999 case OO_MinusEqual:
9000 case OO_StarEqual:
9001 case OO_SlashEqual:
9002 case OO_PercentEqual:
9003 case OO_CaretEqual:
9004 case OO_AmpEqual:
9005 case OO_PipeEqual:
9006 case OO_LessLess:
9007 case OO_GreaterGreater:
9008 case OO_LessLessEqual:
9009 case OO_GreaterGreaterEqual:
9010 case OO_EqualEqual:
9011 case OO_ExclaimEqual:
9012 case OO_PlusPlus:
9013 case OO_MinusMinus:
9014 case OO_Comma:
9015 case OO_ArrowStar:
9016 case OO_Arrow:
9017 case OO_Call:
9018 case OO_Subscript:
9019 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009020 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009021 case NUM_OVERLOADED_OPERATORS:
9022 llvm_unreachable("Unexpected reduction identifier");
9023 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009024 if (auto II = DN.getAsIdentifierInfo()) {
9025 if (II->isStr("max"))
9026 BOK = BO_GT;
9027 else if (II->isStr("min"))
9028 BOK = BO_LT;
9029 }
9030 break;
9031 }
9032 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009033 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009034 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009035 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009036
9037 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009038 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009039 SmallVector<Expr *, 8> LHSs;
9040 SmallVector<Expr *, 8> RHSs;
9041 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009042 SmallVector<Decl *, 4> ExprCaptures;
9043 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009044 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9045 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009046 for (auto RefExpr : VarList) {
9047 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009048 // OpenMP [2.1, C/C++]
9049 // A list item is a variable or array section, subject to the restrictions
9050 // specified in Section 2.4 on page 42 and in each of the sections
9051 // describing clauses and directives for which a list appears.
9052 // OpenMP [2.14.3.3, Restrictions, p.1]
9053 // A variable that is part of another variable (as an array or
9054 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009055 if (!FirstIter && IR != ER)
9056 ++IR;
9057 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009058 SourceLocation ELoc;
9059 SourceRange ERange;
9060 Expr *SimpleRefExpr = RefExpr;
9061 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9062 /*AllowArraySection=*/true);
9063 if (Res.second) {
9064 // It will be analyzed later.
9065 Vars.push_back(RefExpr);
9066 Privates.push_back(nullptr);
9067 LHSs.push_back(nullptr);
9068 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009069 // Try to find 'declare reduction' corresponding construct before using
9070 // builtin/overloaded operators.
9071 QualType Type = Context.DependentTy;
9072 CXXCastPath BasePath;
9073 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9074 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9075 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9076 if (CurContext->isDependentContext() &&
9077 (DeclareReductionRef.isUnset() ||
9078 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9079 ReductionOps.push_back(DeclareReductionRef.get());
9080 else
9081 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009082 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009083 ValueDecl *D = Res.first;
9084 if (!D)
9085 continue;
9086
Alexey Bataeva1764212015-09-30 09:22:36 +00009087 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009088 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9089 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9090 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009091 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009092 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009093 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9094 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9095 Type = ATy->getElementType();
9096 else
9097 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009098 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009099 } else
9100 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9101 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009102
Alexey Bataevc5e02582014-06-16 07:08:35 +00009103 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9104 // A variable that appears in a private clause must not have an incomplete
9105 // type or a reference type.
9106 if (RequireCompleteType(ELoc, Type,
9107 diag::err_omp_reduction_incomplete_type))
9108 continue;
9109 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009110 // A list item that appears in a reduction clause must not be
9111 // const-qualified.
9112 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009113 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009114 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009115 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009116 bool IsDecl = !VD ||
9117 VD->isThisDeclarationADefinition(Context) ==
9118 VarDecl::DeclarationOnly;
9119 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009120 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009121 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009122 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009123 continue;
9124 }
9125 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9126 // If a list-item is a reference type then it must bind to the same object
9127 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009128 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009129 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009130 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009131 DSARefChecker Check(DSAStack);
9132 if (Check.Visit(VDDef->getInit())) {
9133 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9134 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9135 continue;
9136 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009137 }
9138 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009139
Alexey Bataevc5e02582014-06-16 07:08:35 +00009140 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9141 // in a Construct]
9142 // Variables with the predetermined data-sharing attributes may not be
9143 // listed in data-sharing attributes clauses, except for the cases
9144 // listed below. For these exceptions only, listing a predetermined
9145 // variable in a data-sharing attribute clause is allowed and overrides
9146 // the variable's predetermined data-sharing attributes.
9147 // OpenMP [2.14.3.6, Restrictions, p.3]
9148 // Any number of reduction clauses can be specified on the directive,
9149 // but a list item can appear only once in the reduction clauses for that
9150 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009151 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009152 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009153 if (DVar.CKind == OMPC_reduction) {
9154 Diag(ELoc, diag::err_omp_once_referenced)
9155 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009156 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009157 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009158 } else if (DVar.CKind != OMPC_unknown) {
9159 Diag(ELoc, diag::err_omp_wrong_dsa)
9160 << getOpenMPClauseName(DVar.CKind)
9161 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009162 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009163 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009164 }
9165
9166 // OpenMP [2.14.3.6, Restrictions, p.1]
9167 // A list item that appears in a reduction clause of a worksharing
9168 // construct must be shared in the parallel regions to which any of the
9169 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009170 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9171 if (isOpenMPWorksharingDirective(CurrDir) &&
9172 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009173 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009174 if (DVar.CKind != OMPC_shared) {
9175 Diag(ELoc, diag::err_omp_required_access)
9176 << getOpenMPClauseName(OMPC_reduction)
9177 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009178 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009179 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009180 }
9181 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009182
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009183 // Try to find 'declare reduction' corresponding construct before using
9184 // builtin/overloaded operators.
9185 CXXCastPath BasePath;
9186 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9187 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9188 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9189 if (DeclareReductionRef.isInvalid())
9190 continue;
9191 if (CurContext->isDependentContext() &&
9192 (DeclareReductionRef.isUnset() ||
9193 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9194 Vars.push_back(RefExpr);
9195 Privates.push_back(nullptr);
9196 LHSs.push_back(nullptr);
9197 RHSs.push_back(nullptr);
9198 ReductionOps.push_back(DeclareReductionRef.get());
9199 continue;
9200 }
9201 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9202 // Not allowed reduction identifier is found.
9203 Diag(ReductionId.getLocStart(),
9204 diag::err_omp_unknown_reduction_identifier)
9205 << Type << ReductionIdRange;
9206 continue;
9207 }
9208
9209 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9210 // The type of a list item that appears in a reduction clause must be valid
9211 // for the reduction-identifier. For a max or min reduction in C, the type
9212 // of the list item must be an allowed arithmetic data type: char, int,
9213 // float, double, or _Bool, possibly modified with long, short, signed, or
9214 // unsigned. For a max or min reduction in C++, the type of the list item
9215 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9216 // double, or bool, possibly modified with long, short, signed, or unsigned.
9217 if (DeclareReductionRef.isUnset()) {
9218 if ((BOK == BO_GT || BOK == BO_LT) &&
9219 !(Type->isScalarType() ||
9220 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9221 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9222 << getLangOpts().CPlusPlus;
9223 if (!ASE && !OASE) {
9224 bool IsDecl = !VD ||
9225 VD->isThisDeclarationADefinition(Context) ==
9226 VarDecl::DeclarationOnly;
9227 Diag(D->getLocation(),
9228 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9229 << D;
9230 }
9231 continue;
9232 }
9233 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9234 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9235 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9236 if (!ASE && !OASE) {
9237 bool IsDecl = !VD ||
9238 VD->isThisDeclarationADefinition(Context) ==
9239 VarDecl::DeclarationOnly;
9240 Diag(D->getLocation(),
9241 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9242 << D;
9243 }
9244 continue;
9245 }
9246 }
9247
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009248 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009249 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009250 D->hasAttrs() ? &D->getAttrs() : nullptr);
9251 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9252 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009253 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009254 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009255 (!ASE &&
9256 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009257 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009258 // Create pseudo array type for private copy. The size for this array will
9259 // be generated during codegen.
9260 // For array subscripts or single variables Private Ty is the same as Type
9261 // (type of the variable or single array element).
9262 PrivateTy = Context.getVariableArrayType(
9263 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9264 Context.getSizeType(), VK_RValue),
9265 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009266 } else if (!ASE && !OASE &&
9267 Context.getAsArrayType(D->getType().getNonReferenceType()))
9268 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009269 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009270 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9271 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009272 // Add initializer for private variable.
9273 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009274 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9275 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9276 if (DeclareReductionRef.isUsable()) {
9277 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9278 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9279 if (DRD->getInitializer()) {
9280 Init = DRDRef;
9281 RHSVD->setInit(DRDRef);
9282 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009283 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009284 } else {
9285 switch (BOK) {
9286 case BO_Add:
9287 case BO_Xor:
9288 case BO_Or:
9289 case BO_LOr:
9290 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9291 if (Type->isScalarType() || Type->isAnyComplexType())
9292 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9293 break;
9294 case BO_Mul:
9295 case BO_LAnd:
9296 if (Type->isScalarType() || Type->isAnyComplexType()) {
9297 // '*' and '&&' reduction ops - initializer is '1'.
9298 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009299 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009300 break;
9301 case BO_And: {
9302 // '&' reduction op - initializer is '~0'.
9303 QualType OrigType = Type;
9304 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9305 Type = ComplexTy->getElementType();
9306 if (Type->isRealFloatingType()) {
9307 llvm::APFloat InitValue =
9308 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9309 /*isIEEE=*/true);
9310 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9311 Type, ELoc);
9312 } else if (Type->isScalarType()) {
9313 auto Size = Context.getTypeSize(Type);
9314 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9315 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9316 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9317 }
9318 if (Init && OrigType->isAnyComplexType()) {
9319 // Init = 0xFFFF + 0xFFFFi;
9320 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9321 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9322 }
9323 Type = OrigType;
9324 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009325 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009326 case BO_LT:
9327 case BO_GT: {
9328 // 'min' reduction op - initializer is 'Largest representable number in
9329 // the reduction list item type'.
9330 // 'max' reduction op - initializer is 'Least representable number in
9331 // the reduction list item type'.
9332 if (Type->isIntegerType() || Type->isPointerType()) {
9333 bool IsSigned = Type->hasSignedIntegerRepresentation();
9334 auto Size = Context.getTypeSize(Type);
9335 QualType IntTy =
9336 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9337 llvm::APInt InitValue =
9338 (BOK != BO_LT)
9339 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9340 : llvm::APInt::getMinValue(Size)
9341 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9342 : llvm::APInt::getMaxValue(Size);
9343 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9344 if (Type->isPointerType()) {
9345 // Cast to pointer type.
9346 auto CastExpr = BuildCStyleCastExpr(
9347 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9348 SourceLocation(), Init);
9349 if (CastExpr.isInvalid())
9350 continue;
9351 Init = CastExpr.get();
9352 }
9353 } else if (Type->isRealFloatingType()) {
9354 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9355 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9356 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9357 Type, ELoc);
9358 }
9359 break;
9360 }
9361 case BO_PtrMemD:
9362 case BO_PtrMemI:
9363 case BO_MulAssign:
9364 case BO_Div:
9365 case BO_Rem:
9366 case BO_Sub:
9367 case BO_Shl:
9368 case BO_Shr:
9369 case BO_LE:
9370 case BO_GE:
9371 case BO_EQ:
9372 case BO_NE:
9373 case BO_AndAssign:
9374 case BO_XorAssign:
9375 case BO_OrAssign:
9376 case BO_Assign:
9377 case BO_AddAssign:
9378 case BO_SubAssign:
9379 case BO_DivAssign:
9380 case BO_RemAssign:
9381 case BO_ShlAssign:
9382 case BO_ShrAssign:
9383 case BO_Comma:
9384 llvm_unreachable("Unexpected reduction operation");
9385 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009386 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009387 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009388 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9389 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009390 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009391 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009392 if (RHSVD->isInvalidDecl())
9393 continue;
9394 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009395 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9396 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009397 bool IsDecl =
9398 !VD ||
9399 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9400 Diag(D->getLocation(),
9401 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9402 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009403 continue;
9404 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009405 // Store initializer for single element in private copy. Will be used during
9406 // codegen.
9407 PrivateVD->setInit(RHSVD->getInit());
9408 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009409 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009410 ExprResult ReductionOp;
9411 if (DeclareReductionRef.isUsable()) {
9412 QualType RedTy = DeclareReductionRef.get()->getType();
9413 QualType PtrRedTy = Context.getPointerType(RedTy);
9414 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9415 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9416 if (!BasePath.empty()) {
9417 LHS = DefaultLvalueConversion(LHS.get());
9418 RHS = DefaultLvalueConversion(RHS.get());
9419 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9420 CK_UncheckedDerivedToBase, LHS.get(),
9421 &BasePath, LHS.get()->getValueKind());
9422 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9423 CK_UncheckedDerivedToBase, RHS.get(),
9424 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009425 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009426 FunctionProtoType::ExtProtoInfo EPI;
9427 QualType Params[] = {PtrRedTy, PtrRedTy};
9428 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9429 auto *OVE = new (Context) OpaqueValueExpr(
9430 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9431 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9432 Expr *Args[] = {LHS.get(), RHS.get()};
9433 ReductionOp = new (Context)
9434 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9435 } else {
9436 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9437 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9438 if (ReductionOp.isUsable()) {
9439 if (BOK != BO_LT && BOK != BO_GT) {
9440 ReductionOp =
9441 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9442 BO_Assign, LHSDRE, ReductionOp.get());
9443 } else {
9444 auto *ConditionalOp = new (Context) ConditionalOperator(
9445 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9446 RHSDRE, Type, VK_LValue, OK_Ordinary);
9447 ReductionOp =
9448 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9449 BO_Assign, LHSDRE, ConditionalOp);
9450 }
9451 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9452 }
9453 if (ReductionOp.isInvalid())
9454 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009455 }
9456
Alexey Bataev60da77e2016-02-29 05:54:20 +00009457 DeclRefExpr *Ref = nullptr;
9458 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009459 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009460 if (ASE || OASE) {
9461 TransformExprToCaptures RebuildToCapture(*this, D);
9462 VarsExpr =
9463 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9464 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009465 } else {
9466 VarsExpr = Ref =
9467 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009468 }
9469 if (!IsOpenMPCapturedDecl(D)) {
9470 ExprCaptures.push_back(Ref->getDecl());
9471 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9472 ExprResult RefRes = DefaultLvalueConversion(Ref);
9473 if (!RefRes.isUsable())
9474 continue;
9475 ExprResult PostUpdateRes =
9476 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9477 SimpleRefExpr, RefRes.get());
9478 if (!PostUpdateRes.isUsable())
9479 continue;
9480 ExprPostUpdates.push_back(
9481 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009482 }
9483 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009484 }
9485 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9486 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009487 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009488 LHSs.push_back(LHSDRE);
9489 RHSs.push_back(RHSDRE);
9490 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009491 }
9492
9493 if (Vars.empty())
9494 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009495
Alexey Bataevc5e02582014-06-16 07:08:35 +00009496 return OMPReductionClause::Create(
9497 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009498 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009499 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9500 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009501}
9502
Alexey Bataevecba70f2016-04-12 11:02:11 +00009503bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9504 SourceLocation LinLoc) {
9505 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9506 LinKind == OMPC_LINEAR_unknown) {
9507 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9508 return true;
9509 }
9510 return false;
9511}
9512
9513bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9514 OpenMPLinearClauseKind LinKind,
9515 QualType Type) {
9516 auto *VD = dyn_cast_or_null<VarDecl>(D);
9517 // A variable must not have an incomplete type or a reference type.
9518 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9519 return true;
9520 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9521 !Type->isReferenceType()) {
9522 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9523 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9524 return true;
9525 }
9526 Type = Type.getNonReferenceType();
9527
9528 // A list item must not be const-qualified.
9529 if (Type.isConstant(Context)) {
9530 Diag(ELoc, diag::err_omp_const_variable)
9531 << getOpenMPClauseName(OMPC_linear);
9532 if (D) {
9533 bool IsDecl =
9534 !VD ||
9535 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9536 Diag(D->getLocation(),
9537 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9538 << D;
9539 }
9540 return true;
9541 }
9542
9543 // A list item must be of integral or pointer type.
9544 Type = Type.getUnqualifiedType().getCanonicalType();
9545 const auto *Ty = Type.getTypePtrOrNull();
9546 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9547 !Ty->isPointerType())) {
9548 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9549 if (D) {
9550 bool IsDecl =
9551 !VD ||
9552 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9553 Diag(D->getLocation(),
9554 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9555 << D;
9556 }
9557 return true;
9558 }
9559 return false;
9560}
9561
Alexey Bataev182227b2015-08-20 10:54:39 +00009562OMPClause *Sema::ActOnOpenMPLinearClause(
9563 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9564 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9565 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009566 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009567 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009568 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009569 SmallVector<Decl *, 4> ExprCaptures;
9570 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009571 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009572 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009573 for (auto &RefExpr : VarList) {
9574 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009575 SourceLocation ELoc;
9576 SourceRange ERange;
9577 Expr *SimpleRefExpr = RefExpr;
9578 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9579 /*AllowArraySection=*/false);
9580 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009581 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009582 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009583 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009584 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009585 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009586 ValueDecl *D = Res.first;
9587 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009588 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009589
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009590 QualType Type = D->getType();
9591 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009592
9593 // OpenMP [2.14.3.7, linear clause]
9594 // A list-item cannot appear in more than one linear clause.
9595 // A list-item that appears in a linear clause cannot appear in any
9596 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009597 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009598 if (DVar.RefExpr) {
9599 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9600 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009601 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009602 continue;
9603 }
9604
Alexey Bataevecba70f2016-04-12 11:02:11 +00009605 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009606 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009607 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009608
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009609 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009610 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9611 D->hasAttrs() ? &D->getAttrs() : nullptr);
9612 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009613 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009614 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009615 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009616 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009617 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009618 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9619 if (!IsOpenMPCapturedDecl(D)) {
9620 ExprCaptures.push_back(Ref->getDecl());
9621 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9622 ExprResult RefRes = DefaultLvalueConversion(Ref);
9623 if (!RefRes.isUsable())
9624 continue;
9625 ExprResult PostUpdateRes =
9626 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9627 SimpleRefExpr, RefRes.get());
9628 if (!PostUpdateRes.isUsable())
9629 continue;
9630 ExprPostUpdates.push_back(
9631 IgnoredValueConversions(PostUpdateRes.get()).get());
9632 }
9633 }
9634 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009635 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009636 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009637 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009638 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009639 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009640 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9641 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9642
9643 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009644 Vars.push_back((VD || CurContext->isDependentContext())
9645 ? RefExpr->IgnoreParens()
9646 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009647 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009648 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009649 }
9650
9651 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009652 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009653
9654 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009655 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009656 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9657 !Step->isInstantiationDependent() &&
9658 !Step->containsUnexpandedParameterPack()) {
9659 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009660 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009661 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009662 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009663 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009664
Alexander Musman3276a272015-03-21 10:12:56 +00009665 // Build var to save the step value.
9666 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009667 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009668 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009669 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009670 ExprResult CalcStep =
9671 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009672 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009673
Alexander Musman8dba6642014-04-22 13:09:42 +00009674 // Warn about zero linear step (it would be probably better specified as
9675 // making corresponding variables 'const').
9676 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009677 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9678 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009679 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9680 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009681 if (!IsConstant && CalcStep.isUsable()) {
9682 // Calculate the step beforehand instead of doing this on each iteration.
9683 // (This is not used if the number of iterations may be kfold-ed).
9684 CalcStepExpr = CalcStep.get();
9685 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009686 }
9687
Alexey Bataev182227b2015-08-20 10:54:39 +00009688 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9689 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009690 StepExpr, CalcStepExpr,
9691 buildPreInits(Context, ExprCaptures),
9692 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009693}
9694
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009695static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9696 Expr *NumIterations, Sema &SemaRef,
9697 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009698 // Walk the vars and build update/final expressions for the CodeGen.
9699 SmallVector<Expr *, 8> Updates;
9700 SmallVector<Expr *, 8> Finals;
9701 Expr *Step = Clause.getStep();
9702 Expr *CalcStep = Clause.getCalcStep();
9703 // OpenMP [2.14.3.7, linear clause]
9704 // If linear-step is not specified it is assumed to be 1.
9705 if (Step == nullptr)
9706 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009707 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009708 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009709 }
Alexander Musman3276a272015-03-21 10:12:56 +00009710 bool HasErrors = false;
9711 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009712 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009713 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009714 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009715 SourceLocation ELoc;
9716 SourceRange ERange;
9717 Expr *SimpleRefExpr = RefExpr;
9718 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9719 /*AllowArraySection=*/false);
9720 ValueDecl *D = Res.first;
9721 if (Res.second || !D) {
9722 Updates.push_back(nullptr);
9723 Finals.push_back(nullptr);
9724 HasErrors = true;
9725 continue;
9726 }
9727 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9728 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9729 ->getMemberDecl();
9730 }
9731 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009732 Expr *InitExpr = *CurInit;
9733
9734 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009735 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009736 Expr *CapturedRef;
9737 if (LinKind == OMPC_LINEAR_uval)
9738 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9739 else
9740 CapturedRef =
9741 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9742 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9743 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009744
9745 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009746 ExprResult Update;
9747 if (!Info.first) {
9748 Update =
9749 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9750 InitExpr, IV, Step, /* Subtract */ false);
9751 } else
9752 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009753 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9754 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009755
9756 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009757 ExprResult Final;
9758 if (!Info.first) {
9759 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9760 InitExpr, NumIterations, Step,
9761 /* Subtract */ false);
9762 } else
9763 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009764 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9765 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009766
Alexander Musman3276a272015-03-21 10:12:56 +00009767 if (!Update.isUsable() || !Final.isUsable()) {
9768 Updates.push_back(nullptr);
9769 Finals.push_back(nullptr);
9770 HasErrors = true;
9771 } else {
9772 Updates.push_back(Update.get());
9773 Finals.push_back(Final.get());
9774 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009775 ++CurInit;
9776 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009777 }
9778 Clause.setUpdates(Updates);
9779 Clause.setFinals(Finals);
9780 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009781}
9782
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009783OMPClause *Sema::ActOnOpenMPAlignedClause(
9784 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9785 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9786
9787 SmallVector<Expr *, 8> Vars;
9788 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009789 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9790 SourceLocation ELoc;
9791 SourceRange ERange;
9792 Expr *SimpleRefExpr = RefExpr;
9793 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9794 /*AllowArraySection=*/false);
9795 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009796 // It will be analyzed later.
9797 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009798 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009799 ValueDecl *D = Res.first;
9800 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009801 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009802
Alexey Bataev1efd1662016-03-29 10:59:56 +00009803 QualType QType = D->getType();
9804 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009805
9806 // OpenMP [2.8.1, simd construct, Restrictions]
9807 // The type of list items appearing in the aligned clause must be
9808 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009809 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009810 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009811 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009812 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009813 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009814 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009815 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009816 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009817 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009818 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009819 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009820 continue;
9821 }
9822
9823 // OpenMP [2.8.1, simd construct, Restrictions]
9824 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009825 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009826 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009827 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9828 << getOpenMPClauseName(OMPC_aligned);
9829 continue;
9830 }
9831
Alexey Bataev1efd1662016-03-29 10:59:56 +00009832 DeclRefExpr *Ref = nullptr;
9833 if (!VD && IsOpenMPCapturedDecl(D))
9834 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9835 Vars.push_back(DefaultFunctionArrayConversion(
9836 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9837 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009838 }
9839
9840 // OpenMP [2.8.1, simd construct, Description]
9841 // The parameter of the aligned clause, alignment, must be a constant
9842 // positive integer expression.
9843 // If no optional parameter is specified, implementation-defined default
9844 // alignments for SIMD instructions on the target platforms are assumed.
9845 if (Alignment != nullptr) {
9846 ExprResult AlignResult =
9847 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9848 if (AlignResult.isInvalid())
9849 return nullptr;
9850 Alignment = AlignResult.get();
9851 }
9852 if (Vars.empty())
9853 return nullptr;
9854
9855 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9856 EndLoc, Vars, Alignment);
9857}
9858
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009859OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9860 SourceLocation StartLoc,
9861 SourceLocation LParenLoc,
9862 SourceLocation EndLoc) {
9863 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009864 SmallVector<Expr *, 8> SrcExprs;
9865 SmallVector<Expr *, 8> DstExprs;
9866 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009867 for (auto &RefExpr : VarList) {
9868 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9869 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009870 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009871 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009872 SrcExprs.push_back(nullptr);
9873 DstExprs.push_back(nullptr);
9874 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009875 continue;
9876 }
9877
Alexey Bataeved09d242014-05-28 05:53:51 +00009878 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009879 // OpenMP [2.1, C/C++]
9880 // A list item is a variable name.
9881 // OpenMP [2.14.4.1, Restrictions, p.1]
9882 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009883 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009884 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009885 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9886 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009887 continue;
9888 }
9889
9890 Decl *D = DE->getDecl();
9891 VarDecl *VD = cast<VarDecl>(D);
9892
9893 QualType Type = VD->getType();
9894 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9895 // It will be analyzed later.
9896 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009897 SrcExprs.push_back(nullptr);
9898 DstExprs.push_back(nullptr);
9899 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009900 continue;
9901 }
9902
9903 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9904 // A list item that appears in a copyin clause must be threadprivate.
9905 if (!DSAStack->isThreadPrivate(VD)) {
9906 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009907 << getOpenMPClauseName(OMPC_copyin)
9908 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009909 continue;
9910 }
9911
9912 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9913 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009914 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009915 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009916 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009917 auto *SrcVD =
9918 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9919 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009920 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009921 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9922 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009923 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9924 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009925 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009926 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009927 // For arrays generate assignment operation for single element and replace
9928 // it by the original array element in CodeGen.
9929 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9930 PseudoDstExpr, PseudoSrcExpr);
9931 if (AssignmentOp.isInvalid())
9932 continue;
9933 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9934 /*DiscardedValue=*/true);
9935 if (AssignmentOp.isInvalid())
9936 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009937
9938 DSAStack->addDSA(VD, DE, OMPC_copyin);
9939 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009940 SrcExprs.push_back(PseudoSrcExpr);
9941 DstExprs.push_back(PseudoDstExpr);
9942 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009943 }
9944
Alexey Bataeved09d242014-05-28 05:53:51 +00009945 if (Vars.empty())
9946 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009947
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009948 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9949 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009950}
9951
Alexey Bataevbae9a792014-06-27 10:37:06 +00009952OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9953 SourceLocation StartLoc,
9954 SourceLocation LParenLoc,
9955 SourceLocation EndLoc) {
9956 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009957 SmallVector<Expr *, 8> SrcExprs;
9958 SmallVector<Expr *, 8> DstExprs;
9959 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009960 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009961 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9962 SourceLocation ELoc;
9963 SourceRange ERange;
9964 Expr *SimpleRefExpr = RefExpr;
9965 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9966 /*AllowArraySection=*/false);
9967 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009968 // It will be analyzed later.
9969 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009970 SrcExprs.push_back(nullptr);
9971 DstExprs.push_back(nullptr);
9972 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009973 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009974 ValueDecl *D = Res.first;
9975 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009976 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009977
Alexey Bataeve122da12016-03-17 10:50:17 +00009978 QualType Type = D->getType();
9979 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009980
9981 // OpenMP [2.14.4.2, Restrictions, p.2]
9982 // A list item that appears in a copyprivate clause may not appear in a
9983 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009984 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9985 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009986 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9987 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009988 Diag(ELoc, diag::err_omp_wrong_dsa)
9989 << getOpenMPClauseName(DVar.CKind)
9990 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009991 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009992 continue;
9993 }
9994
9995 // OpenMP [2.11.4.2, Restrictions, p.1]
9996 // All list items that appear in a copyprivate clause must be either
9997 // threadprivate or private in the enclosing context.
9998 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009999 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010000 if (DVar.CKind == OMPC_shared) {
10001 Diag(ELoc, diag::err_omp_required_access)
10002 << getOpenMPClauseName(OMPC_copyprivate)
10003 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000010004 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010005 continue;
10006 }
10007 }
10008 }
10009
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010010 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010011 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010012 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010013 << getOpenMPClauseName(OMPC_copyprivate) << Type
10014 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010015 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000010016 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010017 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010018 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010019 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010020 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010021 continue;
10022 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010023
Alexey Bataevbae9a792014-06-27 10:37:06 +000010024 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10025 // A variable of class type (or array thereof) that appears in a
10026 // copyin clause requires an accessible, unambiguous copy assignment
10027 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010028 Type = Context.getBaseElementType(Type.getNonReferenceType())
10029 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010030 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010031 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10032 D->hasAttrs() ? &D->getAttrs() : nullptr);
10033 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010034 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010035 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10036 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010037 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010038 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10039 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010040 PseudoDstExpr, PseudoSrcExpr);
10041 if (AssignmentOp.isInvalid())
10042 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010043 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010044 /*DiscardedValue=*/true);
10045 if (AssignmentOp.isInvalid())
10046 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010047
10048 // No need to mark vars as copyprivate, they are already threadprivate or
10049 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010050 assert(VD || IsOpenMPCapturedDecl(D));
10051 Vars.push_back(
10052 VD ? RefExpr->IgnoreParens()
10053 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010054 SrcExprs.push_back(PseudoSrcExpr);
10055 DstExprs.push_back(PseudoDstExpr);
10056 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010057 }
10058
10059 if (Vars.empty())
10060 return nullptr;
10061
Alexey Bataeva63048e2015-03-23 06:18:07 +000010062 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10063 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010064}
10065
Alexey Bataev6125da92014-07-21 11:26:11 +000010066OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10067 SourceLocation StartLoc,
10068 SourceLocation LParenLoc,
10069 SourceLocation EndLoc) {
10070 if (VarList.empty())
10071 return nullptr;
10072
10073 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10074}
Alexey Bataevdea47612014-07-23 07:46:59 +000010075
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010076OMPClause *
10077Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10078 SourceLocation DepLoc, SourceLocation ColonLoc,
10079 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10080 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010081 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010082 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010083 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010084 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010085 return nullptr;
10086 }
10087 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010088 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10089 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010090 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010091 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010092 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10093 /*Last=*/OMPC_DEPEND_unknown, Except)
10094 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010095 return nullptr;
10096 }
10097 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010098 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010099 llvm::APSInt DepCounter(/*BitWidth=*/32);
10100 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10101 if (DepKind == OMPC_DEPEND_sink) {
10102 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10103 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10104 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010105 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010106 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010107 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10108 DSAStack->getParentOrderedRegionParam()) {
10109 for (auto &RefExpr : VarList) {
10110 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010111 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010112 // It will be analyzed later.
10113 Vars.push_back(RefExpr);
10114 continue;
10115 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010116
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010117 SourceLocation ELoc = RefExpr->getExprLoc();
10118 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10119 if (DepKind == OMPC_DEPEND_sink) {
10120 if (DepCounter >= TotalDepCount) {
10121 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10122 continue;
10123 }
10124 ++DepCounter;
10125 // OpenMP [2.13.9, Summary]
10126 // depend(dependence-type : vec), where dependence-type is:
10127 // 'sink' and where vec is the iteration vector, which has the form:
10128 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10129 // where n is the value specified by the ordered clause in the loop
10130 // directive, xi denotes the loop iteration variable of the i-th nested
10131 // loop associated with the loop directive, and di is a constant
10132 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010133 if (CurContext->isDependentContext()) {
10134 // It will be analyzed later.
10135 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010136 continue;
10137 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010138 SimpleExpr = SimpleExpr->IgnoreImplicit();
10139 OverloadedOperatorKind OOK = OO_None;
10140 SourceLocation OOLoc;
10141 Expr *LHS = SimpleExpr;
10142 Expr *RHS = nullptr;
10143 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10144 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10145 OOLoc = BO->getOperatorLoc();
10146 LHS = BO->getLHS()->IgnoreParenImpCasts();
10147 RHS = BO->getRHS()->IgnoreParenImpCasts();
10148 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10149 OOK = OCE->getOperator();
10150 OOLoc = OCE->getOperatorLoc();
10151 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10152 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10153 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10154 OOK = MCE->getMethodDecl()
10155 ->getNameInfo()
10156 .getName()
10157 .getCXXOverloadedOperator();
10158 OOLoc = MCE->getCallee()->getExprLoc();
10159 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10160 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10161 }
10162 SourceLocation ELoc;
10163 SourceRange ERange;
10164 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10165 /*AllowArraySection=*/false);
10166 if (Res.second) {
10167 // It will be analyzed later.
10168 Vars.push_back(RefExpr);
10169 }
10170 ValueDecl *D = Res.first;
10171 if (!D)
10172 continue;
10173
10174 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10175 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10176 continue;
10177 }
10178 if (RHS) {
10179 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10180 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10181 if (RHSRes.isInvalid())
10182 continue;
10183 }
10184 if (!CurContext->isDependentContext() &&
10185 DSAStack->getParentOrderedRegionParam() &&
10186 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10187 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10188 << DSAStack->getParentLoopControlVariable(
10189 DepCounter.getZExtValue());
10190 continue;
10191 }
10192 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010193 } else {
10194 // OpenMP [2.11.1.1, Restrictions, p.3]
10195 // A variable that is part of another variable (such as a field of a
10196 // structure) but is not an array element or an array section cannot
10197 // appear in a depend clause.
10198 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10199 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10200 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10201 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10202 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010203 (ASE &&
10204 !ASE->getBase()
10205 ->getType()
10206 .getNonReferenceType()
10207 ->isPointerType() &&
10208 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010209 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10210 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010211 continue;
10212 }
10213 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010214 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10215 }
10216
10217 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10218 TotalDepCount > VarList.size() &&
10219 DSAStack->getParentOrderedRegionParam()) {
10220 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10221 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10222 }
10223 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10224 Vars.empty())
10225 return nullptr;
10226 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010227 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10228 DepKind, DepLoc, ColonLoc, Vars);
10229 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10230 DSAStack->addDoacrossDependClause(C, OpsOffs);
10231 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010232}
Michael Wonge710d542015-08-07 16:16:36 +000010233
10234OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10235 SourceLocation LParenLoc,
10236 SourceLocation EndLoc) {
10237 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010238
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010239 // OpenMP [2.9.1, Restrictions]
10240 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010241 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10242 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010243 return nullptr;
10244
Michael Wonge710d542015-08-07 16:16:36 +000010245 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10246}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010247
10248static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10249 DSAStackTy *Stack, CXXRecordDecl *RD) {
10250 if (!RD || RD->isInvalidDecl())
10251 return true;
10252
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010253 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10254 if (auto *CTD = CTSD->getSpecializedTemplate())
10255 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010256 auto QTy = SemaRef.Context.getRecordType(RD);
10257 if (RD->isDynamicClass()) {
10258 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10259 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10260 return false;
10261 }
10262 auto *DC = RD;
10263 bool IsCorrect = true;
10264 for (auto *I : DC->decls()) {
10265 if (I) {
10266 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10267 if (MD->isStatic()) {
10268 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10269 SemaRef.Diag(MD->getLocation(),
10270 diag::note_omp_static_member_in_target);
10271 IsCorrect = false;
10272 }
10273 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10274 if (VD->isStaticDataMember()) {
10275 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10276 SemaRef.Diag(VD->getLocation(),
10277 diag::note_omp_static_member_in_target);
10278 IsCorrect = false;
10279 }
10280 }
10281 }
10282 }
10283
10284 for (auto &I : RD->bases()) {
10285 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10286 I.getType()->getAsCXXRecordDecl()))
10287 IsCorrect = false;
10288 }
10289 return IsCorrect;
10290}
10291
10292static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10293 DSAStackTy *Stack, QualType QTy) {
10294 NamedDecl *ND;
10295 if (QTy->isIncompleteType(&ND)) {
10296 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10297 return false;
10298 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10299 if (!RD->isInvalidDecl() &&
10300 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10301 return false;
10302 }
10303 return true;
10304}
10305
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010306/// \brief Return true if it can be proven that the provided array expression
10307/// (array section or array subscript) does NOT specify the whole size of the
10308/// array whose base type is \a BaseQTy.
10309static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10310 const Expr *E,
10311 QualType BaseQTy) {
10312 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10313
10314 // If this is an array subscript, it refers to the whole size if the size of
10315 // the dimension is constant and equals 1. Also, an array section assumes the
10316 // format of an array subscript if no colon is used.
10317 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10318 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10319 return ATy->getSize().getSExtValue() != 1;
10320 // Size can't be evaluated statically.
10321 return false;
10322 }
10323
10324 assert(OASE && "Expecting array section if not an array subscript.");
10325 auto *LowerBound = OASE->getLowerBound();
10326 auto *Length = OASE->getLength();
10327
10328 // If there is a lower bound that does not evaluates to zero, we are not
10329 // convering the whole dimension.
10330 if (LowerBound) {
10331 llvm::APSInt ConstLowerBound;
10332 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10333 return false; // Can't get the integer value as a constant.
10334 if (ConstLowerBound.getSExtValue())
10335 return true;
10336 }
10337
10338 // If we don't have a length we covering the whole dimension.
10339 if (!Length)
10340 return false;
10341
10342 // If the base is a pointer, we don't have a way to get the size of the
10343 // pointee.
10344 if (BaseQTy->isPointerType())
10345 return false;
10346
10347 // We can only check if the length is the same as the size of the dimension
10348 // if we have a constant array.
10349 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10350 if (!CATy)
10351 return false;
10352
10353 llvm::APSInt ConstLength;
10354 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10355 return false; // Can't get the integer value as a constant.
10356
10357 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10358}
10359
10360// Return true if it can be proven that the provided array expression (array
10361// section or array subscript) does NOT specify a single element of the array
10362// whose base type is \a BaseQTy.
10363static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10364 const Expr *E,
10365 QualType BaseQTy) {
10366 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10367
10368 // An array subscript always refer to a single element. Also, an array section
10369 // assumes the format of an array subscript if no colon is used.
10370 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10371 return false;
10372
10373 assert(OASE && "Expecting array section if not an array subscript.");
10374 auto *Length = OASE->getLength();
10375
10376 // If we don't have a length we have to check if the array has unitary size
10377 // for this dimension. Also, we should always expect a length if the base type
10378 // is pointer.
10379 if (!Length) {
10380 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10381 return ATy->getSize().getSExtValue() != 1;
10382 // We cannot assume anything.
10383 return false;
10384 }
10385
10386 // Check if the length evaluates to 1.
10387 llvm::APSInt ConstLength;
10388 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10389 return false; // Can't get the integer value as a constant.
10390
10391 return ConstLength.getSExtValue() != 1;
10392}
10393
Samuel Antao661c0902016-05-26 17:39:58 +000010394// Return the expression of the base of the mappable expression or null if it
10395// cannot be determined and do all the necessary checks to see if the expression
10396// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010397// components of the expression.
10398static Expr *CheckMapClauseExpressionBase(
10399 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010400 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10401 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010402 SourceLocation ELoc = E->getExprLoc();
10403 SourceRange ERange = E->getSourceRange();
10404
10405 // The base of elements of list in a map clause have to be either:
10406 // - a reference to variable or field.
10407 // - a member expression.
10408 // - an array expression.
10409 //
10410 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10411 // reference to 'r'.
10412 //
10413 // If we have:
10414 //
10415 // struct SS {
10416 // Bla S;
10417 // foo() {
10418 // #pragma omp target map (S.Arr[:12]);
10419 // }
10420 // }
10421 //
10422 // We want to retrieve the member expression 'this->S';
10423
10424 Expr *RelevantExpr = nullptr;
10425
Samuel Antao5de996e2016-01-22 20:21:36 +000010426 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10427 // If a list item is an array section, it must specify contiguous storage.
10428 //
10429 // For this restriction it is sufficient that we make sure only references
10430 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010431 // exist except in the rightmost expression (unless they cover the whole
10432 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010433 //
10434 // r.ArrS[3:5].Arr[6:7]
10435 //
10436 // r.ArrS[3:5].x
10437 //
10438 // but these would be valid:
10439 // r.ArrS[3].Arr[6:7]
10440 //
10441 // r.ArrS[3].x
10442
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010443 bool AllowUnitySizeArraySection = true;
10444 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010445
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010446 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010447 E = E->IgnoreParenImpCasts();
10448
10449 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10450 if (!isa<VarDecl>(CurE->getDecl()))
10451 break;
10452
10453 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010454
10455 // If we got a reference to a declaration, we should not expect any array
10456 // section before that.
10457 AllowUnitySizeArraySection = false;
10458 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010459
10460 // Record the component.
10461 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10462 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010463 continue;
10464 }
10465
10466 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10467 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10468
10469 if (isa<CXXThisExpr>(BaseE))
10470 // We found a base expression: this->Val.
10471 RelevantExpr = CurE;
10472 else
10473 E = BaseE;
10474
10475 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10476 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10477 << CurE->getSourceRange();
10478 break;
10479 }
10480
10481 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10482
10483 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10484 // A bit-field cannot appear in a map clause.
10485 //
10486 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010487 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10488 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010489 break;
10490 }
10491
10492 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10493 // If the type of a list item is a reference to a type T then the type
10494 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010495 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010496
10497 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10498 // A list item cannot be a variable that is a member of a structure with
10499 // a union type.
10500 //
10501 if (auto *RT = CurType->getAs<RecordType>())
10502 if (RT->isUnionType()) {
10503 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10504 << CurE->getSourceRange();
10505 break;
10506 }
10507
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010508 // If we got a member expression, we should not expect any array section
10509 // before that:
10510 //
10511 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10512 // If a list item is an element of a structure, only the rightmost symbol
10513 // of the variable reference can be an array section.
10514 //
10515 AllowUnitySizeArraySection = false;
10516 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010517
10518 // Record the component.
10519 CurComponents.push_back(
10520 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010521 continue;
10522 }
10523
10524 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10525 E = CurE->getBase()->IgnoreParenImpCasts();
10526
10527 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10528 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10529 << 0 << CurE->getSourceRange();
10530 break;
10531 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010532
10533 // If we got an array subscript that express the whole dimension we
10534 // can have any array expressions before. If it only expressing part of
10535 // the dimension, we can only have unitary-size array expressions.
10536 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10537 E->getType()))
10538 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010539
10540 // Record the component - we don't have any declaration associated.
10541 CurComponents.push_back(
10542 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010543 continue;
10544 }
10545
10546 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010547 E = CurE->getBase()->IgnoreParenImpCasts();
10548
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010549 auto CurType =
10550 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10551
Samuel Antao5de996e2016-01-22 20:21:36 +000010552 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10553 // If the type of a list item is a reference to a type T then the type
10554 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010555 if (CurType->isReferenceType())
10556 CurType = CurType->getPointeeType();
10557
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010558 bool IsPointer = CurType->isAnyPointerType();
10559
10560 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010561 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10562 << 0 << CurE->getSourceRange();
10563 break;
10564 }
10565
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010566 bool NotWhole =
10567 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10568 bool NotUnity =
10569 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10570
Samuel Antaodab51bb2016-07-18 23:22:11 +000010571 if (AllowWholeSizeArraySection) {
10572 // Any array section is currently allowed. Allowing a whole size array
10573 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010574 //
10575 // If this array section refers to the whole dimension we can still
10576 // accept other array sections before this one, except if the base is a
10577 // pointer. Otherwise, only unitary sections are accepted.
10578 if (NotWhole || IsPointer)
10579 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000010580 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010581 // A unity or whole array section is not allowed and that is not
10582 // compatible with the properties of the current array section.
10583 SemaRef.Diag(
10584 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10585 << CurE->getSourceRange();
10586 break;
10587 }
Samuel Antao90927002016-04-26 14:54:23 +000010588
10589 // Record the component - we don't have any declaration associated.
10590 CurComponents.push_back(
10591 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010592 continue;
10593 }
10594
10595 // If nothing else worked, this is not a valid map clause expression.
10596 SemaRef.Diag(ELoc,
10597 diag::err_omp_expected_named_var_member_or_array_expression)
10598 << ERange;
10599 break;
10600 }
10601
10602 return RelevantExpr;
10603}
10604
10605// Return true if expression E associated with value VD has conflicts with other
10606// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010607static bool CheckMapConflicts(
10608 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10609 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010610 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10611 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010612 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010613 SourceLocation ELoc = E->getExprLoc();
10614 SourceRange ERange = E->getSourceRange();
10615
10616 // In order to easily check the conflicts we need to match each component of
10617 // the expression under test with the components of the expressions that are
10618 // already in the stack.
10619
Samuel Antao5de996e2016-01-22 20:21:36 +000010620 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010621 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010622 "Map clause expression with unexpected base!");
10623
10624 // Variables to help detecting enclosing problems in data environment nests.
10625 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010626 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010627
Samuel Antao90927002016-04-26 14:54:23 +000010628 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10629 VD, CurrentRegionOnly,
10630 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10631 StackComponents) -> bool {
10632
Samuel Antao5de996e2016-01-22 20:21:36 +000010633 assert(!StackComponents.empty() &&
10634 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010635 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010636 "Map clause expression with unexpected base!");
10637
Samuel Antao90927002016-04-26 14:54:23 +000010638 // The whole expression in the stack.
10639 auto *RE = StackComponents.front().getAssociatedExpression();
10640
Samuel Antao5de996e2016-01-22 20:21:36 +000010641 // Expressions must start from the same base. Here we detect at which
10642 // point both expressions diverge from each other and see if we can
10643 // detect if the memory referred to both expressions is contiguous and
10644 // do not overlap.
10645 auto CI = CurComponents.rbegin();
10646 auto CE = CurComponents.rend();
10647 auto SI = StackComponents.rbegin();
10648 auto SE = StackComponents.rend();
10649 for (; CI != CE && SI != SE; ++CI, ++SI) {
10650
10651 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10652 // At most one list item can be an array item derived from a given
10653 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010654 if (CurrentRegionOnly &&
10655 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10656 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10657 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10658 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10659 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010660 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010661 << CI->getAssociatedExpression()->getSourceRange();
10662 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10663 diag::note_used_here)
10664 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010665 return true;
10666 }
10667
10668 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010669 if (CI->getAssociatedExpression()->getStmtClass() !=
10670 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010671 break;
10672
10673 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010674 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010675 break;
10676 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000010677 // Check if the extra components of the expressions in the enclosing
10678 // data environment are redundant for the current base declaration.
10679 // If they are, the maps completely overlap, which is legal.
10680 for (; SI != SE; ++SI) {
10681 QualType Type;
10682 if (auto *ASE =
10683 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
10684 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
10685 } else if (auto *OASE =
10686 dyn_cast<OMPArraySectionExpr>(SI->getAssociatedExpression())) {
10687 auto *E = OASE->getBase()->IgnoreParenImpCasts();
10688 Type =
10689 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10690 }
10691 if (Type.isNull() || Type->isAnyPointerType() ||
10692 CheckArrayExpressionDoesNotReferToWholeSize(
10693 SemaRef, SI->getAssociatedExpression(), Type))
10694 break;
10695 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010696
10697 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10698 // List items of map clauses in the same construct must not share
10699 // original storage.
10700 //
10701 // If the expressions are exactly the same or one is a subset of the
10702 // other, it means they are sharing storage.
10703 if (CI == CE && SI == SE) {
10704 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010705 if (CKind == OMPC_map)
10706 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10707 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010708 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010709 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10710 << ERange;
10711 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010712 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10713 << RE->getSourceRange();
10714 return true;
10715 } else {
10716 // If we find the same expression in the enclosing data environment,
10717 // that is legal.
10718 IsEnclosedByDataEnvironmentExpr = true;
10719 return false;
10720 }
10721 }
10722
Samuel Antao90927002016-04-26 14:54:23 +000010723 QualType DerivedType =
10724 std::prev(CI)->getAssociatedDeclaration()->getType();
10725 SourceLocation DerivedLoc =
10726 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010727
10728 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10729 // If the type of a list item is a reference to a type T then the type
10730 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010731 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010732
10733 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10734 // A variable for which the type is pointer and an array section
10735 // derived from that variable must not appear as list items of map
10736 // clauses of the same construct.
10737 //
10738 // Also, cover one of the cases in:
10739 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10740 // If any part of the original storage of a list item has corresponding
10741 // storage in the device data environment, all of the original storage
10742 // must have corresponding storage in the device data environment.
10743 //
10744 if (DerivedType->isAnyPointerType()) {
10745 if (CI == CE || SI == SE) {
10746 SemaRef.Diag(
10747 DerivedLoc,
10748 diag::err_omp_pointer_mapped_along_with_derived_section)
10749 << DerivedLoc;
10750 } else {
10751 assert(CI != CE && SI != SE);
10752 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10753 << DerivedLoc;
10754 }
10755 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10756 << RE->getSourceRange();
10757 return true;
10758 }
10759
10760 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10761 // List items of map clauses in the same construct must not share
10762 // original storage.
10763 //
10764 // An expression is a subset of the other.
10765 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010766 if (CKind == OMPC_map)
10767 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10768 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010769 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010770 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10771 << ERange;
10772 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010773 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10774 << RE->getSourceRange();
10775 return true;
10776 }
10777
10778 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010779 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010780 if (!CurrentRegionOnly && SI != SE)
10781 EnclosingExpr = RE;
10782
10783 // The current expression is a subset of the expression in the data
10784 // environment.
10785 IsEnclosedByDataEnvironmentExpr |=
10786 (!CurrentRegionOnly && CI != CE && SI == SE);
10787
10788 return false;
10789 });
10790
10791 if (CurrentRegionOnly)
10792 return FoundError;
10793
10794 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10795 // If any part of the original storage of a list item has corresponding
10796 // storage in the device data environment, all of the original storage must
10797 // have corresponding storage in the device data environment.
10798 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10799 // If a list item is an element of a structure, and a different element of
10800 // the structure has a corresponding list item in the device data environment
10801 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010802 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010803 // data environment prior to the task encountering the construct.
10804 //
10805 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10806 SemaRef.Diag(ELoc,
10807 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10808 << ERange;
10809 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10810 << EnclosingExpr->getSourceRange();
10811 return true;
10812 }
10813
10814 return FoundError;
10815}
10816
Samuel Antao661c0902016-05-26 17:39:58 +000010817namespace {
10818// Utility struct that gathers all the related lists associated with a mappable
10819// expression.
10820struct MappableVarListInfo final {
10821 // The list of expressions.
10822 ArrayRef<Expr *> VarList;
10823 // The list of processed expressions.
10824 SmallVector<Expr *, 16> ProcessedVarList;
10825 // The mappble components for each expression.
10826 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10827 // The base declaration of the variable.
10828 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10829
10830 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10831 // We have a list of components and base declarations for each entry in the
10832 // variable list.
10833 VarComponents.reserve(VarList.size());
10834 VarBaseDeclarations.reserve(VarList.size());
10835 }
10836};
10837}
10838
10839// Check the validity of the provided variable list for the provided clause kind
10840// \a CKind. In the check process the valid expressions, and mappable expression
10841// components and variables are extracted and used to fill \a Vars,
10842// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10843// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10844static void
10845checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10846 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10847 SourceLocation StartLoc,
10848 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10849 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010850 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10851 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010852 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010853
Samuel Antao90927002016-04-26 14:54:23 +000010854 // Keep track of the mappable components and base declarations in this clause.
10855 // Each entry in the list is going to have a list of components associated. We
10856 // record each set of the components so that we can build the clause later on.
10857 // In the end we should have the same amount of declarations and component
10858 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010859
Samuel Antao661c0902016-05-26 17:39:58 +000010860 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010861 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010862 SourceLocation ELoc = RE->getExprLoc();
10863
Kelvin Li0bff7af2015-11-23 05:32:03 +000010864 auto *VE = RE->IgnoreParenLValueCasts();
10865
10866 if (VE->isValueDependent() || VE->isTypeDependent() ||
10867 VE->isInstantiationDependent() ||
10868 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010869 // We can only analyze this information once the missing information is
10870 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000010871 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010872 continue;
10873 }
10874
10875 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010876
Samuel Antao5de996e2016-01-22 20:21:36 +000010877 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010878 SemaRef.Diag(ELoc,
10879 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000010880 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010881 continue;
10882 }
10883
Samuel Antao90927002016-04-26 14:54:23 +000010884 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10885 ValueDecl *CurDeclaration = nullptr;
10886
10887 // Obtain the array or member expression bases if required. Also, fill the
10888 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000010889 auto *BE =
10890 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010891 if (!BE)
10892 continue;
10893
Samuel Antao90927002016-04-26 14:54:23 +000010894 assert(!CurComponents.empty() &&
10895 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010896
Samuel Antao90927002016-04-26 14:54:23 +000010897 // For the following checks, we rely on the base declaration which is
10898 // expected to be associated with the last component. The declaration is
10899 // expected to be a variable or a field (if 'this' is being mapped).
10900 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10901 assert(CurDeclaration && "Null decl on map clause.");
10902 assert(
10903 CurDeclaration->isCanonicalDecl() &&
10904 "Expecting components to have associated only canonical declarations.");
10905
10906 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10907 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010908
10909 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010910 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010911
10912 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000010913 // threadprivate variables cannot appear in a map clause.
10914 // OpenMP 4.5 [2.10.5, target update Construct]
10915 // threadprivate variables cannot appear in a from clause.
10916 if (VD && DSAS->isThreadPrivate(VD)) {
10917 auto DVar = DSAS->getTopDSA(VD, false);
10918 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10919 << getOpenMPClauseName(CKind);
10920 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010921 continue;
10922 }
10923
Samuel Antao5de996e2016-01-22 20:21:36 +000010924 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10925 // A list item cannot appear in both a map clause and a data-sharing
10926 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010927
Samuel Antao5de996e2016-01-22 20:21:36 +000010928 // Check conflicts with other map clause expressions. We check the conflicts
10929 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000010930 // environment, because the restrictions are different. We only have to
10931 // check conflicts across regions for the map clauses.
10932 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10933 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010934 break;
Samuel Antao661c0902016-05-26 17:39:58 +000010935 if (CKind == OMPC_map &&
10936 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10937 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010938 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010939
Samuel Antao661c0902016-05-26 17:39:58 +000010940 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000010941 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10942 // If the type of a list item is a reference to a type T then the type will
10943 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010944 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010945
Samuel Antao661c0902016-05-26 17:39:58 +000010946 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10947 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000010948 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010949 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000010950 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10951 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010952 continue;
10953
Samuel Antao661c0902016-05-26 17:39:58 +000010954 if (CKind == OMPC_map) {
10955 // target enter data
10956 // OpenMP [2.10.2, Restrictions, p. 99]
10957 // A map-type must be specified in all map clauses and must be either
10958 // to or alloc.
10959 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10960 if (DKind == OMPD_target_enter_data &&
10961 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10962 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10963 << (IsMapTypeImplicit ? 1 : 0)
10964 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10965 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010966 continue;
10967 }
Samuel Antao661c0902016-05-26 17:39:58 +000010968
10969 // target exit_data
10970 // OpenMP [2.10.3, Restrictions, p. 102]
10971 // A map-type must be specified in all map clauses and must be either
10972 // from, release, or delete.
10973 if (DKind == OMPD_target_exit_data &&
10974 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10975 MapType == OMPC_MAP_delete)) {
10976 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10977 << (IsMapTypeImplicit ? 1 : 0)
10978 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10979 << getOpenMPDirectiveName(DKind);
10980 continue;
10981 }
10982
10983 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10984 // A list item cannot appear in both a map clause and a data-sharing
10985 // attribute clause on the same construct
10986 if (DKind == OMPD_target && VD) {
10987 auto DVar = DSAS->getTopDSA(VD, false);
10988 if (isOpenMPPrivate(DVar.CKind)) {
10989 SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10990 << getOpenMPClauseName(DVar.CKind)
10991 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
10992 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
10993 continue;
10994 }
10995 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010996 }
10997
Samuel Antao90927002016-04-26 14:54:23 +000010998 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000010999 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000011000
11001 // Store the components in the stack so that they can be used to check
11002 // against other clauses later on.
Samuel Antao661c0902016-05-26 17:39:58 +000011003 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
Samuel Antao90927002016-04-26 14:54:23 +000011004
11005 // Save the components and declaration to create the clause. For purposes of
11006 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000011007 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000011008 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11009 MVLI.VarComponents.back().append(CurComponents.begin(),
11010 CurComponents.end());
11011 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11012 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011013 }
Samuel Antao661c0902016-05-26 17:39:58 +000011014}
11015
11016OMPClause *
11017Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11018 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11019 SourceLocation MapLoc, SourceLocation ColonLoc,
11020 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11021 SourceLocation LParenLoc, SourceLocation EndLoc) {
11022 MappableVarListInfo MVLI(VarList);
11023 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11024 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011025
Samuel Antao5de996e2016-01-22 20:21:36 +000011026 // We need to produce a map clause even if we don't have variables so that
11027 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000011028 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11029 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11030 MVLI.VarComponents, MapTypeModifier, MapType,
11031 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011032}
Kelvin Li099bb8c2015-11-24 20:50:12 +000011033
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011034QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11035 TypeResult ParsedType) {
11036 assert(ParsedType.isUsable());
11037
11038 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11039 if (ReductionType.isNull())
11040 return QualType();
11041
11042 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11043 // A type name in a declare reduction directive cannot be a function type, an
11044 // array type, a reference type, or a type qualified with const, volatile or
11045 // restrict.
11046 if (ReductionType.hasQualifiers()) {
11047 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11048 return QualType();
11049 }
11050
11051 if (ReductionType->isFunctionType()) {
11052 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11053 return QualType();
11054 }
11055 if (ReductionType->isReferenceType()) {
11056 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11057 return QualType();
11058 }
11059 if (ReductionType->isArrayType()) {
11060 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11061 return QualType();
11062 }
11063 return ReductionType;
11064}
11065
11066Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11067 Scope *S, DeclContext *DC, DeclarationName Name,
11068 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11069 AccessSpecifier AS, Decl *PrevDeclInScope) {
11070 SmallVector<Decl *, 8> Decls;
11071 Decls.reserve(ReductionTypes.size());
11072
11073 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11074 ForRedeclaration);
11075 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11076 // A reduction-identifier may not be re-declared in the current scope for the
11077 // same type or for a type that is compatible according to the base language
11078 // rules.
11079 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11080 OMPDeclareReductionDecl *PrevDRD = nullptr;
11081 bool InCompoundScope = true;
11082 if (S != nullptr) {
11083 // Find previous declaration with the same name not referenced in other
11084 // declarations.
11085 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11086 InCompoundScope =
11087 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11088 LookupName(Lookup, S);
11089 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11090 /*AllowInlineNamespace=*/false);
11091 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11092 auto Filter = Lookup.makeFilter();
11093 while (Filter.hasNext()) {
11094 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11095 if (InCompoundScope) {
11096 auto I = UsedAsPrevious.find(PrevDecl);
11097 if (I == UsedAsPrevious.end())
11098 UsedAsPrevious[PrevDecl] = false;
11099 if (auto *D = PrevDecl->getPrevDeclInScope())
11100 UsedAsPrevious[D] = true;
11101 }
11102 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11103 PrevDecl->getLocation();
11104 }
11105 Filter.done();
11106 if (InCompoundScope) {
11107 for (auto &PrevData : UsedAsPrevious) {
11108 if (!PrevData.second) {
11109 PrevDRD = PrevData.first;
11110 break;
11111 }
11112 }
11113 }
11114 } else if (PrevDeclInScope != nullptr) {
11115 auto *PrevDRDInScope = PrevDRD =
11116 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11117 do {
11118 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11119 PrevDRDInScope->getLocation();
11120 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11121 } while (PrevDRDInScope != nullptr);
11122 }
11123 for (auto &TyData : ReductionTypes) {
11124 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11125 bool Invalid = false;
11126 if (I != PreviousRedeclTypes.end()) {
11127 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11128 << TyData.first;
11129 Diag(I->second, diag::note_previous_definition);
11130 Invalid = true;
11131 }
11132 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11133 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11134 Name, TyData.first, PrevDRD);
11135 DC->addDecl(DRD);
11136 DRD->setAccess(AS);
11137 Decls.push_back(DRD);
11138 if (Invalid)
11139 DRD->setInvalidDecl();
11140 else
11141 PrevDRD = DRD;
11142 }
11143
11144 return DeclGroupPtrTy::make(
11145 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11146}
11147
11148void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11149 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11150
11151 // Enter new function scope.
11152 PushFunctionScope();
11153 getCurFunction()->setHasBranchProtectedScope();
11154 getCurFunction()->setHasOMPDeclareReductionCombiner();
11155
11156 if (S != nullptr)
11157 PushDeclContext(S, DRD);
11158 else
11159 CurContext = DRD;
11160
11161 PushExpressionEvaluationContext(PotentiallyEvaluated);
11162
11163 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011164 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11165 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11166 // uses semantics of argument handles by value, but it should be passed by
11167 // reference. C lang does not support references, so pass all parameters as
11168 // pointers.
11169 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011170 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011171 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011172 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11173 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11174 // uses semantics of argument handles by value, but it should be passed by
11175 // reference. C lang does not support references, so pass all parameters as
11176 // pointers.
11177 // Create 'T omp_out;' variable.
11178 auto *OmpOutParm =
11179 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11180 if (S != nullptr) {
11181 PushOnScopeChains(OmpInParm, S);
11182 PushOnScopeChains(OmpOutParm, S);
11183 } else {
11184 DRD->addDecl(OmpInParm);
11185 DRD->addDecl(OmpOutParm);
11186 }
11187}
11188
11189void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11190 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11191 DiscardCleanupsInEvaluationContext();
11192 PopExpressionEvaluationContext();
11193
11194 PopDeclContext();
11195 PopFunctionScopeInfo();
11196
11197 if (Combiner != nullptr)
11198 DRD->setCombiner(Combiner);
11199 else
11200 DRD->setInvalidDecl();
11201}
11202
11203void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11204 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11205
11206 // Enter new function scope.
11207 PushFunctionScope();
11208 getCurFunction()->setHasBranchProtectedScope();
11209
11210 if (S != nullptr)
11211 PushDeclContext(S, DRD);
11212 else
11213 CurContext = DRD;
11214
11215 PushExpressionEvaluationContext(PotentiallyEvaluated);
11216
11217 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011218 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11219 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11220 // uses semantics of argument handles by value, but it should be passed by
11221 // reference. C lang does not support references, so pass all parameters as
11222 // pointers.
11223 // Create 'T omp_priv;' variable.
11224 auto *OmpPrivParm =
11225 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011226 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11227 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11228 // uses semantics of argument handles by value, but it should be passed by
11229 // reference. C lang does not support references, so pass all parameters as
11230 // pointers.
11231 // Create 'T omp_orig;' variable.
11232 auto *OmpOrigParm =
11233 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011234 if (S != nullptr) {
11235 PushOnScopeChains(OmpPrivParm, S);
11236 PushOnScopeChains(OmpOrigParm, S);
11237 } else {
11238 DRD->addDecl(OmpPrivParm);
11239 DRD->addDecl(OmpOrigParm);
11240 }
11241}
11242
11243void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11244 Expr *Initializer) {
11245 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11246 DiscardCleanupsInEvaluationContext();
11247 PopExpressionEvaluationContext();
11248
11249 PopDeclContext();
11250 PopFunctionScopeInfo();
11251
11252 if (Initializer != nullptr)
11253 DRD->setInitializer(Initializer);
11254 else
11255 DRD->setInvalidDecl();
11256}
11257
11258Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11259 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11260 for (auto *D : DeclReductions.get()) {
11261 if (IsValid) {
11262 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11263 if (S != nullptr)
11264 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11265 } else
11266 D->setInvalidDecl();
11267 }
11268 return DeclReductions;
11269}
11270
Kelvin Li099bb8c2015-11-24 20:50:12 +000011271OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11272 SourceLocation StartLoc,
11273 SourceLocation LParenLoc,
11274 SourceLocation EndLoc) {
11275 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011276
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011277 // OpenMP [teams Constrcut, Restrictions]
11278 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011279 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11280 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011281 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011282
11283 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11284}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011285
11286OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11287 SourceLocation StartLoc,
11288 SourceLocation LParenLoc,
11289 SourceLocation EndLoc) {
11290 Expr *ValExpr = ThreadLimit;
11291
11292 // OpenMP [teams Constrcut, Restrictions]
11293 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011294 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11295 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011296 return nullptr;
11297
11298 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11299 EndLoc);
11300}
Alexey Bataeva0569352015-12-01 10:17:31 +000011301
11302OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11303 SourceLocation StartLoc,
11304 SourceLocation LParenLoc,
11305 SourceLocation EndLoc) {
11306 Expr *ValExpr = Priority;
11307
11308 // OpenMP [2.9.1, task Constrcut]
11309 // The priority-value is a non-negative numerical scalar expression.
11310 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11311 /*StrictlyPositive=*/false))
11312 return nullptr;
11313
11314 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11315}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011316
11317OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11318 SourceLocation StartLoc,
11319 SourceLocation LParenLoc,
11320 SourceLocation EndLoc) {
11321 Expr *ValExpr = Grainsize;
11322
11323 // OpenMP [2.9.2, taskloop Constrcut]
11324 // The parameter of the grainsize clause must be a positive integer
11325 // expression.
11326 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11327 /*StrictlyPositive=*/true))
11328 return nullptr;
11329
11330 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11331}
Alexey Bataev382967a2015-12-08 12:06:20 +000011332
11333OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11334 SourceLocation StartLoc,
11335 SourceLocation LParenLoc,
11336 SourceLocation EndLoc) {
11337 Expr *ValExpr = NumTasks;
11338
11339 // OpenMP [2.9.2, taskloop Constrcut]
11340 // The parameter of the num_tasks clause must be a positive integer
11341 // expression.
11342 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11343 /*StrictlyPositive=*/true))
11344 return nullptr;
11345
11346 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11347}
11348
Alexey Bataev28c75412015-12-15 08:19:24 +000011349OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11350 SourceLocation LParenLoc,
11351 SourceLocation EndLoc) {
11352 // OpenMP [2.13.2, critical construct, Description]
11353 // ... where hint-expression is an integer constant expression that evaluates
11354 // to a valid lock hint.
11355 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11356 if (HintExpr.isInvalid())
11357 return nullptr;
11358 return new (Context)
11359 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11360}
11361
Carlo Bertollib4adf552016-01-15 18:50:31 +000011362OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11363 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11364 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11365 SourceLocation EndLoc) {
11366 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11367 std::string Values;
11368 Values += "'";
11369 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11370 Values += "'";
11371 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11372 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11373 return nullptr;
11374 }
11375 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011376 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011377 if (ChunkSize) {
11378 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11379 !ChunkSize->isInstantiationDependent() &&
11380 !ChunkSize->containsUnexpandedParameterPack()) {
11381 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11382 ExprResult Val =
11383 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11384 if (Val.isInvalid())
11385 return nullptr;
11386
11387 ValExpr = Val.get();
11388
11389 // OpenMP [2.7.1, Restrictions]
11390 // chunk_size must be a loop invariant integer expression with a positive
11391 // value.
11392 llvm::APSInt Result;
11393 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11394 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11395 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11396 << "dist_schedule" << ChunkSize->getSourceRange();
11397 return nullptr;
11398 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011399 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11400 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011401 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11402 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11403 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011404 }
11405 }
11406 }
11407
11408 return new (Context)
11409 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011410 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011411}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011412
11413OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11414 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11415 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11416 SourceLocation KindLoc, SourceLocation EndLoc) {
11417 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11418 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11419 Kind != OMPC_DEFAULTMAP_scalar) {
11420 std::string Value;
11421 SourceLocation Loc;
11422 Value += "'";
11423 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11424 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11425 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11426 Loc = MLoc;
11427 } else {
11428 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11429 OMPC_DEFAULTMAP_scalar);
11430 Loc = KindLoc;
11431 }
11432 Value += "'";
11433 Diag(Loc, diag::err_omp_unexpected_clause_value)
11434 << Value << getOpenMPClauseName(OMPC_defaultmap);
11435 return nullptr;
11436 }
11437
11438 return new (Context)
11439 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11440}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011441
11442bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11443 DeclContext *CurLexicalContext = getCurLexicalContext();
11444 if (!CurLexicalContext->isFileContext() &&
11445 !CurLexicalContext->isExternCContext() &&
11446 !CurLexicalContext->isExternCXXContext()) {
11447 Diag(Loc, diag::err_omp_region_not_file_context);
11448 return false;
11449 }
11450 if (IsInOpenMPDeclareTargetContext) {
11451 Diag(Loc, diag::err_omp_enclosed_declare_target);
11452 return false;
11453 }
11454
11455 IsInOpenMPDeclareTargetContext = true;
11456 return true;
11457}
11458
11459void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11460 assert(IsInOpenMPDeclareTargetContext &&
11461 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11462
11463 IsInOpenMPDeclareTargetContext = false;
11464}
11465
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011466void
11467Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11468 const DeclarationNameInfo &Id,
11469 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11470 NamedDeclSetType &SameDirectiveDecls) {
11471 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11472 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11473
11474 if (Lookup.isAmbiguous())
11475 return;
11476 Lookup.suppressDiagnostics();
11477
11478 if (!Lookup.isSingleResult()) {
11479 if (TypoCorrection Corrected =
11480 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11481 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11482 CTK_ErrorRecovery)) {
11483 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11484 << Id.getName());
11485 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11486 return;
11487 }
11488
11489 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11490 return;
11491 }
11492
11493 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11494 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11495 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11496 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11497
11498 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11499 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11500 ND->addAttr(A);
11501 if (ASTMutationListener *ML = Context.getASTMutationListener())
11502 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11503 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11504 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11505 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11506 << Id.getName();
11507 }
11508 } else
11509 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11510}
11511
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011512static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11513 Sema &SemaRef, Decl *D) {
11514 if (!D)
11515 return;
11516 Decl *LD = nullptr;
11517 if (isa<TagDecl>(D)) {
11518 LD = cast<TagDecl>(D)->getDefinition();
11519 } else if (isa<VarDecl>(D)) {
11520 LD = cast<VarDecl>(D)->getDefinition();
11521
11522 // If this is an implicit variable that is legal and we do not need to do
11523 // anything.
11524 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011525 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11526 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11527 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011528 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011529 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011530 return;
11531 }
11532
11533 } else if (isa<FunctionDecl>(D)) {
11534 const FunctionDecl *FD = nullptr;
11535 if (cast<FunctionDecl>(D)->hasBody(FD))
11536 LD = const_cast<FunctionDecl *>(FD);
11537
11538 // If the definition is associated with the current declaration in the
11539 // target region (it can be e.g. a lambda) that is legal and we do not need
11540 // to do anything else.
11541 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011542 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11543 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11544 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011545 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011546 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011547 return;
11548 }
11549 }
11550 if (!LD)
11551 LD = D;
11552 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11553 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11554 // Outlined declaration is not declared target.
11555 if (LD->isOutOfLine()) {
11556 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11557 SemaRef.Diag(SL, diag::note_used_here) << SR;
11558 } else {
11559 DeclContext *DC = LD->getDeclContext();
11560 while (DC) {
11561 if (isa<FunctionDecl>(DC) &&
11562 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11563 break;
11564 DC = DC->getParent();
11565 }
11566 if (DC)
11567 return;
11568
11569 // Is not declared in target context.
11570 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11571 SemaRef.Diag(SL, diag::note_used_here) << SR;
11572 }
11573 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011574 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11575 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11576 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011577 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011578 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011579 }
11580}
11581
11582static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11583 Sema &SemaRef, DSAStackTy *Stack,
11584 ValueDecl *VD) {
11585 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11586 return true;
11587 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11588 return false;
11589 return true;
11590}
11591
11592void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11593 if (!D || D->isInvalidDecl())
11594 return;
11595 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11596 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11597 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11598 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11599 if (DSAStack->isThreadPrivate(VD)) {
11600 Diag(SL, diag::err_omp_threadprivate_in_target);
11601 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11602 return;
11603 }
11604 }
11605 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11606 // Problem if any with var declared with incomplete type will be reported
11607 // as normal, so no need to check it here.
11608 if ((E || !VD->getType()->isIncompleteType()) &&
11609 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11610 // Mark decl as declared target to prevent further diagnostic.
11611 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011612 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11613 Context, OMPDeclareTargetDeclAttr::MT_To);
11614 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011615 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011616 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011617 }
11618 return;
11619 }
11620 }
11621 if (!E) {
11622 // Checking declaration inside declare target region.
11623 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11624 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011625 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11626 Context, OMPDeclareTargetDeclAttr::MT_To);
11627 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011628 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011629 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011630 }
11631 return;
11632 }
11633 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11634}
Samuel Antao661c0902016-05-26 17:39:58 +000011635
11636OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11637 SourceLocation StartLoc,
11638 SourceLocation LParenLoc,
11639 SourceLocation EndLoc) {
11640 MappableVarListInfo MVLI(VarList);
11641 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11642 if (MVLI.ProcessedVarList.empty())
11643 return nullptr;
11644
11645 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11646 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11647 MVLI.VarComponents);
11648}
Samuel Antaoec172c62016-05-26 17:49:04 +000011649
11650OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11651 SourceLocation StartLoc,
11652 SourceLocation LParenLoc,
11653 SourceLocation EndLoc) {
11654 MappableVarListInfo MVLI(VarList);
11655 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11656 if (MVLI.ProcessedVarList.empty())
11657 return nullptr;
11658
11659 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11660 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11661 MVLI.VarComponents);
11662}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011663
11664OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11665 SourceLocation StartLoc,
11666 SourceLocation LParenLoc,
11667 SourceLocation EndLoc) {
11668 SmallVector<Expr *, 8> Vars;
11669 for (auto &RefExpr : VarList) {
11670 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11671 SourceLocation ELoc;
11672 SourceRange ERange;
11673 Expr *SimpleRefExpr = RefExpr;
11674 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11675 if (Res.second) {
11676 // It will be analyzed later.
11677 Vars.push_back(RefExpr);
11678 }
11679 ValueDecl *D = Res.first;
11680 if (!D)
11681 continue;
11682
11683 QualType Type = D->getType();
11684 // item should be a pointer or reference to pointer
11685 if (!Type.getNonReferenceType()->isPointerType()) {
11686 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11687 << 0 << RefExpr->getSourceRange();
11688 continue;
11689 }
11690 Vars.push_back(RefExpr->IgnoreParens());
11691 }
11692
11693 if (Vars.empty())
11694 return nullptr;
11695
11696 return OMPUseDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11697 Vars);
11698}
Carlo Bertolli70594e92016-07-13 17:16:49 +000011699
11700OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11701 SourceLocation StartLoc,
11702 SourceLocation LParenLoc,
11703 SourceLocation EndLoc) {
11704 SmallVector<Expr *, 8> Vars;
11705 for (auto &RefExpr : VarList) {
11706 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11707 SourceLocation ELoc;
11708 SourceRange ERange;
11709 Expr *SimpleRefExpr = RefExpr;
11710 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11711 if (Res.second) {
11712 // It will be analyzed later.
11713 Vars.push_back(RefExpr);
11714 }
11715 ValueDecl *D = Res.first;
11716 if (!D)
11717 continue;
11718
11719 QualType Type = D->getType();
11720 // item should be a pointer or array or reference to pointer or array
11721 if (!Type.getNonReferenceType()->isPointerType() &&
11722 !Type.getNonReferenceType()->isArrayType()) {
11723 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11724 << 0 << RefExpr->getSourceRange();
11725 continue;
11726 }
11727 Vars.push_back(RefExpr->IgnoreParens());
11728 }
11729
11730 if (Vars.empty())
11731 return nullptr;
11732
11733 return OMPIsDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11734 Vars);
11735}