blob: 14029e20c3435f8f3b51b55e558cac3d79548cd8 [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 Li986330c2016-07-20 22:57:10 +00001613 case OMPD_target_parallel_for_simd:
1614 case OMPD_target_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001615 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001616 std::make_pair(StringRef(), QualType()) // __context with shared vars
1617 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001618 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1619 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001620 break;
1621 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001622 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001623 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001624 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1625 FunctionProtoType::ExtProtoInfo EPI;
1626 EPI.Variadic = true;
1627 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001628 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001629 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001630 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1631 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1632 std::make_pair(".copy_fn.",
1633 Context.getPointerType(CopyFnType).withConst()),
1634 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001635 std::make_pair(StringRef(), QualType()) // __context with shared vars
1636 };
1637 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1638 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001639 // Mark this captured region as inlined, because we don't use outlined
1640 // function directly.
1641 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1642 AlwaysInlineAttr::CreateImplicit(
1643 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001644 break;
1645 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001646 case OMPD_taskloop:
1647 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001648 QualType KmpInt32Ty =
1649 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1650 QualType KmpUInt64Ty =
1651 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1652 QualType KmpInt64Ty =
1653 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1654 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1655 FunctionProtoType::ExtProtoInfo EPI;
1656 EPI.Variadic = true;
1657 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001658 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001659 std::make_pair(".global_tid.", KmpInt32Ty),
1660 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1661 std::make_pair(".privates.",
1662 Context.VoidPtrTy.withConst().withRestrict()),
1663 std::make_pair(
1664 ".copy_fn.",
1665 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1666 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1667 std::make_pair(".lb.", KmpUInt64Ty),
1668 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1669 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001670 std::make_pair(StringRef(), QualType()) // __context with shared vars
1671 };
1672 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1673 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001674 // Mark this captured region as inlined, because we don't use outlined
1675 // function directly.
1676 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1677 AlwaysInlineAttr::CreateImplicit(
1678 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001679 break;
1680 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001681 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001682 case OMPD_distribute_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001683 case OMPD_distribute_parallel_for: {
1684 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1685 QualType KmpInt32PtrTy =
1686 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1687 Sema::CapturedParamNameType Params[] = {
1688 std::make_pair(".global_tid.", KmpInt32PtrTy),
1689 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1690 std::make_pair(".previous.lb.", Context.getSizeType()),
1691 std::make_pair(".previous.ub.", Context.getSizeType()),
1692 std::make_pair(StringRef(), QualType()) // __context with shared vars
1693 };
1694 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1695 Params);
1696 break;
1697 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001698 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001699 case OMPD_taskyield:
1700 case OMPD_barrier:
1701 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001702 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001703 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001704 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001705 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001706 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001707 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001708 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001709 case OMPD_declare_target:
1710 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001711 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001712 llvm_unreachable("OpenMP Directive is not allowed");
1713 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001714 llvm_unreachable("Unknown OpenMP directive");
1715 }
1716}
1717
Alexey Bataev3392d762016-02-16 11:18:12 +00001718static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001719 Expr *CaptureExpr, bool WithInit,
1720 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001721 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001722 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001723 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001724 QualType Ty = Init->getType();
1725 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1726 if (S.getLangOpts().CPlusPlus)
1727 Ty = C.getLValueReferenceType(Ty);
1728 else {
1729 Ty = C.getPointerType(Ty);
1730 ExprResult Res =
1731 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1732 if (!Res.isUsable())
1733 return nullptr;
1734 Init = Res.get();
1735 }
Alexey Bataev61205072016-03-02 04:57:40 +00001736 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001737 }
1738 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001739 if (!WithInit)
1740 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001741 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001742 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1743 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001744 return CED;
1745}
1746
Alexey Bataev61205072016-03-02 04:57:40 +00001747static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1748 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001749 OMPCapturedExprDecl *CD;
1750 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1751 CD = cast<OMPCapturedExprDecl>(VD);
1752 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001753 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1754 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001755 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001756 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001757}
1758
Alexey Bataev5a3af132016-03-29 08:58:54 +00001759static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1760 if (!Ref) {
1761 auto *CD =
1762 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1763 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1764 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1765 CaptureExpr->getExprLoc());
1766 }
1767 ExprResult Res = Ref;
1768 if (!S.getLangOpts().CPlusPlus &&
1769 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1770 Ref->getType()->isPointerType())
1771 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1772 if (!Res.isUsable())
1773 return ExprError();
1774 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001775}
1776
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001777StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1778 ArrayRef<OMPClause *> Clauses) {
1779 if (!S.isUsable()) {
1780 ActOnCapturedRegionError();
1781 return StmtError();
1782 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001783
1784 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001785 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001786 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001787 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001788 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001789 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001790 Clause->getClauseKind() == OMPC_copyprivate ||
1791 (getLangOpts().OpenMPUseTLS &&
1792 getASTContext().getTargetInfo().isTLSSupported() &&
1793 Clause->getClauseKind() == OMPC_copyin)) {
1794 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001795 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001796 for (auto *VarRef : Clause->children()) {
1797 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001798 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001799 }
1800 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001801 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001802 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001803 // Mark all variables in private list clauses as used in inner region.
1804 // Required for proper codegen of combined directives.
1805 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001806 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001807 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1808 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001809 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1810 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001811 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001812 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1813 if (auto *E = C->getPostUpdateExpr())
1814 MarkDeclarationsReferencedInExpr(E);
1815 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001816 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001817 if (Clause->getClauseKind() == OMPC_schedule)
1818 SC = cast<OMPScheduleClause>(Clause);
1819 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001820 OC = cast<OMPOrderedClause>(Clause);
1821 else if (Clause->getClauseKind() == OMPC_linear)
1822 LCs.push_back(cast<OMPLinearClause>(Clause));
1823 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001824 bool ErrorFound = false;
1825 // OpenMP, 2.7.1 Loop Construct, Restrictions
1826 // The nonmonotonic modifier cannot be specified if an ordered clause is
1827 // specified.
1828 if (SC &&
1829 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1830 SC->getSecondScheduleModifier() ==
1831 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1832 OC) {
1833 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1834 ? SC->getFirstScheduleModifierLoc()
1835 : SC->getSecondScheduleModifierLoc(),
1836 diag::err_omp_schedule_nonmonotonic_ordered)
1837 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1838 ErrorFound = true;
1839 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001840 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1841 for (auto *C : LCs) {
1842 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1843 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1844 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001845 ErrorFound = true;
1846 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001847 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1848 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1849 OC->getNumForLoops()) {
1850 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1851 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1852 ErrorFound = true;
1853 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001854 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001855 ActOnCapturedRegionError();
1856 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001857 }
1858 return ActOnCapturedRegionEnd(S.get());
1859}
1860
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001861static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1862 OpenMPDirectiveKind CurrentRegion,
1863 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001864 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001865 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001866 // Allowed nesting of constructs
1867 // +------------------+-----------------+------------------------------------+
1868 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1869 // +------------------+-----------------+------------------------------------+
1870 // | parallel | parallel | * |
1871 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001872 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001873 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001874 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001875 // | parallel | simd | * |
1876 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001877 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001878 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001879 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001880 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001881 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001882 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001883 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001884 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001885 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001886 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001887 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001888 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001889 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001890 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001891 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001892 // | parallel | target parallel | * |
1893 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001894 // | parallel | target enter | * |
1895 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001896 // | parallel | target exit | * |
1897 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001898 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001899 // | parallel | cancellation | |
1900 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001901 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001902 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001903 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001904 // | parallel | distribute | + |
1905 // | parallel | distribute | + |
1906 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001907 // | parallel | distribute | + |
1908 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001909 // | parallel | distribute simd | + |
Kelvin Li986330c2016-07-20 22:57:10 +00001910 // | parallel | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001911 // +------------------+-----------------+------------------------------------+
1912 // | for | parallel | * |
1913 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001914 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001915 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001916 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001917 // | for | simd | * |
1918 // | for | sections | + |
1919 // | for | section | + |
1920 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001921 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001922 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001923 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001924 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001925 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001926 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001927 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001928 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001929 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001930 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001931 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001932 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001933 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001934 // | for | target parallel | * |
1935 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001936 // | for | target enter | * |
1937 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001938 // | for | target exit | * |
1939 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001940 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001941 // | for | cancellation | |
1942 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001943 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001944 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001945 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001946 // | for | distribute | + |
1947 // | for | distribute | + |
1948 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001949 // | for | distribute | + |
1950 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001951 // | for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001952 // | for | target parallel | + |
1953 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00001954 // | for | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001955 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001956 // | master | parallel | * |
1957 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001958 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001959 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001960 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001961 // | master | simd | * |
1962 // | master | sections | + |
1963 // | master | section | + |
1964 // | master | single | + |
1965 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001966 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001967 // | master |parallel sections| * |
1968 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001969 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001970 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001971 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001972 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001973 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001974 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001975 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001976 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001977 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001978 // | master | target parallel | * |
1979 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001980 // | master | target enter | * |
1981 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001982 // | master | target exit | * |
1983 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001984 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001985 // | master | cancellation | |
1986 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001987 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001988 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001989 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001990 // | master | distribute | + |
1991 // | master | distribute | + |
1992 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001993 // | master | distribute | + |
1994 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001995 // | master | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001996 // | master | target parallel | + |
1997 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00001998 // | master | target simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001999 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002000 // | critical | parallel | * |
2001 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002002 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002003 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002004 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002005 // | critical | simd | * |
2006 // | critical | sections | + |
2007 // | critical | section | + |
2008 // | critical | single | + |
2009 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002010 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002011 // | critical |parallel sections| * |
2012 // | critical | task | * |
2013 // | critical | taskyield | * |
2014 // | critical | barrier | + |
2015 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002016 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002017 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002018 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002019 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002020 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002021 // | critical | target parallel | * |
2022 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002023 // | critical | target enter | * |
2024 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002025 // | critical | target exit | * |
2026 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002027 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002028 // | critical | cancellation | |
2029 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002030 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002031 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002032 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002033 // | critical | distribute | + |
2034 // | critical | distribute | + |
2035 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002036 // | critical | distribute | + |
2037 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002038 // | critical | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002039 // | critical | target parallel | + |
2040 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002041 // | critical | target simd | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002042 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002043 // | simd | parallel | |
2044 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002045 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002046 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002047 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002048 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002049 // | simd | sections | |
2050 // | simd | section | |
2051 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002052 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002053 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002054 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002055 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002056 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002057 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002058 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002059 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002060 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002061 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002062 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002063 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002064 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002065 // | simd | target parallel | |
2066 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002067 // | simd | target enter | |
2068 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002069 // | simd | target exit | |
2070 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002071 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002072 // | simd | cancellation | |
2073 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002074 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002075 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002076 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002077 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002078 // | simd | distribute | |
2079 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002080 // | simd | distribute | |
2081 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002082 // | simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002083 // | simd | target parallel | |
2084 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002085 // | simd | target simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002086 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002087 // | for simd | parallel | |
2088 // | for simd | for | |
2089 // | for simd | for simd | |
2090 // | for simd | master | |
2091 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002092 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002093 // | for simd | sections | |
2094 // | for simd | section | |
2095 // | for simd | single | |
2096 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002097 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002098 // | for simd |parallel sections| |
2099 // | for simd | task | |
2100 // | for simd | taskyield | |
2101 // | for simd | barrier | |
2102 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002103 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002104 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002105 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002106 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002107 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002108 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002109 // | for simd | target parallel | |
2110 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002111 // | for simd | target enter | |
2112 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002113 // | for simd | target exit | |
2114 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002115 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002116 // | for simd | cancellation | |
2117 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002118 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002119 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002120 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002121 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002122 // | for simd | distribute | |
2123 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002124 // | for simd | distribute | |
2125 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002126 // | for simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002127 // | for simd | target parallel | |
2128 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002129 // | for simd | target simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002130 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002131 // | parallel for simd| parallel | |
2132 // | parallel for simd| for | |
2133 // | parallel for simd| for simd | |
2134 // | parallel for simd| master | |
2135 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002136 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002137 // | parallel for simd| sections | |
2138 // | parallel for simd| section | |
2139 // | parallel for simd| single | |
2140 // | parallel for simd| parallel for | |
2141 // | parallel for simd|parallel for simd| |
2142 // | parallel for simd|parallel sections| |
2143 // | parallel for simd| task | |
2144 // | parallel for simd| taskyield | |
2145 // | parallel for simd| barrier | |
2146 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002147 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002148 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002149 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002150 // | parallel for simd| atomic | |
2151 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002152 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002153 // | parallel for simd| target parallel | |
2154 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002155 // | parallel for simd| target enter | |
2156 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002157 // | parallel for simd| target exit | |
2158 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002159 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002160 // | parallel for simd| cancellation | |
2161 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002162 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002163 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002164 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002165 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002166 // | parallel for simd| distribute | |
2167 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002168 // | parallel for simd| distribute | |
2169 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002170 // | parallel for simd| distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002171 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002172 // | parallel for simd| target simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002173 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002174 // | sections | parallel | * |
2175 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002176 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002177 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002178 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002179 // | sections | simd | * |
2180 // | sections | sections | + |
2181 // | sections | section | * |
2182 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002183 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002184 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002185 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002186 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002187 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002188 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002189 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002190 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002191 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002192 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002193 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002194 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002195 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002196 // | sections | target parallel | * |
2197 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002198 // | sections | target enter | * |
2199 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002200 // | sections | target exit | * |
2201 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002202 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002203 // | sections | cancellation | |
2204 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002205 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002206 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002207 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002208 // | sections | distribute | + |
2209 // | sections | distribute | + |
2210 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002211 // | sections | distribute | + |
2212 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002213 // | sections | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002214 // | sections | target parallel | + |
2215 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002216 // | sections | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002217 // +------------------+-----------------+------------------------------------+
2218 // | section | parallel | * |
2219 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002220 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002221 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002222 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002223 // | section | simd | * |
2224 // | section | sections | + |
2225 // | section | section | + |
2226 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002227 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002228 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002229 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002230 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002231 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002232 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002233 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002234 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002235 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002236 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002237 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002238 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002239 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002240 // | section | target parallel | * |
2241 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002242 // | section | target enter | * |
2243 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002244 // | section | target exit | * |
2245 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002246 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002247 // | section | cancellation | |
2248 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002249 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002250 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002251 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002252 // | section | distribute | + |
2253 // | section | distribute | + |
2254 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002255 // | section | distribute | + |
2256 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002257 // | section | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002258 // | section | target parallel | + |
2259 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002260 // | section | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002261 // +------------------+-----------------+------------------------------------+
2262 // | single | parallel | * |
2263 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002264 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002265 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002266 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002267 // | single | simd | * |
2268 // | single | sections | + |
2269 // | single | section | + |
2270 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002271 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002272 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002273 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002274 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002275 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002276 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002277 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002278 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002279 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002280 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002281 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002282 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002283 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002284 // | single | target parallel | * |
2285 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002286 // | single | target enter | * |
2287 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002288 // | single | target exit | * |
2289 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002290 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002291 // | single | cancellation | |
2292 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002293 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002294 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002295 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002296 // | single | distribute | + |
2297 // | single | distribute | + |
2298 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002299 // | single | distribute | + |
2300 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002301 // | single | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002302 // | single | target parallel | + |
2303 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002304 // | single | target simd | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002305 // +------------------+-----------------+------------------------------------+
2306 // | parallel for | parallel | * |
2307 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002308 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002309 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002310 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002311 // | parallel for | simd | * |
2312 // | parallel for | sections | + |
2313 // | parallel for | section | + |
2314 // | parallel for | single | + |
2315 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002316 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002317 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002318 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002319 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002320 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002321 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002322 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002323 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002324 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002325 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002326 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002327 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002328 // | parallel for | target parallel | * |
2329 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002330 // | parallel for | target enter | * |
2331 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002332 // | parallel for | target exit | * |
2333 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002334 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002335 // | parallel for | cancellation | |
2336 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002337 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002338 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002339 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002340 // | parallel for | distribute | + |
2341 // | parallel for | distribute | + |
2342 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002343 // | parallel for | distribute | + |
2344 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002345 // | parallel for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002346 // | parallel for | target parallel | + |
2347 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002348 // | parallel for | target simd | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002349 // +------------------+-----------------+------------------------------------+
2350 // | parallel sections| parallel | * |
2351 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002352 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002353 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002354 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002355 // | parallel sections| simd | * |
2356 // | parallel sections| sections | + |
2357 // | parallel sections| section | * |
2358 // | parallel sections| single | + |
2359 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002360 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002361 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002362 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002363 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002364 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002365 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002366 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002367 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002368 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002369 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002370 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002371 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002372 // | parallel sections| target parallel | * |
2373 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002374 // | parallel sections| target enter | * |
2375 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002376 // | parallel sections| target exit | * |
2377 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002378 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002379 // | parallel sections| cancellation | |
2380 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002381 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002382 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002383 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002384 // | parallel sections| distribute | + |
2385 // | parallel sections| distribute | + |
2386 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002387 // | parallel sections| distribute | + |
2388 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002389 // | parallel sections| distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002390 // | parallel sections| target parallel | + |
2391 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002392 // | parallel sections| target simd | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002393 // +------------------+-----------------+------------------------------------+
2394 // | task | parallel | * |
2395 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002396 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002397 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002398 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002399 // | task | simd | * |
2400 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002401 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002402 // | task | single | + |
2403 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002404 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002405 // | task |parallel sections| * |
2406 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002407 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002408 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002409 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002410 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002411 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002412 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002413 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002414 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002415 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002416 // | task | target parallel | * |
2417 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002418 // | task | target enter | * |
2419 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002420 // | task | target exit | * |
2421 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002422 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002423 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002424 // | | point | ! |
2425 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002426 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002427 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002428 // | task | distribute | + |
2429 // | task | distribute | + |
2430 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002431 // | task | distribute | + |
2432 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002433 // | task | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002434 // | task | target parallel | + |
2435 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002436 // | task | target simd | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002437 // +------------------+-----------------+------------------------------------+
2438 // | ordered | parallel | * |
2439 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002440 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002441 // | ordered | master | * |
2442 // | ordered | critical | * |
2443 // | ordered | simd | * |
2444 // | ordered | sections | + |
2445 // | ordered | section | + |
2446 // | ordered | single | + |
2447 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002448 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002449 // | ordered |parallel sections| * |
2450 // | ordered | task | * |
2451 // | ordered | taskyield | * |
2452 // | ordered | barrier | + |
2453 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002454 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002455 // | ordered | flush | * |
2456 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002457 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002458 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002459 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002460 // | ordered | target parallel | * |
2461 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002462 // | ordered | target enter | * |
2463 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002464 // | ordered | target exit | * |
2465 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002466 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002467 // | ordered | cancellation | |
2468 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002469 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002470 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002471 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002472 // | ordered | distribute | + |
2473 // | ordered | distribute | + |
2474 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002475 // | ordered | distribute | + |
2476 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002477 // | ordered | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002478 // | ordered | target parallel | + |
2479 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002480 // | ordered | target simd | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002481 // +------------------+-----------------+------------------------------------+
2482 // | atomic | parallel | |
2483 // | atomic | for | |
2484 // | atomic | for simd | |
2485 // | atomic | master | |
2486 // | atomic | critical | |
2487 // | atomic | simd | |
2488 // | atomic | sections | |
2489 // | atomic | section | |
2490 // | atomic | single | |
2491 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002492 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002493 // | atomic |parallel sections| |
2494 // | atomic | task | |
2495 // | atomic | taskyield | |
2496 // | atomic | barrier | |
2497 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002498 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002499 // | atomic | flush | |
2500 // | atomic | ordered | |
2501 // | atomic | atomic | |
2502 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002503 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002504 // | atomic | target parallel | |
2505 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002506 // | atomic | target enter | |
2507 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002508 // | atomic | target exit | |
2509 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002510 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002511 // | atomic | cancellation | |
2512 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002513 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002514 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002515 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002516 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002517 // | atomic | distribute | |
2518 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002519 // | atomic | distribute | |
2520 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002521 // | atomic | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002522 // | atomic | target parallel | |
2523 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002524 // | atomic | target simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002525 // +------------------+-----------------+------------------------------------+
2526 // | target | parallel | * |
2527 // | target | for | * |
2528 // | target | for simd | * |
2529 // | target | master | * |
2530 // | target | critical | * |
2531 // | target | simd | * |
2532 // | target | sections | * |
2533 // | target | section | * |
2534 // | target | single | * |
2535 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002536 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002537 // | target |parallel sections| * |
2538 // | target | task | * |
2539 // | target | taskyield | * |
2540 // | target | barrier | * |
2541 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002542 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002543 // | target | flush | * |
2544 // | target | ordered | * |
2545 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002546 // | target | target | |
2547 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002548 // | target | target parallel | |
2549 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002550 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002551 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002552 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002553 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002554 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002555 // | target | cancellation | |
2556 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002557 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002558 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002559 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002560 // | target | distribute | + |
2561 // | target | distribute | + |
2562 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002563 // | target | distribute | + |
2564 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002565 // | target | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002566 // | target | target parallel | |
2567 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002568 // | target | target simd | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002569 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002570 // | target parallel | parallel | * |
2571 // | target parallel | for | * |
2572 // | target parallel | for simd | * |
2573 // | target parallel | master | * |
2574 // | target parallel | critical | * |
2575 // | target parallel | simd | * |
2576 // | target parallel | sections | * |
2577 // | target parallel | section | * |
2578 // | target parallel | single | * |
2579 // | target parallel | parallel for | * |
2580 // | target parallel |parallel for simd| * |
2581 // | target parallel |parallel sections| * |
2582 // | target parallel | task | * |
2583 // | target parallel | taskyield | * |
2584 // | target parallel | barrier | * |
2585 // | target parallel | taskwait | * |
2586 // | target parallel | taskgroup | * |
2587 // | target parallel | flush | * |
2588 // | target parallel | ordered | * |
2589 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002590 // | target parallel | target | |
2591 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002592 // | target parallel | target parallel | |
2593 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002594 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002595 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002596 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002597 // | | data | |
2598 // | target parallel | teams | |
2599 // | target parallel | cancellation | |
2600 // | | point | ! |
2601 // | target parallel | cancel | ! |
2602 // | target parallel | taskloop | * |
2603 // | target parallel | taskloop simd | * |
2604 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002605 // | target parallel | distribute | |
2606 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002607 // | target parallel | distribute | |
2608 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002609 // | target parallel | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002610 // | target parallel | target parallel | |
2611 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002612 // | target parallel | target simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002613 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002614 // | target parallel | parallel | * |
2615 // | for | | |
2616 // | target parallel | for | * |
2617 // | for | | |
2618 // | target parallel | for simd | * |
2619 // | for | | |
2620 // | target parallel | master | * |
2621 // | for | | |
2622 // | target parallel | critical | * |
2623 // | for | | |
2624 // | target parallel | simd | * |
2625 // | for | | |
2626 // | target parallel | sections | * |
2627 // | for | | |
2628 // | target parallel | section | * |
2629 // | for | | |
2630 // | target parallel | single | * |
2631 // | for | | |
2632 // | target parallel | parallel for | * |
2633 // | for | | |
2634 // | target parallel |parallel for simd| * |
2635 // | for | | |
2636 // | target parallel |parallel sections| * |
2637 // | for | | |
2638 // | target parallel | task | * |
2639 // | for | | |
2640 // | target parallel | taskyield | * |
2641 // | for | | |
2642 // | target parallel | barrier | * |
2643 // | for | | |
2644 // | target parallel | taskwait | * |
2645 // | for | | |
2646 // | target parallel | taskgroup | * |
2647 // | for | | |
2648 // | target parallel | flush | * |
2649 // | for | | |
2650 // | target parallel | ordered | * |
2651 // | for | | |
2652 // | target parallel | atomic | * |
2653 // | for | | |
2654 // | target parallel | target | |
2655 // | for | | |
2656 // | target parallel | target parallel | |
2657 // | for | | |
2658 // | target parallel | target parallel | |
2659 // | for | for | |
2660 // | target parallel | target enter | |
2661 // | for | data | |
2662 // | target parallel | target exit | |
2663 // | for | data | |
2664 // | target parallel | teams | |
2665 // | for | | |
2666 // | target parallel | cancellation | |
2667 // | for | point | ! |
2668 // | target parallel | cancel | ! |
2669 // | for | | |
2670 // | target parallel | taskloop | * |
2671 // | for | | |
2672 // | target parallel | taskloop simd | * |
2673 // | for | | |
2674 // | target parallel | distribute | |
2675 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002676 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002677 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002678 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002679 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002680 // | target parallel | distribute simd | |
2681 // | for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002682 // | target parallel | target parallel | |
2683 // | for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002684 // | target parallel | target simd | |
2685 // | for | | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002686 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002687 // | teams | parallel | * |
2688 // | teams | for | + |
2689 // | teams | for simd | + |
2690 // | teams | master | + |
2691 // | teams | critical | + |
2692 // | teams | simd | + |
2693 // | teams | sections | + |
2694 // | teams | section | + |
2695 // | teams | single | + |
2696 // | teams | parallel for | * |
2697 // | teams |parallel for simd| * |
2698 // | teams |parallel sections| * |
2699 // | teams | task | + |
2700 // | teams | taskyield | + |
2701 // | teams | barrier | + |
2702 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002703 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002704 // | teams | flush | + |
2705 // | teams | ordered | + |
2706 // | teams | atomic | + |
2707 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002708 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002709 // | teams | target parallel | + |
2710 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002711 // | teams | target enter | + |
2712 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002713 // | teams | target exit | + |
2714 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002715 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002716 // | teams | cancellation | |
2717 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002718 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002719 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002720 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002721 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002722 // | teams | distribute | ! |
2723 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002724 // | teams | distribute | ! |
2725 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002726 // | teams | distribute simd | ! |
Kelvin Lia579b912016-07-14 02:54:56 +00002727 // | teams | target parallel | + |
2728 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002729 // | teams | target simd | + |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002730 // +------------------+-----------------+------------------------------------+
2731 // | taskloop | parallel | * |
2732 // | taskloop | for | + |
2733 // | taskloop | for simd | + |
2734 // | taskloop | master | + |
2735 // | taskloop | critical | * |
2736 // | taskloop | simd | * |
2737 // | taskloop | sections | + |
2738 // | taskloop | section | + |
2739 // | taskloop | single | + |
2740 // | taskloop | parallel for | * |
2741 // | taskloop |parallel for simd| * |
2742 // | taskloop |parallel sections| * |
2743 // | taskloop | task | * |
2744 // | taskloop | taskyield | * |
2745 // | taskloop | barrier | + |
2746 // | taskloop | taskwait | * |
2747 // | taskloop | taskgroup | * |
2748 // | taskloop | flush | * |
2749 // | taskloop | ordered | + |
2750 // | taskloop | atomic | * |
2751 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002752 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002753 // | taskloop | target parallel | * |
2754 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002755 // | taskloop | target enter | * |
2756 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002757 // | taskloop | target exit | * |
2758 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002759 // | taskloop | teams | + |
2760 // | taskloop | cancellation | |
2761 // | | point | |
2762 // | taskloop | cancel | |
2763 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002764 // | taskloop | distribute | + |
2765 // | taskloop | distribute | + |
2766 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002767 // | taskloop | distribute | + |
2768 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002769 // | taskloop | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002770 // | taskloop | target parallel | * |
2771 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002772 // | taskloop | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002773 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002774 // | taskloop simd | parallel | |
2775 // | taskloop simd | for | |
2776 // | taskloop simd | for simd | |
2777 // | taskloop simd | master | |
2778 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002779 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002780 // | taskloop simd | sections | |
2781 // | taskloop simd | section | |
2782 // | taskloop simd | single | |
2783 // | taskloop simd | parallel for | |
2784 // | taskloop simd |parallel for simd| |
2785 // | taskloop simd |parallel sections| |
2786 // | taskloop simd | task | |
2787 // | taskloop simd | taskyield | |
2788 // | taskloop simd | barrier | |
2789 // | taskloop simd | taskwait | |
2790 // | taskloop simd | taskgroup | |
2791 // | taskloop simd | flush | |
2792 // | taskloop simd | ordered | + (with simd clause) |
2793 // | taskloop simd | atomic | |
2794 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002795 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002796 // | taskloop simd | target parallel | |
2797 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002798 // | taskloop simd | target enter | |
2799 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002800 // | taskloop simd | target exit | |
2801 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002802 // | taskloop simd | teams | |
2803 // | taskloop simd | cancellation | |
2804 // | | point | |
2805 // | taskloop simd | cancel | |
2806 // | taskloop simd | taskloop | |
2807 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002808 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002809 // | taskloop simd | distribute | |
2810 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002811 // | taskloop simd | distribute | |
2812 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002813 // | taskloop simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002814 // | taskloop simd | target parallel | |
2815 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002816 // | taskloop simd | target simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002817 // +------------------+-----------------+------------------------------------+
2818 // | distribute | parallel | * |
2819 // | distribute | for | * |
2820 // | distribute | for simd | * |
2821 // | distribute | master | * |
2822 // | distribute | critical | * |
2823 // | distribute | simd | * |
2824 // | distribute | sections | * |
2825 // | distribute | section | * |
2826 // | distribute | single | * |
2827 // | distribute | parallel for | * |
2828 // | distribute |parallel for simd| * |
2829 // | distribute |parallel sections| * |
2830 // | distribute | task | * |
2831 // | distribute | taskyield | * |
2832 // | distribute | barrier | * |
2833 // | distribute | taskwait | * |
2834 // | distribute | taskgroup | * |
2835 // | distribute | flush | * |
2836 // | distribute | ordered | + |
2837 // | distribute | atomic | * |
2838 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002839 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002840 // | distribute | target parallel | |
2841 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002842 // | distribute | target enter | |
2843 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002844 // | distribute | target exit | |
2845 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002846 // | distribute | teams | |
2847 // | distribute | cancellation | + |
2848 // | | point | |
2849 // | distribute | cancel | + |
2850 // | distribute | taskloop | * |
2851 // | distribute | taskloop simd | * |
2852 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002853 // | distribute | distribute | |
2854 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002855 // | distribute | distribute | |
2856 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002857 // | distribute | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002858 // | distribute | target parallel | |
2859 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002860 // | distribute | target simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002861 // +------------------+-----------------+------------------------------------+
2862 // | distribute | parallel | * |
2863 // | parallel for | | |
2864 // | distribute | for | * |
2865 // | parallel for | | |
2866 // | distribute | for simd | * |
2867 // | parallel for | | |
2868 // | distribute | master | * |
2869 // | parallel for | | |
2870 // | distribute | critical | * |
2871 // | parallel for | | |
2872 // | distribute | simd | * |
2873 // | parallel for | | |
2874 // | distribute | sections | * |
2875 // | parallel for | | |
2876 // | distribute | section | * |
2877 // | parallel for | | |
2878 // | distribute | single | * |
2879 // | parallel for | | |
2880 // | distribute | parallel for | * |
2881 // | parallel for | | |
2882 // | distribute |parallel for simd| * |
2883 // | parallel for | | |
2884 // | distribute |parallel sections| * |
2885 // | parallel for | | |
2886 // | distribute | task | * |
2887 // | parallel for | | |
2888 // | parallel for | | |
2889 // | distribute | taskyield | * |
2890 // | parallel for | | |
2891 // | distribute | barrier | * |
2892 // | parallel for | | |
2893 // | distribute | taskwait | * |
2894 // | parallel for | | |
2895 // | distribute | taskgroup | * |
2896 // | parallel for | | |
2897 // | distribute | flush | * |
2898 // | parallel for | | |
2899 // | distribute | ordered | + |
2900 // | parallel for | | |
2901 // | distribute | atomic | * |
2902 // | parallel for | | |
2903 // | distribute | target | |
2904 // | parallel for | | |
2905 // | distribute | target parallel | |
2906 // | parallel for | | |
2907 // | distribute | target parallel | |
2908 // | parallel for | for | |
2909 // | distribute | target enter | |
2910 // | parallel for | data | |
2911 // | distribute | target exit | |
2912 // | parallel for | data | |
2913 // | distribute | teams | |
2914 // | parallel for | | |
2915 // | distribute | cancellation | + |
2916 // | parallel for | point | |
2917 // | distribute | cancel | + |
2918 // | parallel for | | |
2919 // | distribute | taskloop | * |
2920 // | parallel for | | |
2921 // | distribute | taskloop simd | * |
2922 // | parallel for | | |
2923 // | distribute | distribute | |
2924 // | parallel for | | |
2925 // | distribute | distribute | |
2926 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002927 // | distribute | distribute | |
2928 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002929 // | distribute | distribute simd | |
2930 // | parallel for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002931 // | distribute | target parallel | |
2932 // | parallel for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002933 // | distribute | target simd | |
2934 // | parallel for | | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002935 // +------------------+-----------------+------------------------------------+
2936 // | distribute | parallel | * |
2937 // | parallel for simd| | |
2938 // | distribute | for | * |
2939 // | parallel for simd| | |
2940 // | distribute | for simd | * |
2941 // | parallel for simd| | |
2942 // | distribute | master | * |
2943 // | parallel for simd| | |
2944 // | distribute | critical | * |
2945 // | parallel for simd| | |
2946 // | distribute | simd | * |
2947 // | parallel for simd| | |
2948 // | distribute | sections | * |
2949 // | parallel for simd| | |
2950 // | distribute | section | * |
2951 // | parallel for simd| | |
2952 // | distribute | single | * |
2953 // | parallel for simd| | |
2954 // | distribute | parallel for | * |
2955 // | parallel for simd| | |
2956 // | distribute |parallel for simd| * |
2957 // | parallel for simd| | |
2958 // | distribute |parallel sections| * |
2959 // | parallel for simd| | |
2960 // | distribute | task | * |
2961 // | parallel for simd| | |
2962 // | distribute | taskyield | * |
2963 // | parallel for simd| | |
2964 // | distribute | barrier | * |
2965 // | parallel for simd| | |
2966 // | distribute | taskwait | * |
2967 // | parallel for simd| | |
2968 // | distribute | taskgroup | * |
2969 // | parallel for simd| | |
2970 // | distribute | flush | * |
2971 // | parallel for simd| | |
2972 // | distribute | ordered | + |
2973 // | parallel for simd| | |
2974 // | distribute | atomic | * |
2975 // | parallel for simd| | |
2976 // | distribute | target | |
2977 // | parallel for simd| | |
2978 // | distribute | target parallel | |
2979 // | parallel for simd| | |
2980 // | distribute | target parallel | |
2981 // | parallel for simd| for | |
2982 // | distribute | target enter | |
2983 // | parallel for simd| data | |
2984 // | distribute | target exit | |
2985 // | parallel for simd| data | |
2986 // | distribute | teams | |
2987 // | parallel for simd| | |
2988 // | distribute | cancellation | + |
2989 // | parallel for simd| point | |
2990 // | distribute | cancel | + |
2991 // | parallel for simd| | |
2992 // | distribute | taskloop | * |
2993 // | parallel for simd| | |
2994 // | distribute | taskloop simd | * |
2995 // | parallel for simd| | |
2996 // | distribute | distribute | |
2997 // | parallel for simd| | |
2998 // | distribute | distribute | * |
2999 // | parallel for simd| parallel for | |
3000 // | distribute | distribute | * |
3001 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003002 // | distribute | distribute simd | * |
3003 // | parallel for simd| | |
Kelvin Lia579b912016-07-14 02:54:56 +00003004 // | distribute | target parallel | |
3005 // | parallel for simd| for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003006 // | distribute | target simd | |
3007 // | parallel for simd| | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003008 // +------------------+-----------------+------------------------------------+
3009 // | distribute simd | parallel | * |
3010 // | distribute simd | for | * |
3011 // | distribute simd | for simd | * |
3012 // | distribute simd | master | * |
3013 // | distribute simd | critical | * |
3014 // | distribute simd | simd | * |
3015 // | distribute simd | sections | * |
3016 // | distribute simd | section | * |
3017 // | distribute simd | single | * |
3018 // | distribute simd | parallel for | * |
3019 // | distribute simd |parallel for simd| * |
3020 // | distribute simd |parallel sections| * |
3021 // | distribute simd | task | * |
3022 // | distribute simd | taskyield | * |
3023 // | distribute simd | barrier | * |
3024 // | distribute simd | taskwait | * |
3025 // | distribute simd | taskgroup | * |
3026 // | distribute simd | flush | * |
3027 // | distribute simd | ordered | + |
3028 // | distribute simd | atomic | * |
3029 // | distribute simd | target | * |
3030 // | distribute simd | target parallel | * |
3031 // | distribute simd | target parallel | * |
3032 // | | for | |
3033 // | distribute simd | target enter | * |
3034 // | | data | |
3035 // | distribute simd | target exit | * |
3036 // | | data | |
3037 // | distribute simd | teams | * |
3038 // | distribute simd | cancellation | + |
3039 // | | point | |
3040 // | distribute simd | cancel | + |
3041 // | distribute simd | taskloop | * |
3042 // | distribute simd | taskloop simd | * |
3043 // | distribute simd | distribute | |
3044 // | distribute simd | distribute | * |
3045 // | | parallel for | |
3046 // | distribute simd | distribute | * |
3047 // | |parallel for simd| |
3048 // | distribute simd | distribute simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003049 // | distribute simd | target parallel | * |
3050 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003051 // | distribute simd | target simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003052 // +------------------+-----------------+------------------------------------+
3053 // | target parallel | parallel | * |
3054 // | for simd | | |
3055 // | target parallel | for | * |
3056 // | for simd | | |
3057 // | target parallel | for simd | * |
3058 // | for simd | | |
3059 // | target parallel | master | * |
3060 // | for simd | | |
3061 // | target parallel | critical | * |
3062 // | for simd | | |
3063 // | target parallel | simd | ! |
3064 // | for simd | | |
3065 // | target parallel | sections | * |
3066 // | for simd | | |
3067 // | target parallel | section | * |
3068 // | for simd | | |
3069 // | target parallel | single | * |
3070 // | for simd | | |
3071 // | target parallel | parallel for | * |
3072 // | for simd | | |
3073 // | target parallel |parallel for simd| * |
3074 // | for simd | | |
3075 // | target parallel |parallel sections| * |
3076 // | for simd | | |
3077 // | target parallel | task | * |
3078 // | for simd | | |
3079 // | target parallel | taskyield | * |
3080 // | for simd | | |
3081 // | target parallel | barrier | * |
3082 // | for simd | | |
3083 // | target parallel | taskwait | * |
3084 // | for simd | | |
3085 // | target parallel | taskgroup | * |
3086 // | for simd | | |
3087 // | target parallel | flush | * |
3088 // | for simd | | |
3089 // | target parallel | ordered | + (with simd clause) |
3090 // | for simd | | |
3091 // | target parallel | atomic | * |
3092 // | for simd | | |
3093 // | target parallel | target | * |
3094 // | for simd | | |
3095 // | target parallel | target parallel | * |
3096 // | for simd | | |
3097 // | target parallel | target parallel | * |
3098 // | for simd | for | |
3099 // | target parallel | target enter | * |
3100 // | for simd | data | |
3101 // | target parallel | target exit | * |
3102 // | for simd | data | |
3103 // | target parallel | teams | * |
3104 // | for simd | | |
3105 // | target parallel | cancellation | * |
3106 // | for simd | point | |
3107 // | target parallel | cancel | * |
3108 // | for simd | | |
3109 // | target parallel | taskloop | * |
3110 // | for simd | | |
3111 // | target parallel | taskloop simd | * |
3112 // | for simd | | |
3113 // | target parallel | distribute | * |
3114 // | for simd | | |
3115 // | target parallel | distribute | * |
3116 // | for simd | parallel for | |
3117 // | target parallel | distribute | * |
3118 // | for simd |parallel for simd| |
3119 // | target parallel | distribute simd | * |
3120 // | for simd | | |
3121 // | target parallel | target parallel | * |
3122 // | for simd | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003123 // | target parallel | target simd | * |
3124 // | for simd | | |
3125 // +------------------+-----------------+------------------------------------+
3126 // | target simd | parallel | |
3127 // | target simd | for | |
3128 // | target simd | for simd | |
3129 // | target simd | master | |
3130 // | target simd | critical | |
3131 // | target simd | simd | |
3132 // | target simd | sections | |
3133 // | target simd | section | |
3134 // | target simd | single | |
3135 // | target simd | parallel for | |
3136 // | target simd |parallel for simd| |
3137 // | target simd |parallel sections| |
3138 // | target simd | task | |
3139 // | target simd | taskyield | |
3140 // | target simd | barrier | |
3141 // | target simd | taskwait | |
3142 // | target simd | taskgroup | |
3143 // | target simd | flush | |
3144 // | target simd | ordered | + (with simd clause) |
3145 // | target simd | atomic | |
3146 // | target simd | target | |
3147 // | target simd | target parallel | |
3148 // | target simd | target parallel | |
3149 // | | for | |
3150 // | target simd | target enter | |
3151 // | | data | |
3152 // | target simd | target exit | |
3153 // | | data | |
3154 // | target simd | teams | |
3155 // | target simd | cancellation | |
3156 // | | point | |
3157 // | target simd | cancel | |
3158 // | target simd | taskloop | |
3159 // | target simd | taskloop simd | |
3160 // | target simd | distribute | |
3161 // | target simd | distribute | |
3162 // | | parallel for | |
3163 // | target simd | distribute | |
3164 // | |parallel for simd| |
3165 // | target simd | distribute simd | |
3166 // | target simd | target parallel | |
3167 // | | for simd | |
3168 // | target simd | target simd | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003169 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003170 if (Stack->getCurScope()) {
3171 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003172 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003173 bool NestingProhibited = false;
3174 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003175 enum {
3176 NoRecommend,
3177 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003178 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003179 ShouldBeInTargetRegion,
3180 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003181 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003182 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003183 // OpenMP [2.16, Nesting of Regions]
3184 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003185 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003186 // An ordered construct with the simd clause is the only OpenMP
3187 // construct that can appear in the simd region.
3188 // Allowing a SIMD consruct nested in another SIMD construct is an
3189 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3190 // message.
3191 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3192 ? diag::err_omp_prohibited_region_simd
3193 : diag::warn_omp_nesting_simd);
3194 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003195 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003196 if (ParentRegion == OMPD_atomic) {
3197 // OpenMP [2.16, Nesting of Regions]
3198 // OpenMP constructs may not be nested inside an atomic region.
3199 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3200 return true;
3201 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003202 if (CurrentRegion == OMPD_section) {
3203 // OpenMP [2.7.2, sections Construct, Restrictions]
3204 // Orphaned section directives are prohibited. That is, the section
3205 // directives must appear within the sections construct and must not be
3206 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003207 if (ParentRegion != OMPD_sections &&
3208 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003209 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3210 << (ParentRegion != OMPD_unknown)
3211 << getOpenMPDirectiveName(ParentRegion);
3212 return true;
3213 }
3214 return false;
3215 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003216 // Allow some constructs to be orphaned (they could be used in functions,
3217 // called from OpenMP regions with the required preconditions).
3218 if (ParentRegion == OMPD_unknown)
3219 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003220 if (CurrentRegion == OMPD_cancellation_point ||
3221 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003222 // OpenMP [2.16, Nesting of Regions]
3223 // A cancellation point construct for which construct-type-clause is
3224 // taskgroup must be nested inside a task construct. A cancellation
3225 // point construct for which construct-type-clause is not taskgroup must
3226 // be closely nested inside an OpenMP construct that matches the type
3227 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003228 // A cancel construct for which construct-type-clause is taskgroup must be
3229 // nested inside a task construct. A cancel construct for which
3230 // construct-type-clause is not taskgroup must be closely nested inside an
3231 // OpenMP construct that matches the type specified in
3232 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003233 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003234 !((CancelRegion == OMPD_parallel &&
3235 (ParentRegion == OMPD_parallel ||
3236 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003237 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003238 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3239 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003240 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3241 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003242 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3243 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003244 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003245 // OpenMP [2.16, Nesting of Regions]
3246 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003247 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003248 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003249 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003250 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3251 // OpenMP [2.16, Nesting of Regions]
3252 // A critical region may not be nested (closely or otherwise) inside a
3253 // critical region with the same name. Note that this restriction is not
3254 // sufficient to prevent deadlock.
3255 SourceLocation PreviousCriticalLoc;
3256 bool DeadLock =
3257 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3258 OpenMPDirectiveKind K,
3259 const DeclarationNameInfo &DNI,
3260 SourceLocation Loc)
3261 ->bool {
3262 if (K == OMPD_critical &&
3263 DNI.getName() == CurrentName.getName()) {
3264 PreviousCriticalLoc = Loc;
3265 return true;
3266 } else
3267 return false;
3268 },
3269 false /* skip top directive */);
3270 if (DeadLock) {
3271 SemaRef.Diag(StartLoc,
3272 diag::err_omp_prohibited_region_critical_same_name)
3273 << CurrentName.getName();
3274 if (PreviousCriticalLoc.isValid())
3275 SemaRef.Diag(PreviousCriticalLoc,
3276 diag::note_omp_previous_critical_region);
3277 return true;
3278 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003279 } else if (CurrentRegion == OMPD_barrier) {
3280 // OpenMP [2.16, Nesting of Regions]
3281 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003282 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003283 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3284 isOpenMPTaskingDirective(ParentRegion) ||
3285 ParentRegion == OMPD_master ||
3286 ParentRegion == OMPD_critical ||
3287 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003288 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003289 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003290 // OpenMP [2.16, Nesting of Regions]
3291 // A worksharing region may not be closely nested inside a worksharing,
3292 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003293 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3294 isOpenMPTaskingDirective(ParentRegion) ||
3295 ParentRegion == OMPD_master ||
3296 ParentRegion == OMPD_critical ||
3297 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003298 Recommend = ShouldBeInParallelRegion;
3299 } else if (CurrentRegion == OMPD_ordered) {
3300 // OpenMP [2.16, Nesting of Regions]
3301 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003302 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003303 // An ordered region must be closely nested inside a loop region (or
3304 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003305 // OpenMP [2.8.1,simd Construct, Restrictions]
3306 // An ordered construct with the simd clause is the only OpenMP construct
3307 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003308 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003309 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003310 !(isOpenMPSimdDirective(ParentRegion) ||
3311 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003312 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003313 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3314 // OpenMP [2.16, Nesting of Regions]
3315 // If specified, a teams construct must be contained within a target
3316 // construct.
3317 NestingProhibited = ParentRegion != OMPD_target;
3318 Recommend = ShouldBeInTargetRegion;
3319 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3320 }
3321 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3322 // OpenMP [2.16, Nesting of Regions]
3323 // distribute, parallel, parallel sections, parallel workshare, and the
3324 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3325 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003326 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3327 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003328 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003329 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003330 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3331 // OpenMP 4.5 [2.17 Nesting of Regions]
3332 // The region associated with the distribute construct must be strictly
3333 // nested inside a teams region
3334 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3335 Recommend = ShouldBeInTeamsRegion;
3336 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003337 if (!NestingProhibited &&
3338 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3339 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3340 // OpenMP 4.5 [2.17 Nesting of Regions]
3341 // If a target, target update, target data, target enter data, or
3342 // target exit data construct is encountered during execution of a
3343 // target region, the behavior is unspecified.
3344 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003345 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3346 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003347 if (isOpenMPTargetExecutionDirective(K)) {
3348 OffendingRegion = K;
3349 return true;
3350 } else
3351 return false;
3352 },
3353 false /* don't skip top directive */);
3354 CloseNesting = false;
3355 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003356 if (NestingProhibited) {
3357 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003358 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3359 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00003360 return true;
3361 }
3362 }
3363 return false;
3364}
3365
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003366static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3367 ArrayRef<OMPClause *> Clauses,
3368 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3369 bool ErrorFound = false;
3370 unsigned NamedModifiersNumber = 0;
3371 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3372 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003373 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003374 for (const auto *C : Clauses) {
3375 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3376 // At most one if clause without a directive-name-modifier can appear on
3377 // the directive.
3378 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3379 if (FoundNameModifiers[CurNM]) {
3380 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3381 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3382 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3383 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003384 } else if (CurNM != OMPD_unknown) {
3385 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003386 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003387 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003388 FoundNameModifiers[CurNM] = IC;
3389 if (CurNM == OMPD_unknown)
3390 continue;
3391 // Check if the specified name modifier is allowed for the current
3392 // directive.
3393 // At most one if clause with the particular directive-name-modifier can
3394 // appear on the directive.
3395 bool MatchFound = false;
3396 for (auto NM : AllowedNameModifiers) {
3397 if (CurNM == NM) {
3398 MatchFound = true;
3399 break;
3400 }
3401 }
3402 if (!MatchFound) {
3403 S.Diag(IC->getNameModifierLoc(),
3404 diag::err_omp_wrong_if_directive_name_modifier)
3405 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3406 ErrorFound = true;
3407 }
3408 }
3409 }
3410 // If any if clause on the directive includes a directive-name-modifier then
3411 // all if clauses on the directive must include a directive-name-modifier.
3412 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3413 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3414 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3415 diag::err_omp_no_more_if_clause);
3416 } else {
3417 std::string Values;
3418 std::string Sep(", ");
3419 unsigned AllowedCnt = 0;
3420 unsigned TotalAllowedNum =
3421 AllowedNameModifiers.size() - NamedModifiersNumber;
3422 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3423 ++Cnt) {
3424 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3425 if (!FoundNameModifiers[NM]) {
3426 Values += "'";
3427 Values += getOpenMPDirectiveName(NM);
3428 Values += "'";
3429 if (AllowedCnt + 2 == TotalAllowedNum)
3430 Values += " or ";
3431 else if (AllowedCnt + 1 != TotalAllowedNum)
3432 Values += Sep;
3433 ++AllowedCnt;
3434 }
3435 }
3436 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3437 diag::err_omp_unnamed_if_clause)
3438 << (TotalAllowedNum > 1) << Values;
3439 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003440 for (auto Loc : NameModifierLoc) {
3441 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3442 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003443 ErrorFound = true;
3444 }
3445 return ErrorFound;
3446}
3447
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003448StmtResult Sema::ActOnOpenMPExecutableDirective(
3449 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3450 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3451 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003452 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003453 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3454 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003455 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003456
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003457 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003458 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003459 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003460 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003461 if (AStmt) {
3462 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3463
3464 // Check default data sharing attributes for referenced variables.
3465 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3466 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3467 if (DSAChecker.isErrorFound())
3468 return StmtError();
3469 // Generate list of implicitly defined firstprivate variables.
3470 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003471
3472 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3473 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3474 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3475 SourceLocation(), SourceLocation())) {
3476 ClausesWithImplicit.push_back(Implicit);
3477 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3478 DSAChecker.getImplicitFirstprivate().size();
3479 } else
3480 ErrorFound = true;
3481 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003482 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003483
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003484 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003485 switch (Kind) {
3486 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003487 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3488 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003489 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003490 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003491 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003492 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3493 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003494 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003495 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003496 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3497 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003498 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003499 case OMPD_for_simd:
3500 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3501 EndLoc, VarsWithInheritedDSA);
3502 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003503 case OMPD_sections:
3504 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3505 EndLoc);
3506 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003507 case OMPD_section:
3508 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003509 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003510 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3511 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003512 case OMPD_single:
3513 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3514 EndLoc);
3515 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003516 case OMPD_master:
3517 assert(ClausesWithImplicit.empty() &&
3518 "No clauses are allowed for 'omp master' directive");
3519 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3520 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003521 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003522 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3523 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003524 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003525 case OMPD_parallel_for:
3526 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3527 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003528 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003529 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003530 case OMPD_parallel_for_simd:
3531 Res = ActOnOpenMPParallelForSimdDirective(
3532 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003533 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003534 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003535 case OMPD_parallel_sections:
3536 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3537 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003538 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003539 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003540 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003541 Res =
3542 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003543 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003544 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003545 case OMPD_taskyield:
3546 assert(ClausesWithImplicit.empty() &&
3547 "No clauses are allowed for 'omp taskyield' directive");
3548 assert(AStmt == nullptr &&
3549 "No associated statement allowed for 'omp taskyield' directive");
3550 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3551 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003552 case OMPD_barrier:
3553 assert(ClausesWithImplicit.empty() &&
3554 "No clauses are allowed for 'omp barrier' directive");
3555 assert(AStmt == nullptr &&
3556 "No associated statement allowed for 'omp barrier' directive");
3557 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3558 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003559 case OMPD_taskwait:
3560 assert(ClausesWithImplicit.empty() &&
3561 "No clauses are allowed for 'omp taskwait' directive");
3562 assert(AStmt == nullptr &&
3563 "No associated statement allowed for 'omp taskwait' directive");
3564 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3565 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003566 case OMPD_taskgroup:
3567 assert(ClausesWithImplicit.empty() &&
3568 "No clauses are allowed for 'omp taskgroup' directive");
3569 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3570 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003571 case OMPD_flush:
3572 assert(AStmt == nullptr &&
3573 "No associated statement allowed for 'omp flush' directive");
3574 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3575 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003576 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003577 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3578 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003579 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003580 case OMPD_atomic:
3581 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3582 EndLoc);
3583 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003584 case OMPD_teams:
3585 Res =
3586 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3587 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003588 case OMPD_target:
3589 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3590 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003591 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003592 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003593 case OMPD_target_parallel:
3594 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3595 StartLoc, EndLoc);
3596 AllowedNameModifiers.push_back(OMPD_target);
3597 AllowedNameModifiers.push_back(OMPD_parallel);
3598 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003599 case OMPD_target_parallel_for:
3600 Res = ActOnOpenMPTargetParallelForDirective(
3601 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3602 AllowedNameModifiers.push_back(OMPD_target);
3603 AllowedNameModifiers.push_back(OMPD_parallel);
3604 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003605 case OMPD_cancellation_point:
3606 assert(ClausesWithImplicit.empty() &&
3607 "No clauses are allowed for 'omp cancellation point' directive");
3608 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3609 "cancellation point' directive");
3610 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3611 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003612 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003613 assert(AStmt == nullptr &&
3614 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003615 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3616 CancelRegion);
3617 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003618 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003619 case OMPD_target_data:
3620 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3621 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003622 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003623 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003624 case OMPD_target_enter_data:
3625 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3626 EndLoc);
3627 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3628 break;
Samuel Antao72590762016-01-19 20:04:50 +00003629 case OMPD_target_exit_data:
3630 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3631 EndLoc);
3632 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3633 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003634 case OMPD_taskloop:
3635 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3636 EndLoc, VarsWithInheritedDSA);
3637 AllowedNameModifiers.push_back(OMPD_taskloop);
3638 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003639 case OMPD_taskloop_simd:
3640 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3641 EndLoc, VarsWithInheritedDSA);
3642 AllowedNameModifiers.push_back(OMPD_taskloop);
3643 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003644 case OMPD_distribute:
3645 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3646 EndLoc, VarsWithInheritedDSA);
3647 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003648 case OMPD_target_update:
3649 assert(!AStmt && "Statement is not allowed for target update");
3650 Res =
3651 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3652 AllowedNameModifiers.push_back(OMPD_target_update);
3653 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003654 case OMPD_distribute_parallel_for:
3655 Res = ActOnOpenMPDistributeParallelForDirective(
3656 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3657 AllowedNameModifiers.push_back(OMPD_parallel);
3658 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003659 case OMPD_distribute_parallel_for_simd:
3660 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3661 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3662 AllowedNameModifiers.push_back(OMPD_parallel);
3663 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003664 case OMPD_distribute_simd:
3665 Res = ActOnOpenMPDistributeSimdDirective(
3666 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3667 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003668 case OMPD_target_parallel_for_simd:
3669 Res = ActOnOpenMPTargetParallelForSimdDirective(
3670 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3671 AllowedNameModifiers.push_back(OMPD_target);
3672 AllowedNameModifiers.push_back(OMPD_parallel);
3673 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003674 case OMPD_target_simd:
3675 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3676 EndLoc, VarsWithInheritedDSA);
3677 AllowedNameModifiers.push_back(OMPD_target);
3678 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003679 case OMPD_declare_target:
3680 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003681 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003682 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003683 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003684 llvm_unreachable("OpenMP Directive is not allowed");
3685 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003686 llvm_unreachable("Unknown OpenMP directive");
3687 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003688
Alexey Bataev4acb8592014-07-07 13:01:15 +00003689 for (auto P : VarsWithInheritedDSA) {
3690 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3691 << P.first << P.second->getSourceRange();
3692 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003693 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3694
3695 if (!AllowedNameModifiers.empty())
3696 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3697 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003698
Alexey Bataeved09d242014-05-28 05:53:51 +00003699 if (ErrorFound)
3700 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003701 return Res;
3702}
3703
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003704Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3705 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003706 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003707 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3708 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003709 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003710 assert(Linears.size() == LinModifiers.size());
3711 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003712 if (!DG || DG.get().isNull())
3713 return DeclGroupPtrTy();
3714
3715 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003716 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003717 return DG;
3718 }
3719 auto *ADecl = DG.get().getSingleDecl();
3720 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3721 ADecl = FTD->getTemplatedDecl();
3722
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003723 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3724 if (!FD) {
3725 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003726 return DeclGroupPtrTy();
3727 }
3728
Alexey Bataev2af33e32016-04-07 12:45:37 +00003729 // OpenMP [2.8.2, declare simd construct, Description]
3730 // The parameter of the simdlen clause must be a constant positive integer
3731 // expression.
3732 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003733 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003734 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003735 // OpenMP [2.8.2, declare simd construct, Description]
3736 // The special this pointer can be used as if was one of the arguments to the
3737 // function in any of the linear, aligned, or uniform clauses.
3738 // The uniform clause declares one or more arguments to have an invariant
3739 // value for all concurrent invocations of the function in the execution of a
3740 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003741 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3742 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003743 for (auto *E : Uniforms) {
3744 E = E->IgnoreParenImpCasts();
3745 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3746 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3747 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3748 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003749 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3750 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003751 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003752 }
3753 if (isa<CXXThisExpr>(E)) {
3754 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003755 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003756 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003757 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3758 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003759 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003760 // OpenMP [2.8.2, declare simd construct, Description]
3761 // The aligned clause declares that the object to which each list item points
3762 // is aligned to the number of bytes expressed in the optional parameter of
3763 // the aligned clause.
3764 // The special this pointer can be used as if was one of the arguments to the
3765 // function in any of the linear, aligned, or uniform clauses.
3766 // The type of list items appearing in the aligned clause must be array,
3767 // pointer, reference to array, or reference to pointer.
3768 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3769 Expr *AlignedThis = nullptr;
3770 for (auto *E : Aligneds) {
3771 E = E->IgnoreParenImpCasts();
3772 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3773 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3774 auto *CanonPVD = PVD->getCanonicalDecl();
3775 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3776 FD->getParamDecl(PVD->getFunctionScopeIndex())
3777 ->getCanonicalDecl() == CanonPVD) {
3778 // OpenMP [2.8.1, simd construct, Restrictions]
3779 // A list-item cannot appear in more than one aligned clause.
3780 if (AlignedArgs.count(CanonPVD) > 0) {
3781 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3782 << 1 << E->getSourceRange();
3783 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3784 diag::note_omp_explicit_dsa)
3785 << getOpenMPClauseName(OMPC_aligned);
3786 continue;
3787 }
3788 AlignedArgs[CanonPVD] = E;
3789 QualType QTy = PVD->getType()
3790 .getNonReferenceType()
3791 .getUnqualifiedType()
3792 .getCanonicalType();
3793 const Type *Ty = QTy.getTypePtrOrNull();
3794 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3795 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3796 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3797 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3798 }
3799 continue;
3800 }
3801 }
3802 if (isa<CXXThisExpr>(E)) {
3803 if (AlignedThis) {
3804 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3805 << 2 << E->getSourceRange();
3806 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3807 << getOpenMPClauseName(OMPC_aligned);
3808 }
3809 AlignedThis = E;
3810 continue;
3811 }
3812 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3813 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3814 }
3815 // The optional parameter of the aligned clause, alignment, must be a constant
3816 // positive integer expression. If no optional parameter is specified,
3817 // implementation-defined default alignments for SIMD instructions on the
3818 // target platforms are assumed.
3819 SmallVector<Expr *, 4> NewAligns;
3820 for (auto *E : Alignments) {
3821 ExprResult Align;
3822 if (E)
3823 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3824 NewAligns.push_back(Align.get());
3825 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003826 // OpenMP [2.8.2, declare simd construct, Description]
3827 // The linear clause declares one or more list items to be private to a SIMD
3828 // lane and to have a linear relationship with respect to the iteration space
3829 // of a loop.
3830 // The special this pointer can be used as if was one of the arguments to the
3831 // function in any of the linear, aligned, or uniform clauses.
3832 // When a linear-step expression is specified in a linear clause it must be
3833 // either a constant integer expression or an integer-typed parameter that is
3834 // specified in a uniform clause on the directive.
3835 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3836 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3837 auto MI = LinModifiers.begin();
3838 for (auto *E : Linears) {
3839 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3840 ++MI;
3841 E = E->IgnoreParenImpCasts();
3842 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3843 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3844 auto *CanonPVD = PVD->getCanonicalDecl();
3845 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3846 FD->getParamDecl(PVD->getFunctionScopeIndex())
3847 ->getCanonicalDecl() == CanonPVD) {
3848 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3849 // A list-item cannot appear in more than one linear clause.
3850 if (LinearArgs.count(CanonPVD) > 0) {
3851 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3852 << getOpenMPClauseName(OMPC_linear)
3853 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3854 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3855 diag::note_omp_explicit_dsa)
3856 << getOpenMPClauseName(OMPC_linear);
3857 continue;
3858 }
3859 // Each argument can appear in at most one uniform or linear clause.
3860 if (UniformedArgs.count(CanonPVD) > 0) {
3861 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3862 << getOpenMPClauseName(OMPC_linear)
3863 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3864 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3865 diag::note_omp_explicit_dsa)
3866 << getOpenMPClauseName(OMPC_uniform);
3867 continue;
3868 }
3869 LinearArgs[CanonPVD] = E;
3870 if (E->isValueDependent() || E->isTypeDependent() ||
3871 E->isInstantiationDependent() ||
3872 E->containsUnexpandedParameterPack())
3873 continue;
3874 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3875 PVD->getOriginalType());
3876 continue;
3877 }
3878 }
3879 if (isa<CXXThisExpr>(E)) {
3880 if (UniformedLinearThis) {
3881 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3882 << getOpenMPClauseName(OMPC_linear)
3883 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3884 << E->getSourceRange();
3885 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3886 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3887 : OMPC_linear);
3888 continue;
3889 }
3890 UniformedLinearThis = E;
3891 if (E->isValueDependent() || E->isTypeDependent() ||
3892 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3893 continue;
3894 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3895 E->getType());
3896 continue;
3897 }
3898 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3899 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3900 }
3901 Expr *Step = nullptr;
3902 Expr *NewStep = nullptr;
3903 SmallVector<Expr *, 4> NewSteps;
3904 for (auto *E : Steps) {
3905 // Skip the same step expression, it was checked already.
3906 if (Step == E || !E) {
3907 NewSteps.push_back(E ? NewStep : nullptr);
3908 continue;
3909 }
3910 Step = E;
3911 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3912 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3913 auto *CanonPVD = PVD->getCanonicalDecl();
3914 if (UniformedArgs.count(CanonPVD) == 0) {
3915 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3916 << Step->getSourceRange();
3917 } else if (E->isValueDependent() || E->isTypeDependent() ||
3918 E->isInstantiationDependent() ||
3919 E->containsUnexpandedParameterPack() ||
3920 CanonPVD->getType()->hasIntegerRepresentation())
3921 NewSteps.push_back(Step);
3922 else {
3923 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3924 << Step->getSourceRange();
3925 }
3926 continue;
3927 }
3928 NewStep = Step;
3929 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3930 !Step->isInstantiationDependent() &&
3931 !Step->containsUnexpandedParameterPack()) {
3932 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3933 .get();
3934 if (NewStep)
3935 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3936 }
3937 NewSteps.push_back(NewStep);
3938 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003939 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3940 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003941 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003942 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3943 const_cast<Expr **>(Linears.data()), Linears.size(),
3944 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3945 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003946 ADecl->addAttr(NewAttr);
3947 return ConvertDeclToDeclGroup(ADecl);
3948}
3949
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003950StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3951 Stmt *AStmt,
3952 SourceLocation StartLoc,
3953 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003954 if (!AStmt)
3955 return StmtError();
3956
Alexey Bataev9959db52014-05-06 10:08:46 +00003957 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3958 // 1.2.2 OpenMP Language Terminology
3959 // Structured block - An executable statement with a single entry at the
3960 // top and a single exit at the bottom.
3961 // The point of exit cannot be a branch out of the structured block.
3962 // longjmp() and throw() must not violate the entry/exit criteria.
3963 CS->getCapturedDecl()->setNothrow();
3964
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003965 getCurFunction()->setHasBranchProtectedScope();
3966
Alexey Bataev25e5b442015-09-15 12:52:43 +00003967 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3968 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003969}
3970
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003971namespace {
3972/// \brief Helper class for checking canonical form of the OpenMP loops and
3973/// extracting iteration space of each loop in the loop nest, that will be used
3974/// for IR generation.
3975class OpenMPIterationSpaceChecker {
3976 /// \brief Reference to Sema.
3977 Sema &SemaRef;
3978 /// \brief A location for diagnostics (when there is no some better location).
3979 SourceLocation DefaultLoc;
3980 /// \brief A location for diagnostics (when increment is not compatible).
3981 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003982 /// \brief A source location for referring to loop init later.
3983 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003984 /// \brief A source location for referring to condition later.
3985 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003986 /// \brief A source location for referring to increment later.
3987 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003988 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003989 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003990 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003991 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003992 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003993 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003994 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003995 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003996 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003997 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003998 /// \brief This flag is true when condition is one of:
3999 /// Var < UB
4000 /// Var <= UB
4001 /// UB > Var
4002 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004003 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004004 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004005 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004006 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004007 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004008
4009public:
4010 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004011 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004012 /// \brief Check init-expr for canonical loop form and save loop counter
4013 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00004014 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004015 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
4016 /// for less/greater and for strict/non-strict comparison.
4017 bool CheckCond(Expr *S);
4018 /// \brief Check incr-expr for canonical loop form and return true if it
4019 /// does not conform, otherwise save loop step (#Step).
4020 bool CheckInc(Expr *S);
4021 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004022 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004023 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004024 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004025 /// \brief Source range of the loop init.
4026 SourceRange GetInitSrcRange() const { return InitSrcRange; }
4027 /// \brief Source range of the loop condition.
4028 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
4029 /// \brief Source range of the loop increment.
4030 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
4031 /// \brief True if the step should be subtracted.
4032 bool ShouldSubtractStep() const { return SubtractStep; }
4033 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004034 Expr *
4035 BuildNumIterations(Scope *S, const bool LimitedType,
4036 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00004037 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004038 Expr *BuildPreCond(Scope *S, Expr *Cond,
4039 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004040 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004041 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
4042 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00004043 /// \brief Build reference expression to the private counter be used for
4044 /// codegen.
4045 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004046 /// \brief Build initization of the counter be used for codegen.
4047 Expr *BuildCounterInit() const;
4048 /// \brief Build step of the counter be used for codegen.
4049 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004050 /// \brief Return true if any expression is dependent.
4051 bool Dependent() const;
4052
4053private:
4054 /// \brief Check the right-hand side of an assignment in the increment
4055 /// expression.
4056 bool CheckIncRHS(Expr *RHS);
4057 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004058 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004059 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00004060 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00004061 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004062 /// \brief Helper to set loop increment.
4063 bool SetStep(Expr *NewStep, bool Subtract);
4064};
4065
4066bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004067 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004068 assert(!LB && !UB && !Step);
4069 return false;
4070 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004071 return LCDecl->getType()->isDependentType() ||
4072 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4073 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004074}
4075
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004076static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004077 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4078 E = ExprTemp->getSubExpr();
4079
4080 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4081 E = MTE->GetTemporaryExpr();
4082
4083 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4084 E = Binder->getSubExpr();
4085
4086 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4087 E = ICE->getSubExprAsWritten();
4088 return E->IgnoreParens();
4089}
4090
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004091bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4092 Expr *NewLCRefExpr,
4093 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004094 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004095 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004096 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004097 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004098 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004099 LCDecl = getCanonicalDecl(NewLCDecl);
4100 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004101 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4102 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004103 if ((Ctor->isCopyOrMoveConstructor() ||
4104 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4105 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004106 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004107 LB = NewLB;
4108 return false;
4109}
4110
4111bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004112 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004113 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004114 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4115 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004116 if (!NewUB)
4117 return true;
4118 UB = NewUB;
4119 TestIsLessOp = LessOp;
4120 TestIsStrictOp = StrictOp;
4121 ConditionSrcRange = SR;
4122 ConditionLoc = SL;
4123 return false;
4124}
4125
4126bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4127 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004128 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004129 if (!NewStep)
4130 return true;
4131 if (!NewStep->isValueDependent()) {
4132 // Check that the step is integer expression.
4133 SourceLocation StepLoc = NewStep->getLocStart();
4134 ExprResult Val =
4135 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4136 if (Val.isInvalid())
4137 return true;
4138 NewStep = Val.get();
4139
4140 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4141 // If test-expr is of form var relational-op b and relational-op is < or
4142 // <= then incr-expr must cause var to increase on each iteration of the
4143 // loop. If test-expr is of form var relational-op b and relational-op is
4144 // > or >= then incr-expr must cause var to decrease on each iteration of
4145 // the loop.
4146 // If test-expr is of form b relational-op var and relational-op is < or
4147 // <= then incr-expr must cause var to decrease on each iteration of the
4148 // loop. If test-expr is of form b relational-op var and relational-op is
4149 // > or >= then incr-expr must cause var to increase on each iteration of
4150 // the loop.
4151 llvm::APSInt Result;
4152 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4153 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4154 bool IsConstNeg =
4155 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004156 bool IsConstPos =
4157 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004158 bool IsConstZero = IsConstant && !Result.getBoolValue();
4159 if (UB && (IsConstZero ||
4160 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004161 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004162 SemaRef.Diag(NewStep->getExprLoc(),
4163 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004164 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004165 SemaRef.Diag(ConditionLoc,
4166 diag::note_omp_loop_cond_requres_compatible_incr)
4167 << TestIsLessOp << ConditionSrcRange;
4168 return true;
4169 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004170 if (TestIsLessOp == Subtract) {
4171 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4172 NewStep).get();
4173 Subtract = !Subtract;
4174 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004175 }
4176
4177 Step = NewStep;
4178 SubtractStep = Subtract;
4179 return false;
4180}
4181
Alexey Bataev9c821032015-04-30 04:23:23 +00004182bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004183 // Check init-expr for canonical loop form and save loop counter
4184 // variable - #Var and its initialization value - #LB.
4185 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4186 // var = lb
4187 // integer-type var = lb
4188 // random-access-iterator-type var = lb
4189 // pointer-type var = lb
4190 //
4191 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004192 if (EmitDiags) {
4193 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4194 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 return true;
4196 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004197 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4198 if (!ExprTemp->cleanupsHaveSideEffects())
4199 S = ExprTemp->getSubExpr();
4200
Alexander Musmana5f070a2014-10-01 06:03:56 +00004201 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004202 if (Expr *E = dyn_cast<Expr>(S))
4203 S = E->IgnoreParens();
4204 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004205 if (BO->getOpcode() == BO_Assign) {
4206 auto *LHS = BO->getLHS()->IgnoreParens();
4207 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4208 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4209 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4210 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4211 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4212 }
4213 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4214 if (ME->isArrow() &&
4215 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4216 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4217 }
4218 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004219 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4220 if (DS->isSingleDecl()) {
4221 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004222 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004223 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004224 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004225 SemaRef.Diag(S->getLocStart(),
4226 diag::ext_omp_loop_not_canonical_init)
4227 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004228 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004229 }
4230 }
4231 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004232 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4233 if (CE->getOperator() == OO_Equal) {
4234 auto *LHS = CE->getArg(0);
4235 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4236 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4237 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4238 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4239 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4240 }
4241 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4242 if (ME->isArrow() &&
4243 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4244 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4245 }
4246 }
4247 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004248
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004249 if (Dependent() || SemaRef.CurContext->isDependentContext())
4250 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004251 if (EmitDiags) {
4252 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4253 << S->getSourceRange();
4254 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004255 return true;
4256}
4257
Alexey Bataev23b69422014-06-18 07:08:49 +00004258/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004259/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004260static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004261 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004262 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004263 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004264 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4265 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004266 if ((Ctor->isCopyOrMoveConstructor() ||
4267 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4268 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004269 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004270 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4271 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4272 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4273 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4274 return getCanonicalDecl(ME->getMemberDecl());
4275 return getCanonicalDecl(VD);
4276 }
4277 }
4278 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4279 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4280 return getCanonicalDecl(ME->getMemberDecl());
4281 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004282}
4283
4284bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4285 // Check test-expr for canonical form, save upper-bound UB, flags for
4286 // less/greater and for strict/non-strict comparison.
4287 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4288 // var relational-op b
4289 // b relational-op var
4290 //
4291 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004292 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004293 return true;
4294 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004295 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004296 SourceLocation CondLoc = S->getLocStart();
4297 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4298 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004299 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004300 return SetUB(BO->getRHS(),
4301 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4302 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4303 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004304 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004305 return SetUB(BO->getLHS(),
4306 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4307 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4308 BO->getSourceRange(), BO->getOperatorLoc());
4309 }
4310 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4311 if (CE->getNumArgs() == 2) {
4312 auto Op = CE->getOperator();
4313 switch (Op) {
4314 case OO_Greater:
4315 case OO_GreaterEqual:
4316 case OO_Less:
4317 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004318 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004319 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4320 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4321 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004322 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004323 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4324 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4325 CE->getOperatorLoc());
4326 break;
4327 default:
4328 break;
4329 }
4330 }
4331 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004332 if (Dependent() || SemaRef.CurContext->isDependentContext())
4333 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004334 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004335 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004336 return true;
4337}
4338
4339bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4340 // RHS of canonical loop form increment can be:
4341 // var + incr
4342 // incr + var
4343 // var - incr
4344 //
4345 RHS = RHS->IgnoreParenImpCasts();
4346 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4347 if (BO->isAdditiveOp()) {
4348 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004349 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004350 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004351 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004352 return SetStep(BO->getLHS(), false);
4353 }
4354 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4355 bool IsAdd = CE->getOperator() == OO_Plus;
4356 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004357 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004358 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004359 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004360 return SetStep(CE->getArg(0), false);
4361 }
4362 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004363 if (Dependent() || SemaRef.CurContext->isDependentContext())
4364 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004365 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004366 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004367 return true;
4368}
4369
4370bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4371 // Check incr-expr for canonical loop form and return true if it
4372 // does not conform.
4373 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4374 // ++var
4375 // var++
4376 // --var
4377 // var--
4378 // var += incr
4379 // var -= incr
4380 // var = var + incr
4381 // var = incr + var
4382 // var = var - incr
4383 //
4384 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004385 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004386 return true;
4387 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004388 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4389 if (!ExprTemp->cleanupsHaveSideEffects())
4390 S = ExprTemp->getSubExpr();
4391
Alexander Musmana5f070a2014-10-01 06:03:56 +00004392 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004393 S = S->IgnoreParens();
4394 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004395 if (UO->isIncrementDecrementOp() &&
4396 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004397 return SetStep(
4398 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4399 (UO->isDecrementOp() ? -1 : 1)).get(),
4400 false);
4401 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4402 switch (BO->getOpcode()) {
4403 case BO_AddAssign:
4404 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004405 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004406 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4407 break;
4408 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004409 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004410 return CheckIncRHS(BO->getRHS());
4411 break;
4412 default:
4413 break;
4414 }
4415 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4416 switch (CE->getOperator()) {
4417 case OO_PlusPlus:
4418 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004419 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004420 return SetStep(
4421 SemaRef.ActOnIntegerConstant(
4422 CE->getLocStart(),
4423 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4424 false);
4425 break;
4426 case OO_PlusEqual:
4427 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004428 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004429 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4430 break;
4431 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004432 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004433 return CheckIncRHS(CE->getArg(1));
4434 break;
4435 default:
4436 break;
4437 }
4438 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004439 if (Dependent() || SemaRef.CurContext->isDependentContext())
4440 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004441 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004442 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004443 return true;
4444}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004445
Alexey Bataev5a3af132016-03-29 08:58:54 +00004446static ExprResult
4447tryBuildCapture(Sema &SemaRef, Expr *Capture,
4448 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004449 if (SemaRef.CurContext->isDependentContext())
4450 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004451 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4452 return SemaRef.PerformImplicitConversion(
4453 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4454 /*AllowExplicit=*/true);
4455 auto I = Captures.find(Capture);
4456 if (I != Captures.end())
4457 return buildCapture(SemaRef, Capture, I->second);
4458 DeclRefExpr *Ref = nullptr;
4459 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4460 Captures[Capture] = Ref;
4461 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004462}
4463
Alexander Musmana5f070a2014-10-01 06:03:56 +00004464/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004465Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4466 Scope *S, const bool LimitedType,
4467 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004468 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004469 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004470 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004471 SemaRef.getLangOpts().CPlusPlus) {
4472 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004473 auto *UBExpr = TestIsLessOp ? UB : LB;
4474 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004475 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4476 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004477 if (!Upper || !Lower)
4478 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004479
4480 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4481
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004482 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004483 // BuildBinOp already emitted error, this one is to point user to upper
4484 // and lower bound, and to tell what is passed to 'operator-'.
4485 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4486 << Upper->getSourceRange() << Lower->getSourceRange();
4487 return nullptr;
4488 }
4489 }
4490
4491 if (!Diff.isUsable())
4492 return nullptr;
4493
4494 // Upper - Lower [- 1]
4495 if (TestIsStrictOp)
4496 Diff = SemaRef.BuildBinOp(
4497 S, DefaultLoc, BO_Sub, Diff.get(),
4498 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4499 if (!Diff.isUsable())
4500 return nullptr;
4501
4502 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004503 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4504 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004505 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004506 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004507 if (!Diff.isUsable())
4508 return nullptr;
4509
4510 // Parentheses (for dumping/debugging purposes only).
4511 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4512 if (!Diff.isUsable())
4513 return nullptr;
4514
4515 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004516 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004517 if (!Diff.isUsable())
4518 return nullptr;
4519
Alexander Musman174b3ca2014-10-06 11:16:29 +00004520 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004521 QualType Type = Diff.get()->getType();
4522 auto &C = SemaRef.Context;
4523 bool UseVarType = VarType->hasIntegerRepresentation() &&
4524 C.getTypeSize(Type) > C.getTypeSize(VarType);
4525 if (!Type->isIntegerType() || UseVarType) {
4526 unsigned NewSize =
4527 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4528 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4529 : Type->hasSignedIntegerRepresentation();
4530 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004531 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4532 Diff = SemaRef.PerformImplicitConversion(
4533 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4534 if (!Diff.isUsable())
4535 return nullptr;
4536 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004537 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004538 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004539 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4540 if (NewSize != C.getTypeSize(Type)) {
4541 if (NewSize < C.getTypeSize(Type)) {
4542 assert(NewSize == 64 && "incorrect loop var size");
4543 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4544 << InitSrcRange << ConditionSrcRange;
4545 }
4546 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004547 NewSize, Type->hasSignedIntegerRepresentation() ||
4548 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004549 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4550 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4551 Sema::AA_Converting, true);
4552 if (!Diff.isUsable())
4553 return nullptr;
4554 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004555 }
4556 }
4557
Alexander Musmana5f070a2014-10-01 06:03:56 +00004558 return Diff.get();
4559}
4560
Alexey Bataev5a3af132016-03-29 08:58:54 +00004561Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4562 Scope *S, Expr *Cond,
4563 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004564 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4565 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4566 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004567
Alexey Bataev5a3af132016-03-29 08:58:54 +00004568 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4569 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4570 if (!NewLB.isUsable() || !NewUB.isUsable())
4571 return nullptr;
4572
Alexey Bataev62dbb972015-04-22 11:59:37 +00004573 auto CondExpr = SemaRef.BuildBinOp(
4574 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4575 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004576 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004577 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004578 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4579 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004580 CondExpr = SemaRef.PerformImplicitConversion(
4581 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4582 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004583 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004584 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4585 // Otherwise use original loop conditon and evaluate it in runtime.
4586 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4587}
4588
Alexander Musmana5f070a2014-10-01 06:03:56 +00004589/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004590DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004591 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004592 auto *VD = dyn_cast<VarDecl>(LCDecl);
4593 if (!VD) {
4594 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4595 auto *Ref = buildDeclRefExpr(
4596 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004597 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4598 // If the loop control decl is explicitly marked as private, do not mark it
4599 // as captured again.
4600 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4601 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004602 return Ref;
4603 }
4604 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004605 DefaultLoc);
4606}
4607
4608Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004609 if (LCDecl && !LCDecl->isInvalidDecl()) {
4610 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004611 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004612 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4613 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004614 if (PrivateVar->isInvalidDecl())
4615 return nullptr;
4616 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4617 }
4618 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004619}
4620
4621/// \brief Build initization of the counter be used for codegen.
4622Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4623
4624/// \brief Build step of the counter be used for codegen.
4625Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4626
4627/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004628struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004629 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004630 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004631 /// \brief This expression calculates the number of iterations in the loop.
4632 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004633 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004634 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004635 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004636 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004637 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004638 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004639 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004640 /// \brief This is step for the #CounterVar used to generate its update:
4641 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004642 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004643 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004644 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004645 /// \brief Source range of the loop init.
4646 SourceRange InitSrcRange;
4647 /// \brief Source range of the loop condition.
4648 SourceRange CondSrcRange;
4649 /// \brief Source range of the loop increment.
4650 SourceRange IncSrcRange;
4651};
4652
Alexey Bataev23b69422014-06-18 07:08:49 +00004653} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004654
Alexey Bataev9c821032015-04-30 04:23:23 +00004655void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4656 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4657 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004658 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4659 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004660 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4661 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004662 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4663 if (auto *D = ISC.GetLoopDecl()) {
4664 auto *VD = dyn_cast<VarDecl>(D);
4665 if (!VD) {
4666 if (auto *Private = IsOpenMPCapturedDecl(D))
4667 VD = Private;
4668 else {
4669 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4670 /*WithInit=*/false);
4671 VD = cast<VarDecl>(Ref->getDecl());
4672 }
4673 }
4674 DSAStack->addLoopControlVariable(D, VD);
4675 }
4676 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004677 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004678 }
4679}
4680
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004681/// \brief Called on a for stmt to check and extract its iteration space
4682/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004683static bool CheckOpenMPIterationSpace(
4684 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4685 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004686 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004687 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004688 LoopIterationSpace &ResultIterSpace,
4689 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004690 // OpenMP [2.6, Canonical Loop Form]
4691 // for (init-expr; test-expr; incr-expr) structured-block
4692 auto For = dyn_cast_or_null<ForStmt>(S);
4693 if (!For) {
4694 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004695 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4696 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4697 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4698 if (NestedLoopCount > 1) {
4699 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4700 SemaRef.Diag(DSA.getConstructLoc(),
4701 diag::note_omp_collapse_ordered_expr)
4702 << 2 << CollapseLoopCountExpr->getSourceRange()
4703 << OrderedLoopCountExpr->getSourceRange();
4704 else if (CollapseLoopCountExpr)
4705 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4706 diag::note_omp_collapse_ordered_expr)
4707 << 0 << CollapseLoopCountExpr->getSourceRange();
4708 else
4709 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4710 diag::note_omp_collapse_ordered_expr)
4711 << 1 << OrderedLoopCountExpr->getSourceRange();
4712 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004713 return true;
4714 }
4715 assert(For->getBody());
4716
4717 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4718
4719 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004720 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004721 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004722 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004723
4724 bool HasErrors = false;
4725
4726 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004727 if (auto *LCDecl = ISC.GetLoopDecl()) {
4728 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004729
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004730 // OpenMP [2.6, Canonical Loop Form]
4731 // Var is one of the following:
4732 // A variable of signed or unsigned integer type.
4733 // For C++, a variable of a random access iterator type.
4734 // For C, a variable of a pointer type.
4735 auto VarType = LCDecl->getType().getNonReferenceType();
4736 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4737 !VarType->isPointerType() &&
4738 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4739 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4740 << SemaRef.getLangOpts().CPlusPlus;
4741 HasErrors = true;
4742 }
4743
4744 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4745 // a Construct
4746 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4747 // parallel for construct is (are) private.
4748 // The loop iteration variable in the associated for-loop of a simd
4749 // construct with just one associated for-loop is linear with a
4750 // constant-linear-step that is the increment of the associated for-loop.
4751 // Exclude loop var from the list of variables with implicitly defined data
4752 // sharing attributes.
4753 VarsWithImplicitDSA.erase(LCDecl);
4754
4755 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4756 // in a Construct, C/C++].
4757 // The loop iteration variable in the associated for-loop of a simd
4758 // construct with just one associated for-loop may be listed in a linear
4759 // clause with a constant-linear-step that is the increment of the
4760 // associated for-loop.
4761 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4762 // parallel for construct may be listed in a private or lastprivate clause.
4763 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4764 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4765 // declared in the loop and it is predetermined as a private.
4766 auto PredeterminedCKind =
4767 isOpenMPSimdDirective(DKind)
4768 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4769 : OMPC_private;
4770 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4771 DVar.CKind != PredeterminedCKind) ||
4772 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4773 isOpenMPDistributeDirective(DKind)) &&
4774 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4775 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4776 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4777 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4778 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4779 << getOpenMPClauseName(PredeterminedCKind);
4780 if (DVar.RefExpr == nullptr)
4781 DVar.CKind = PredeterminedCKind;
4782 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4783 HasErrors = true;
4784 } else if (LoopDeclRefExpr != nullptr) {
4785 // Make the loop iteration variable private (for worksharing constructs),
4786 // linear (for simd directives with the only one associated loop) or
4787 // lastprivate (for simd directives with several collapsed or ordered
4788 // loops).
4789 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004790 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4791 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004792 /*FromParent=*/false);
4793 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4794 }
4795
4796 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4797
4798 // Check test-expr.
4799 HasErrors |= ISC.CheckCond(For->getCond());
4800
4801 // Check incr-expr.
4802 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004803 }
4804
Alexander Musmana5f070a2014-10-01 06:03:56 +00004805 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004806 return HasErrors;
4807
Alexander Musmana5f070a2014-10-01 06:03:56 +00004808 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004809 ResultIterSpace.PreCond =
4810 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004811 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004812 DSA.getCurScope(),
4813 (isOpenMPWorksharingDirective(DKind) ||
4814 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4815 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004816 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004817 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004818 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4819 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4820 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4821 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4822 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4823 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4824
Alexey Bataev62dbb972015-04-22 11:59:37 +00004825 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4826 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004827 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004828 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004829 ResultIterSpace.CounterInit == nullptr ||
4830 ResultIterSpace.CounterStep == nullptr);
4831
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004832 return HasErrors;
4833}
4834
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004835/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004836static ExprResult
4837BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4838 ExprResult Start,
4839 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004840 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004841 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4842 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004843 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004844 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004845 VarRef.get()->getType())) {
4846 NewStart = SemaRef.PerformImplicitConversion(
4847 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4848 /*AllowExplicit=*/true);
4849 if (!NewStart.isUsable())
4850 return ExprError();
4851 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004852
4853 auto Init =
4854 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4855 return Init;
4856}
4857
Alexander Musmana5f070a2014-10-01 06:03:56 +00004858/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004859static ExprResult
4860BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4861 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4862 ExprResult Step, bool Subtract,
4863 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004864 // Add parentheses (for debugging purposes only).
4865 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4866 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4867 !Step.isUsable())
4868 return ExprError();
4869
Alexey Bataev5a3af132016-03-29 08:58:54 +00004870 ExprResult NewStep = Step;
4871 if (Captures)
4872 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004873 if (NewStep.isInvalid())
4874 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004875 ExprResult Update =
4876 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004877 if (!Update.isUsable())
4878 return ExprError();
4879
Alexey Bataevc0214e02016-02-16 12:13:49 +00004880 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4881 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004882 ExprResult NewStart = Start;
4883 if (Captures)
4884 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004885 if (NewStart.isInvalid())
4886 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004887
Alexey Bataevc0214e02016-02-16 12:13:49 +00004888 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4889 ExprResult SavedUpdate = Update;
4890 ExprResult UpdateVal;
4891 if (VarRef.get()->getType()->isOverloadableType() ||
4892 NewStart.get()->getType()->isOverloadableType() ||
4893 Update.get()->getType()->isOverloadableType()) {
4894 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4895 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4896 Update =
4897 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4898 if (Update.isUsable()) {
4899 UpdateVal =
4900 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4901 VarRef.get(), SavedUpdate.get());
4902 if (UpdateVal.isUsable()) {
4903 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4904 UpdateVal.get());
4905 }
4906 }
4907 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4908 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004909
Alexey Bataevc0214e02016-02-16 12:13:49 +00004910 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4911 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4912 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4913 NewStart.get(), SavedUpdate.get());
4914 if (!Update.isUsable())
4915 return ExprError();
4916
Alexey Bataev11481f52016-02-17 10:29:05 +00004917 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4918 VarRef.get()->getType())) {
4919 Update = SemaRef.PerformImplicitConversion(
4920 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4921 if (!Update.isUsable())
4922 return ExprError();
4923 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004924
4925 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4926 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004927 return Update;
4928}
4929
4930/// \brief Convert integer expression \a E to make it have at least \a Bits
4931/// bits.
4932static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4933 Sema &SemaRef) {
4934 if (E == nullptr)
4935 return ExprError();
4936 auto &C = SemaRef.Context;
4937 QualType OldType = E->getType();
4938 unsigned HasBits = C.getTypeSize(OldType);
4939 if (HasBits >= Bits)
4940 return ExprResult(E);
4941 // OK to convert to signed, because new type has more bits than old.
4942 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4943 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4944 true);
4945}
4946
4947/// \brief Check if the given expression \a E is a constant integer that fits
4948/// into \a Bits bits.
4949static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4950 if (E == nullptr)
4951 return false;
4952 llvm::APSInt Result;
4953 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4954 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4955 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004956}
4957
Alexey Bataev5a3af132016-03-29 08:58:54 +00004958/// Build preinits statement for the given declarations.
4959static Stmt *buildPreInits(ASTContext &Context,
4960 SmallVectorImpl<Decl *> &PreInits) {
4961 if (!PreInits.empty()) {
4962 return new (Context) DeclStmt(
4963 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4964 SourceLocation(), SourceLocation());
4965 }
4966 return nullptr;
4967}
4968
4969/// Build preinits statement for the given declarations.
4970static Stmt *buildPreInits(ASTContext &Context,
4971 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4972 if (!Captures.empty()) {
4973 SmallVector<Decl *, 16> PreInits;
4974 for (auto &Pair : Captures)
4975 PreInits.push_back(Pair.second->getDecl());
4976 return buildPreInits(Context, PreInits);
4977 }
4978 return nullptr;
4979}
4980
4981/// Build postupdate expression for the given list of postupdates expressions.
4982static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4983 Expr *PostUpdate = nullptr;
4984 if (!PostUpdates.empty()) {
4985 for (auto *E : PostUpdates) {
4986 Expr *ConvE = S.BuildCStyleCastExpr(
4987 E->getExprLoc(),
4988 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4989 E->getExprLoc(), E)
4990 .get();
4991 PostUpdate = PostUpdate
4992 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4993 PostUpdate, ConvE)
4994 .get()
4995 : ConvE;
4996 }
4997 }
4998 return PostUpdate;
4999}
5000
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005001/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005002/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5003/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005004static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00005005CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
5006 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5007 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005008 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005009 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005010 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005011 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005012 // Found 'collapse' clause - calculate collapse number.
5013 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005014 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005015 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005016 }
5017 if (OrderedLoopCountExpr) {
5018 // Found 'ordered' clause - calculate collapse number.
5019 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005020 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
5021 if (Result.getLimitedValue() < NestedLoopCount) {
5022 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5023 diag::err_omp_wrong_ordered_loop_count)
5024 << OrderedLoopCountExpr->getSourceRange();
5025 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5026 diag::note_collapse_loop_count)
5027 << CollapseLoopCountExpr->getSourceRange();
5028 }
5029 NestedLoopCount = Result.getLimitedValue();
5030 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005031 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005032 // This is helper routine for loop directives (e.g., 'for', 'simd',
5033 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00005034 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005035 SmallVector<LoopIterationSpace, 4> IterSpaces;
5036 IterSpaces.resize(NestedLoopCount);
5037 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005038 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005039 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005040 NestedLoopCount, CollapseLoopCountExpr,
5041 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005042 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005043 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005044 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005045 // OpenMP [2.8.1, simd construct, Restrictions]
5046 // All loops associated with the construct must be perfectly nested; that
5047 // is, there must be no intervening code nor any OpenMP directive between
5048 // any two loops.
5049 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005050 }
5051
Alexander Musmana5f070a2014-10-01 06:03:56 +00005052 Built.clear(/* size */ NestedLoopCount);
5053
5054 if (SemaRef.CurContext->isDependentContext())
5055 return NestedLoopCount;
5056
5057 // An example of what is generated for the following code:
5058 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005059 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005060 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005061 // for (k = 0; k < NK; ++k)
5062 // for (j = J0; j < NJ; j+=2) {
5063 // <loop body>
5064 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005065 //
5066 // We generate the code below.
5067 // Note: the loop body may be outlined in CodeGen.
5068 // Note: some counters may be C++ classes, operator- is used to find number of
5069 // iterations and operator+= to calculate counter value.
5070 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5071 // or i64 is currently supported).
5072 //
5073 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5074 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5075 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5076 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5077 // // similar updates for vars in clauses (e.g. 'linear')
5078 // <loop body (using local i and j)>
5079 // }
5080 // i = NI; // assign final values of counters
5081 // j = NJ;
5082 //
5083
5084 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5085 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005086 // Precondition tests if there is at least one iteration (all conditions are
5087 // true).
5088 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005089 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005090 ExprResult LastIteration32 = WidenIterationCount(
5091 32 /* Bits */, SemaRef.PerformImplicitConversion(
5092 N0->IgnoreImpCasts(), N0->getType(),
5093 Sema::AA_Converting, /*AllowExplicit=*/true)
5094 .get(),
5095 SemaRef);
5096 ExprResult LastIteration64 = WidenIterationCount(
5097 64 /* Bits */, SemaRef.PerformImplicitConversion(
5098 N0->IgnoreImpCasts(), N0->getType(),
5099 Sema::AA_Converting, /*AllowExplicit=*/true)
5100 .get(),
5101 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005102
5103 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5104 return NestedLoopCount;
5105
5106 auto &C = SemaRef.Context;
5107 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5108
5109 Scope *CurScope = DSA.getCurScope();
5110 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005111 if (PreCond.isUsable()) {
5112 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5113 PreCond.get(), IterSpaces[Cnt].PreCond);
5114 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005115 auto N = IterSpaces[Cnt].NumIterations;
5116 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5117 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005118 LastIteration32 = SemaRef.BuildBinOp(
5119 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5120 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5121 Sema::AA_Converting,
5122 /*AllowExplicit=*/true)
5123 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005124 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005125 LastIteration64 = SemaRef.BuildBinOp(
5126 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5127 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5128 Sema::AA_Converting,
5129 /*AllowExplicit=*/true)
5130 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005131 }
5132
5133 // Choose either the 32-bit or 64-bit version.
5134 ExprResult LastIteration = LastIteration64;
5135 if (LastIteration32.isUsable() &&
5136 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5137 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5138 FitsInto(
5139 32 /* Bits */,
5140 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5141 LastIteration64.get(), SemaRef)))
5142 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005143 QualType VType = LastIteration.get()->getType();
5144 QualType RealVType = VType;
5145 QualType StrideVType = VType;
5146 if (isOpenMPTaskLoopDirective(DKind)) {
5147 VType =
5148 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5149 StrideVType =
5150 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5151 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005152
5153 if (!LastIteration.isUsable())
5154 return 0;
5155
5156 // Save the number of iterations.
5157 ExprResult NumIterations = LastIteration;
5158 {
5159 LastIteration = SemaRef.BuildBinOp(
5160 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5161 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5162 if (!LastIteration.isUsable())
5163 return 0;
5164 }
5165
5166 // Calculate the last iteration number beforehand instead of doing this on
5167 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5168 llvm::APSInt Result;
5169 bool IsConstant =
5170 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5171 ExprResult CalcLastIteration;
5172 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005173 ExprResult SaveRef =
5174 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005175 LastIteration = SaveRef;
5176
5177 // Prepare SaveRef + 1.
5178 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005179 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005180 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5181 if (!NumIterations.isUsable())
5182 return 0;
5183 }
5184
5185 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5186
Alexander Musmanc6388682014-12-15 07:07:06 +00005187 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005188 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005189 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5190 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005191 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005192 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5193 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005194 SemaRef.AddInitializerToDecl(
5195 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5196 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5197
5198 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005199 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5200 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005201 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5202 /*DirectInit*/ false,
5203 /*TypeMayContainAuto*/ false);
5204
5205 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5206 // This will be used to implement clause 'lastprivate'.
5207 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005208 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5209 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005210 SemaRef.AddInitializerToDecl(
5211 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5212 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5213
5214 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005215 VarDecl *STDecl =
5216 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5217 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005218 SemaRef.AddInitializerToDecl(
5219 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5220 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5221
5222 // Build expression: UB = min(UB, LastIteration)
5223 // It is nesessary for CodeGen of directives with static scheduling.
5224 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5225 UB.get(), LastIteration.get());
5226 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5227 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5228 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5229 CondOp.get());
5230 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005231
5232 // If we have a combined directive that combines 'distribute', 'for' or
5233 // 'simd' we need to be able to access the bounds of the schedule of the
5234 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5235 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5236 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5237 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5238
5239 // We expect to have at least 2 more parameters than the 'parallel'
5240 // directive does - the lower and upper bounds of the previous schedule.
5241 assert(CD->getNumParams() >= 4 &&
5242 "Unexpected number of parameters in loop combined directive");
5243
5244 // Set the proper type for the bounds given what we learned from the
5245 // enclosed loops.
5246 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5247 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5248
5249 // Previous lower and upper bounds are obtained from the region
5250 // parameters.
5251 PrevLB =
5252 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5253 PrevUB =
5254 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5255 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005256 }
5257
5258 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005259 ExprResult IV;
5260 ExprResult Init;
5261 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005262 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5263 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005264 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005265 isOpenMPTaskLoopDirective(DKind) ||
5266 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005267 ? LB.get()
5268 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5269 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5270 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005271 }
5272
Alexander Musmanc6388682014-12-15 07:07:06 +00005273 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005274 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005275 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005276 (isOpenMPWorksharingDirective(DKind) ||
5277 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005278 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5279 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5280 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005281
5282 // Loop increment (IV = IV + 1)
5283 SourceLocation IncLoc;
5284 ExprResult Inc =
5285 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5286 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5287 if (!Inc.isUsable())
5288 return 0;
5289 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005290 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5291 if (!Inc.isUsable())
5292 return 0;
5293
5294 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5295 // Used for directives with static scheduling.
5296 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005297 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5298 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005299 // LB + ST
5300 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5301 if (!NextLB.isUsable())
5302 return 0;
5303 // LB = LB + ST
5304 NextLB =
5305 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5306 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5307 if (!NextLB.isUsable())
5308 return 0;
5309 // UB + ST
5310 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5311 if (!NextUB.isUsable())
5312 return 0;
5313 // UB = UB + ST
5314 NextUB =
5315 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5316 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5317 if (!NextUB.isUsable())
5318 return 0;
5319 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005320
5321 // Build updates and final values of the loop counters.
5322 bool HasErrors = false;
5323 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005324 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005325 Built.Updates.resize(NestedLoopCount);
5326 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005327 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005328 {
5329 ExprResult Div;
5330 // Go from inner nested loop to outer.
5331 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5332 LoopIterationSpace &IS = IterSpaces[Cnt];
5333 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5334 // Build: Iter = (IV / Div) % IS.NumIters
5335 // where Div is product of previous iterations' IS.NumIters.
5336 ExprResult Iter;
5337 if (Div.isUsable()) {
5338 Iter =
5339 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5340 } else {
5341 Iter = IV;
5342 assert((Cnt == (int)NestedLoopCount - 1) &&
5343 "unusable div expected on first iteration only");
5344 }
5345
5346 if (Cnt != 0 && Iter.isUsable())
5347 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5348 IS.NumIterations);
5349 if (!Iter.isUsable()) {
5350 HasErrors = true;
5351 break;
5352 }
5353
Alexey Bataev39f915b82015-05-08 10:41:21 +00005354 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005355 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5356 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5357 IS.CounterVar->getExprLoc(),
5358 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005359 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005360 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005361 if (!Init.isUsable()) {
5362 HasErrors = true;
5363 break;
5364 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005365 ExprResult Update = BuildCounterUpdate(
5366 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5367 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005368 if (!Update.isUsable()) {
5369 HasErrors = true;
5370 break;
5371 }
5372
5373 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5374 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005375 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005376 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005377 if (!Final.isUsable()) {
5378 HasErrors = true;
5379 break;
5380 }
5381
5382 // Build Div for the next iteration: Div <- Div * IS.NumIters
5383 if (Cnt != 0) {
5384 if (Div.isUnset())
5385 Div = IS.NumIterations;
5386 else
5387 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5388 IS.NumIterations);
5389
5390 // Add parentheses (for debugging purposes only).
5391 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005392 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005393 if (!Div.isUsable()) {
5394 HasErrors = true;
5395 break;
5396 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005397 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005398 }
5399 if (!Update.isUsable() || !Final.isUsable()) {
5400 HasErrors = true;
5401 break;
5402 }
5403 // Save results
5404 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005405 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005406 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005407 Built.Updates[Cnt] = Update.get();
5408 Built.Finals[Cnt] = Final.get();
5409 }
5410 }
5411
5412 if (HasErrors)
5413 return 0;
5414
5415 // Save results
5416 Built.IterationVarRef = IV.get();
5417 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005418 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005419 Built.CalcLastIteration =
5420 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005421 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005422 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005423 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005424 Built.Init = Init.get();
5425 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005426 Built.LB = LB.get();
5427 Built.UB = UB.get();
5428 Built.IL = IL.get();
5429 Built.ST = ST.get();
5430 Built.EUB = EUB.get();
5431 Built.NLB = NextLB.get();
5432 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005433 Built.PrevLB = PrevLB.get();
5434 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005435
Alexey Bataev8b427062016-05-25 12:36:08 +00005436 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5437 // Fill data for doacross depend clauses.
5438 for (auto Pair : DSA.getDoacrossDependClauses()) {
5439 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5440 Pair.first->setCounterValue(CounterVal);
5441 else {
5442 if (NestedLoopCount != Pair.second.size() ||
5443 NestedLoopCount != LoopMultipliers.size() + 1) {
5444 // Erroneous case - clause has some problems.
5445 Pair.first->setCounterValue(CounterVal);
5446 continue;
5447 }
5448 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5449 auto I = Pair.second.rbegin();
5450 auto IS = IterSpaces.rbegin();
5451 auto ILM = LoopMultipliers.rbegin();
5452 Expr *UpCounterVal = CounterVal;
5453 Expr *Multiplier = nullptr;
5454 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5455 if (I->first) {
5456 assert(IS->CounterStep);
5457 Expr *NormalizedOffset =
5458 SemaRef
5459 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5460 I->first, IS->CounterStep)
5461 .get();
5462 if (Multiplier) {
5463 NormalizedOffset =
5464 SemaRef
5465 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5466 NormalizedOffset, Multiplier)
5467 .get();
5468 }
5469 assert(I->second == OO_Plus || I->second == OO_Minus);
5470 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5471 UpCounterVal =
5472 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5473 UpCounterVal, NormalizedOffset).get();
5474 }
5475 Multiplier = *ILM;
5476 ++I;
5477 ++IS;
5478 ++ILM;
5479 }
5480 Pair.first->setCounterValue(UpCounterVal);
5481 }
5482 }
5483
Alexey Bataevabfc0692014-06-25 06:52:00 +00005484 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005485}
5486
Alexey Bataev10e775f2015-07-30 11:36:16 +00005487static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005488 auto CollapseClauses =
5489 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5490 if (CollapseClauses.begin() != CollapseClauses.end())
5491 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005492 return nullptr;
5493}
5494
Alexey Bataev10e775f2015-07-30 11:36:16 +00005495static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005496 auto OrderedClauses =
5497 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5498 if (OrderedClauses.begin() != OrderedClauses.end())
5499 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005500 return nullptr;
5501}
5502
Kelvin Lic5609492016-07-15 04:39:07 +00005503static bool checkSimdlenSafelenSpecified(Sema &S,
5504 const ArrayRef<OMPClause *> Clauses) {
5505 OMPSafelenClause *Safelen = nullptr;
5506 OMPSimdlenClause *Simdlen = nullptr;
5507
5508 for (auto *Clause : Clauses) {
5509 if (Clause->getClauseKind() == OMPC_safelen)
5510 Safelen = cast<OMPSafelenClause>(Clause);
5511 else if (Clause->getClauseKind() == OMPC_simdlen)
5512 Simdlen = cast<OMPSimdlenClause>(Clause);
5513 if (Safelen && Simdlen)
5514 break;
5515 }
5516
5517 if (Simdlen && Safelen) {
5518 llvm::APSInt SimdlenRes, SafelenRes;
5519 auto SimdlenLength = Simdlen->getSimdlen();
5520 auto SafelenLength = Safelen->getSafelen();
5521 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5522 SimdlenLength->isInstantiationDependent() ||
5523 SimdlenLength->containsUnexpandedParameterPack())
5524 return false;
5525 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5526 SafelenLength->isInstantiationDependent() ||
5527 SafelenLength->containsUnexpandedParameterPack())
5528 return false;
5529 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5530 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5531 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5532 // If both simdlen and safelen clauses are specified, the value of the
5533 // simdlen parameter must be less than or equal to the value of the safelen
5534 // parameter.
5535 if (SimdlenRes > SafelenRes) {
5536 S.Diag(SimdlenLength->getExprLoc(),
5537 diag::err_omp_wrong_simdlen_safelen_values)
5538 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5539 return true;
5540 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005541 }
5542 return false;
5543}
5544
Alexey Bataev4acb8592014-07-07 13:01:15 +00005545StmtResult Sema::ActOnOpenMPSimdDirective(
5546 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5547 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005548 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005549 if (!AStmt)
5550 return StmtError();
5551
5552 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005553 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005554 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5555 // define the nested loops number.
5556 unsigned NestedLoopCount = CheckOpenMPLoop(
5557 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5558 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005559 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005560 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005561
Alexander Musmana5f070a2014-10-01 06:03:56 +00005562 assert((CurContext->isDependentContext() || B.builtAll()) &&
5563 "omp simd loop exprs were not built");
5564
Alexander Musman3276a272015-03-21 10:12:56 +00005565 if (!CurContext->isDependentContext()) {
5566 // Finalize the clauses that need pre-built expressions for CodeGen.
5567 for (auto C : Clauses) {
5568 if (auto LC = dyn_cast<OMPLinearClause>(C))
5569 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005570 B.NumIterations, *this, CurScope,
5571 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005572 return StmtError();
5573 }
5574 }
5575
Kelvin Lic5609492016-07-15 04:39:07 +00005576 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005577 return StmtError();
5578
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005579 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005580 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5581 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005582}
5583
Alexey Bataev4acb8592014-07-07 13:01:15 +00005584StmtResult Sema::ActOnOpenMPForDirective(
5585 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5586 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005587 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005588 if (!AStmt)
5589 return StmtError();
5590
5591 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005592 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005593 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5594 // define the nested loops number.
5595 unsigned NestedLoopCount = CheckOpenMPLoop(
5596 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5597 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005598 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005599 return StmtError();
5600
Alexander Musmana5f070a2014-10-01 06:03:56 +00005601 assert((CurContext->isDependentContext() || B.builtAll()) &&
5602 "omp for loop exprs were not built");
5603
Alexey Bataev54acd402015-08-04 11:18:19 +00005604 if (!CurContext->isDependentContext()) {
5605 // Finalize the clauses that need pre-built expressions for CodeGen.
5606 for (auto C : Clauses) {
5607 if (auto LC = dyn_cast<OMPLinearClause>(C))
5608 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005609 B.NumIterations, *this, CurScope,
5610 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005611 return StmtError();
5612 }
5613 }
5614
Alexey Bataevf29276e2014-06-18 04:14:57 +00005615 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005616 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005617 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005618}
5619
Alexander Musmanf82886e2014-09-18 05:12:34 +00005620StmtResult Sema::ActOnOpenMPForSimdDirective(
5621 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5622 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005623 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005624 if (!AStmt)
5625 return StmtError();
5626
5627 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005628 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005629 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5630 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005631 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005632 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5633 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5634 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005635 if (NestedLoopCount == 0)
5636 return StmtError();
5637
Alexander Musmanc6388682014-12-15 07:07:06 +00005638 assert((CurContext->isDependentContext() || B.builtAll()) &&
5639 "omp for simd loop exprs were not built");
5640
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005641 if (!CurContext->isDependentContext()) {
5642 // Finalize the clauses that need pre-built expressions for CodeGen.
5643 for (auto C : Clauses) {
5644 if (auto LC = dyn_cast<OMPLinearClause>(C))
5645 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005646 B.NumIterations, *this, CurScope,
5647 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005648 return StmtError();
5649 }
5650 }
5651
Kelvin Lic5609492016-07-15 04:39:07 +00005652 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005653 return StmtError();
5654
Alexander Musmanf82886e2014-09-18 05:12:34 +00005655 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005656 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5657 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005658}
5659
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005660StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5661 Stmt *AStmt,
5662 SourceLocation StartLoc,
5663 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005664 if (!AStmt)
5665 return StmtError();
5666
5667 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005668 auto BaseStmt = AStmt;
5669 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5670 BaseStmt = CS->getCapturedStmt();
5671 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5672 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005673 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005674 return StmtError();
5675 // All associated statements must be '#pragma omp section' except for
5676 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005677 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005678 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5679 if (SectionStmt)
5680 Diag(SectionStmt->getLocStart(),
5681 diag::err_omp_sections_substmt_not_section);
5682 return StmtError();
5683 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005684 cast<OMPSectionDirective>(SectionStmt)
5685 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005686 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005687 } else {
5688 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5689 return StmtError();
5690 }
5691
5692 getCurFunction()->setHasBranchProtectedScope();
5693
Alexey Bataev25e5b442015-09-15 12:52:43 +00005694 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5695 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005696}
5697
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005698StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5699 SourceLocation StartLoc,
5700 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005701 if (!AStmt)
5702 return StmtError();
5703
5704 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005705
5706 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005707 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005708
Alexey Bataev25e5b442015-09-15 12:52:43 +00005709 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5710 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005711}
5712
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005713StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5714 Stmt *AStmt,
5715 SourceLocation StartLoc,
5716 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005717 if (!AStmt)
5718 return StmtError();
5719
5720 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005721
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005722 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005723
Alexey Bataev3255bf32015-01-19 05:20:46 +00005724 // OpenMP [2.7.3, single Construct, Restrictions]
5725 // The copyprivate clause must not be used with the nowait clause.
5726 OMPClause *Nowait = nullptr;
5727 OMPClause *Copyprivate = nullptr;
5728 for (auto *Clause : Clauses) {
5729 if (Clause->getClauseKind() == OMPC_nowait)
5730 Nowait = Clause;
5731 else if (Clause->getClauseKind() == OMPC_copyprivate)
5732 Copyprivate = Clause;
5733 if (Copyprivate && Nowait) {
5734 Diag(Copyprivate->getLocStart(),
5735 diag::err_omp_single_copyprivate_with_nowait);
5736 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5737 return StmtError();
5738 }
5739 }
5740
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005741 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5742}
5743
Alexander Musman80c22892014-07-17 08:54:58 +00005744StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5745 SourceLocation StartLoc,
5746 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005747 if (!AStmt)
5748 return StmtError();
5749
5750 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005751
5752 getCurFunction()->setHasBranchProtectedScope();
5753
5754 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5755}
5756
Alexey Bataev28c75412015-12-15 08:19:24 +00005757StmtResult Sema::ActOnOpenMPCriticalDirective(
5758 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5759 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005760 if (!AStmt)
5761 return StmtError();
5762
5763 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005764
Alexey Bataev28c75412015-12-15 08:19:24 +00005765 bool ErrorFound = false;
5766 llvm::APSInt Hint;
5767 SourceLocation HintLoc;
5768 bool DependentHint = false;
5769 for (auto *C : Clauses) {
5770 if (C->getClauseKind() == OMPC_hint) {
5771 if (!DirName.getName()) {
5772 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5773 ErrorFound = true;
5774 }
5775 Expr *E = cast<OMPHintClause>(C)->getHint();
5776 if (E->isTypeDependent() || E->isValueDependent() ||
5777 E->isInstantiationDependent())
5778 DependentHint = true;
5779 else {
5780 Hint = E->EvaluateKnownConstInt(Context);
5781 HintLoc = C->getLocStart();
5782 }
5783 }
5784 }
5785 if (ErrorFound)
5786 return StmtError();
5787 auto Pair = DSAStack->getCriticalWithHint(DirName);
5788 if (Pair.first && DirName.getName() && !DependentHint) {
5789 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5790 Diag(StartLoc, diag::err_omp_critical_with_hint);
5791 if (HintLoc.isValid()) {
5792 Diag(HintLoc, diag::note_omp_critical_hint_here)
5793 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5794 } else
5795 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5796 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5797 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5798 << 1
5799 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5800 /*Radix=*/10, /*Signed=*/false);
5801 } else
5802 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5803 }
5804 }
5805
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005806 getCurFunction()->setHasBranchProtectedScope();
5807
Alexey Bataev28c75412015-12-15 08:19:24 +00005808 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5809 Clauses, AStmt);
5810 if (!Pair.first && DirName.getName() && !DependentHint)
5811 DSAStack->addCriticalWithHint(Dir, Hint);
5812 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005813}
5814
Alexey Bataev4acb8592014-07-07 13:01:15 +00005815StmtResult Sema::ActOnOpenMPParallelForDirective(
5816 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5817 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005818 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005819 if (!AStmt)
5820 return StmtError();
5821
Alexey Bataev4acb8592014-07-07 13:01:15 +00005822 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5823 // 1.2.2 OpenMP Language Terminology
5824 // Structured block - An executable statement with a single entry at the
5825 // top and a single exit at the bottom.
5826 // The point of exit cannot be a branch out of the structured block.
5827 // longjmp() and throw() must not violate the entry/exit criteria.
5828 CS->getCapturedDecl()->setNothrow();
5829
Alexander Musmanc6388682014-12-15 07:07:06 +00005830 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005831 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5832 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005833 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005834 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5835 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5836 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005837 if (NestedLoopCount == 0)
5838 return StmtError();
5839
Alexander Musmana5f070a2014-10-01 06:03:56 +00005840 assert((CurContext->isDependentContext() || B.builtAll()) &&
5841 "omp parallel for loop exprs were not built");
5842
Alexey Bataev54acd402015-08-04 11:18:19 +00005843 if (!CurContext->isDependentContext()) {
5844 // Finalize the clauses that need pre-built expressions for CodeGen.
5845 for (auto C : Clauses) {
5846 if (auto LC = dyn_cast<OMPLinearClause>(C))
5847 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005848 B.NumIterations, *this, CurScope,
5849 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005850 return StmtError();
5851 }
5852 }
5853
Alexey Bataev4acb8592014-07-07 13:01:15 +00005854 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005855 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005856 NestedLoopCount, Clauses, AStmt, B,
5857 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005858}
5859
Alexander Musmane4e893b2014-09-23 09:33:00 +00005860StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5861 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5862 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005863 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005864 if (!AStmt)
5865 return StmtError();
5866
Alexander Musmane4e893b2014-09-23 09:33:00 +00005867 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5868 // 1.2.2 OpenMP Language Terminology
5869 // Structured block - An executable statement with a single entry at the
5870 // top and a single exit at the bottom.
5871 // The point of exit cannot be a branch out of the structured block.
5872 // longjmp() and throw() must not violate the entry/exit criteria.
5873 CS->getCapturedDecl()->setNothrow();
5874
Alexander Musmanc6388682014-12-15 07:07:06 +00005875 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005876 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5877 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005878 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005879 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5880 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5881 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005882 if (NestedLoopCount == 0)
5883 return StmtError();
5884
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005885 if (!CurContext->isDependentContext()) {
5886 // Finalize the clauses that need pre-built expressions for CodeGen.
5887 for (auto C : Clauses) {
5888 if (auto LC = dyn_cast<OMPLinearClause>(C))
5889 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005890 B.NumIterations, *this, CurScope,
5891 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005892 return StmtError();
5893 }
5894 }
5895
Kelvin Lic5609492016-07-15 04:39:07 +00005896 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005897 return StmtError();
5898
Alexander Musmane4e893b2014-09-23 09:33:00 +00005899 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005900 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005901 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005902}
5903
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005904StmtResult
5905Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5906 Stmt *AStmt, SourceLocation StartLoc,
5907 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005908 if (!AStmt)
5909 return StmtError();
5910
5911 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005912 auto BaseStmt = AStmt;
5913 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5914 BaseStmt = CS->getCapturedStmt();
5915 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5916 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005917 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005918 return StmtError();
5919 // All associated statements must be '#pragma omp section' except for
5920 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005921 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005922 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5923 if (SectionStmt)
5924 Diag(SectionStmt->getLocStart(),
5925 diag::err_omp_parallel_sections_substmt_not_section);
5926 return StmtError();
5927 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005928 cast<OMPSectionDirective>(SectionStmt)
5929 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005930 }
5931 } else {
5932 Diag(AStmt->getLocStart(),
5933 diag::err_omp_parallel_sections_not_compound_stmt);
5934 return StmtError();
5935 }
5936
5937 getCurFunction()->setHasBranchProtectedScope();
5938
Alexey Bataev25e5b442015-09-15 12:52:43 +00005939 return OMPParallelSectionsDirective::Create(
5940 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005941}
5942
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005943StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5944 Stmt *AStmt, SourceLocation StartLoc,
5945 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005946 if (!AStmt)
5947 return StmtError();
5948
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005949 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5950 // 1.2.2 OpenMP Language Terminology
5951 // Structured block - An executable statement with a single entry at the
5952 // top and a single exit at the bottom.
5953 // The point of exit cannot be a branch out of the structured block.
5954 // longjmp() and throw() must not violate the entry/exit criteria.
5955 CS->getCapturedDecl()->setNothrow();
5956
5957 getCurFunction()->setHasBranchProtectedScope();
5958
Alexey Bataev25e5b442015-09-15 12:52:43 +00005959 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5960 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005961}
5962
Alexey Bataev68446b72014-07-18 07:47:19 +00005963StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5964 SourceLocation EndLoc) {
5965 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5966}
5967
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005968StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5969 SourceLocation EndLoc) {
5970 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5971}
5972
Alexey Bataev2df347a2014-07-18 10:17:07 +00005973StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5974 SourceLocation EndLoc) {
5975 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5976}
5977
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005978StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5979 SourceLocation StartLoc,
5980 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005981 if (!AStmt)
5982 return StmtError();
5983
5984 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005985
5986 getCurFunction()->setHasBranchProtectedScope();
5987
5988 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5989}
5990
Alexey Bataev6125da92014-07-21 11:26:11 +00005991StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5992 SourceLocation StartLoc,
5993 SourceLocation EndLoc) {
5994 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5995 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5996}
5997
Alexey Bataev346265e2015-09-25 10:37:12 +00005998StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5999 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006000 SourceLocation StartLoc,
6001 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006002 OMPClause *DependFound = nullptr;
6003 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006004 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006005 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00006006 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006007 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006008 for (auto *C : Clauses) {
6009 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6010 DependFound = C;
6011 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6012 if (DependSourceClause) {
6013 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
6014 << getOpenMPDirectiveName(OMPD_ordered)
6015 << getOpenMPClauseName(OMPC_depend) << 2;
6016 ErrorFound = true;
6017 } else
6018 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006019 if (DependSinkClause) {
6020 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6021 << 0;
6022 ErrorFound = true;
6023 }
6024 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6025 if (DependSourceClause) {
6026 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6027 << 1;
6028 ErrorFound = true;
6029 }
6030 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006031 }
6032 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00006033 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006034 else if (C->getClauseKind() == OMPC_simd)
6035 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00006036 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006037 if (!ErrorFound && !SC &&
6038 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006039 // OpenMP [2.8.1,simd Construct, Restrictions]
6040 // An ordered construct with the simd clause is the only OpenMP construct
6041 // that can appear in the simd region.
6042 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006043 ErrorFound = true;
6044 } else if (DependFound && (TC || SC)) {
6045 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6046 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6047 ErrorFound = true;
6048 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6049 Diag(DependFound->getLocStart(),
6050 diag::err_omp_ordered_directive_without_param);
6051 ErrorFound = true;
6052 } else if (TC || Clauses.empty()) {
6053 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6054 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6055 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6056 << (TC != nullptr);
6057 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6058 ErrorFound = true;
6059 }
6060 }
6061 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006062 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006063
6064 if (AStmt) {
6065 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6066
6067 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006068 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006069
6070 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006071}
6072
Alexey Bataev1d160b12015-03-13 12:27:31 +00006073namespace {
6074/// \brief Helper class for checking expression in 'omp atomic [update]'
6075/// construct.
6076class OpenMPAtomicUpdateChecker {
6077 /// \brief Error results for atomic update expressions.
6078 enum ExprAnalysisErrorCode {
6079 /// \brief A statement is not an expression statement.
6080 NotAnExpression,
6081 /// \brief Expression is not builtin binary or unary operation.
6082 NotABinaryOrUnaryExpression,
6083 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6084 NotAnUnaryIncDecExpression,
6085 /// \brief An expression is not of scalar type.
6086 NotAScalarType,
6087 /// \brief A binary operation is not an assignment operation.
6088 NotAnAssignmentOp,
6089 /// \brief RHS part of the binary operation is not a binary expression.
6090 NotABinaryExpression,
6091 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6092 /// expression.
6093 NotABinaryOperator,
6094 /// \brief RHS binary operation does not have reference to the updated LHS
6095 /// part.
6096 NotAnUpdateExpression,
6097 /// \brief No errors is found.
6098 NoError
6099 };
6100 /// \brief Reference to Sema.
6101 Sema &SemaRef;
6102 /// \brief A location for note diagnostics (when error is found).
6103 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006104 /// \brief 'x' lvalue part of the source atomic expression.
6105 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006106 /// \brief 'expr' rvalue part of the source atomic expression.
6107 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006108 /// \brief Helper expression of the form
6109 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6110 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6111 Expr *UpdateExpr;
6112 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6113 /// important for non-associative operations.
6114 bool IsXLHSInRHSPart;
6115 BinaryOperatorKind Op;
6116 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006117 /// \brief true if the source expression is a postfix unary operation, false
6118 /// if it is a prefix unary operation.
6119 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006120
6121public:
6122 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006123 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006124 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006125 /// \brief Check specified statement that it is suitable for 'atomic update'
6126 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006127 /// expression. If DiagId and NoteId == 0, then only check is performed
6128 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006129 /// \param DiagId Diagnostic which should be emitted if error is found.
6130 /// \param NoteId Diagnostic note for the main error message.
6131 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006132 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006133 /// \brief Return the 'x' lvalue part of the source atomic expression.
6134 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006135 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6136 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006137 /// \brief Return the update expression used in calculation of the updated
6138 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6139 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6140 Expr *getUpdateExpr() const { return UpdateExpr; }
6141 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6142 /// false otherwise.
6143 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6144
Alexey Bataevb78ca832015-04-01 03:33:17 +00006145 /// \brief true if the source expression is a postfix unary operation, false
6146 /// if it is a prefix unary operation.
6147 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6148
Alexey Bataev1d160b12015-03-13 12:27:31 +00006149private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006150 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6151 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006152};
6153} // namespace
6154
6155bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6156 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6157 ExprAnalysisErrorCode ErrorFound = NoError;
6158 SourceLocation ErrorLoc, NoteLoc;
6159 SourceRange ErrorRange, NoteRange;
6160 // Allowed constructs are:
6161 // x = x binop expr;
6162 // x = expr binop x;
6163 if (AtomicBinOp->getOpcode() == BO_Assign) {
6164 X = AtomicBinOp->getLHS();
6165 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6166 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6167 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6168 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6169 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006170 Op = AtomicInnerBinOp->getOpcode();
6171 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006172 auto *LHS = AtomicInnerBinOp->getLHS();
6173 auto *RHS = AtomicInnerBinOp->getRHS();
6174 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6175 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6176 /*Canonical=*/true);
6177 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6178 /*Canonical=*/true);
6179 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6180 /*Canonical=*/true);
6181 if (XId == LHSId) {
6182 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006183 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006184 } else if (XId == RHSId) {
6185 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006186 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006187 } else {
6188 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6189 ErrorRange = AtomicInnerBinOp->getSourceRange();
6190 NoteLoc = X->getExprLoc();
6191 NoteRange = X->getSourceRange();
6192 ErrorFound = NotAnUpdateExpression;
6193 }
6194 } else {
6195 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6196 ErrorRange = AtomicInnerBinOp->getSourceRange();
6197 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6198 NoteRange = SourceRange(NoteLoc, NoteLoc);
6199 ErrorFound = NotABinaryOperator;
6200 }
6201 } else {
6202 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6203 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6204 ErrorFound = NotABinaryExpression;
6205 }
6206 } else {
6207 ErrorLoc = AtomicBinOp->getExprLoc();
6208 ErrorRange = AtomicBinOp->getSourceRange();
6209 NoteLoc = AtomicBinOp->getOperatorLoc();
6210 NoteRange = SourceRange(NoteLoc, NoteLoc);
6211 ErrorFound = NotAnAssignmentOp;
6212 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006213 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006214 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6215 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6216 return true;
6217 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006218 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006219 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006220}
6221
6222bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6223 unsigned NoteId) {
6224 ExprAnalysisErrorCode ErrorFound = NoError;
6225 SourceLocation ErrorLoc, NoteLoc;
6226 SourceRange ErrorRange, NoteRange;
6227 // Allowed constructs are:
6228 // x++;
6229 // x--;
6230 // ++x;
6231 // --x;
6232 // x binop= expr;
6233 // x = x binop expr;
6234 // x = expr binop x;
6235 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6236 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6237 if (AtomicBody->getType()->isScalarType() ||
6238 AtomicBody->isInstantiationDependent()) {
6239 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6240 AtomicBody->IgnoreParenImpCasts())) {
6241 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006242 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006243 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006244 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006245 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006246 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006247 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006248 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6249 AtomicBody->IgnoreParenImpCasts())) {
6250 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006251 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6252 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006253 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006254 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6255 // Check for Unary Operation
6256 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006257 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006258 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6259 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006260 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006261 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6262 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006263 } else {
6264 ErrorFound = NotAnUnaryIncDecExpression;
6265 ErrorLoc = AtomicUnaryOp->getExprLoc();
6266 ErrorRange = AtomicUnaryOp->getSourceRange();
6267 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6268 NoteRange = SourceRange(NoteLoc, NoteLoc);
6269 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006270 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006271 ErrorFound = NotABinaryOrUnaryExpression;
6272 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6273 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6274 }
6275 } else {
6276 ErrorFound = NotAScalarType;
6277 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6278 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6279 }
6280 } else {
6281 ErrorFound = NotAnExpression;
6282 NoteLoc = ErrorLoc = S->getLocStart();
6283 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6284 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006285 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006286 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6287 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6288 return true;
6289 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006290 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006291 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006292 // Build an update expression of form 'OpaqueValueExpr(x) binop
6293 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6294 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6295 auto *OVEX = new (SemaRef.getASTContext())
6296 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6297 auto *OVEExpr = new (SemaRef.getASTContext())
6298 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6299 auto Update =
6300 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6301 IsXLHSInRHSPart ? OVEExpr : OVEX);
6302 if (Update.isInvalid())
6303 return true;
6304 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6305 Sema::AA_Casting);
6306 if (Update.isInvalid())
6307 return true;
6308 UpdateExpr = Update.get();
6309 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006310 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006311}
6312
Alexey Bataev0162e452014-07-22 10:10:35 +00006313StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6314 Stmt *AStmt,
6315 SourceLocation StartLoc,
6316 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006317 if (!AStmt)
6318 return StmtError();
6319
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006320 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006321 // 1.2.2 OpenMP Language Terminology
6322 // Structured block - An executable statement with a single entry at the
6323 // top and a single exit at the bottom.
6324 // The point of exit cannot be a branch out of the structured block.
6325 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006326 OpenMPClauseKind AtomicKind = OMPC_unknown;
6327 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006328 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006329 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006330 C->getClauseKind() == OMPC_update ||
6331 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006332 if (AtomicKind != OMPC_unknown) {
6333 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6334 << SourceRange(C->getLocStart(), C->getLocEnd());
6335 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6336 << getOpenMPClauseName(AtomicKind);
6337 } else {
6338 AtomicKind = C->getClauseKind();
6339 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006340 }
6341 }
6342 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006343
Alexey Bataev459dec02014-07-24 06:46:57 +00006344 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006345 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6346 Body = EWC->getSubExpr();
6347
Alexey Bataev62cec442014-11-18 10:14:22 +00006348 Expr *X = nullptr;
6349 Expr *V = nullptr;
6350 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006351 Expr *UE = nullptr;
6352 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006353 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006354 // OpenMP [2.12.6, atomic Construct]
6355 // In the next expressions:
6356 // * x and v (as applicable) are both l-value expressions with scalar type.
6357 // * During the execution of an atomic region, multiple syntactic
6358 // occurrences of x must designate the same storage location.
6359 // * Neither of v and expr (as applicable) may access the storage location
6360 // designated by x.
6361 // * Neither of x and expr (as applicable) may access the storage location
6362 // designated by v.
6363 // * expr is an expression with scalar type.
6364 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6365 // * binop, binop=, ++, and -- are not overloaded operators.
6366 // * The expression x binop expr must be numerically equivalent to x binop
6367 // (expr). This requirement is satisfied if the operators in expr have
6368 // precedence greater than binop, or by using parentheses around expr or
6369 // subexpressions of expr.
6370 // * The expression expr binop x must be numerically equivalent to (expr)
6371 // binop x. This requirement is satisfied if the operators in expr have
6372 // precedence equal to or greater than binop, or by using parentheses around
6373 // expr or subexpressions of expr.
6374 // * For forms that allow multiple occurrences of x, the number of times
6375 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006376 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006377 enum {
6378 NotAnExpression,
6379 NotAnAssignmentOp,
6380 NotAScalarType,
6381 NotAnLValue,
6382 NoError
6383 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006384 SourceLocation ErrorLoc, NoteLoc;
6385 SourceRange ErrorRange, NoteRange;
6386 // If clause is read:
6387 // v = x;
6388 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6389 auto AtomicBinOp =
6390 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6391 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6392 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6393 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6394 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6395 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6396 if (!X->isLValue() || !V->isLValue()) {
6397 auto NotLValueExpr = X->isLValue() ? V : X;
6398 ErrorFound = NotAnLValue;
6399 ErrorLoc = AtomicBinOp->getExprLoc();
6400 ErrorRange = AtomicBinOp->getSourceRange();
6401 NoteLoc = NotLValueExpr->getExprLoc();
6402 NoteRange = NotLValueExpr->getSourceRange();
6403 }
6404 } else if (!X->isInstantiationDependent() ||
6405 !V->isInstantiationDependent()) {
6406 auto NotScalarExpr =
6407 (X->isInstantiationDependent() || X->getType()->isScalarType())
6408 ? V
6409 : X;
6410 ErrorFound = NotAScalarType;
6411 ErrorLoc = AtomicBinOp->getExprLoc();
6412 ErrorRange = AtomicBinOp->getSourceRange();
6413 NoteLoc = NotScalarExpr->getExprLoc();
6414 NoteRange = NotScalarExpr->getSourceRange();
6415 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006416 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006417 ErrorFound = NotAnAssignmentOp;
6418 ErrorLoc = AtomicBody->getExprLoc();
6419 ErrorRange = AtomicBody->getSourceRange();
6420 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6421 : AtomicBody->getExprLoc();
6422 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6423 : AtomicBody->getSourceRange();
6424 }
6425 } else {
6426 ErrorFound = NotAnExpression;
6427 NoteLoc = ErrorLoc = Body->getLocStart();
6428 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006429 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006430 if (ErrorFound != NoError) {
6431 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6432 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006433 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6434 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006435 return StmtError();
6436 } else if (CurContext->isDependentContext())
6437 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006438 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006439 enum {
6440 NotAnExpression,
6441 NotAnAssignmentOp,
6442 NotAScalarType,
6443 NotAnLValue,
6444 NoError
6445 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006446 SourceLocation ErrorLoc, NoteLoc;
6447 SourceRange ErrorRange, NoteRange;
6448 // If clause is write:
6449 // x = expr;
6450 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6451 auto AtomicBinOp =
6452 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6453 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006454 X = AtomicBinOp->getLHS();
6455 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006456 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6457 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6458 if (!X->isLValue()) {
6459 ErrorFound = NotAnLValue;
6460 ErrorLoc = AtomicBinOp->getExprLoc();
6461 ErrorRange = AtomicBinOp->getSourceRange();
6462 NoteLoc = X->getExprLoc();
6463 NoteRange = X->getSourceRange();
6464 }
6465 } else if (!X->isInstantiationDependent() ||
6466 !E->isInstantiationDependent()) {
6467 auto NotScalarExpr =
6468 (X->isInstantiationDependent() || X->getType()->isScalarType())
6469 ? E
6470 : X;
6471 ErrorFound = NotAScalarType;
6472 ErrorLoc = AtomicBinOp->getExprLoc();
6473 ErrorRange = AtomicBinOp->getSourceRange();
6474 NoteLoc = NotScalarExpr->getExprLoc();
6475 NoteRange = NotScalarExpr->getSourceRange();
6476 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006477 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006478 ErrorFound = NotAnAssignmentOp;
6479 ErrorLoc = AtomicBody->getExprLoc();
6480 ErrorRange = AtomicBody->getSourceRange();
6481 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6482 : AtomicBody->getExprLoc();
6483 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6484 : AtomicBody->getSourceRange();
6485 }
6486 } else {
6487 ErrorFound = NotAnExpression;
6488 NoteLoc = ErrorLoc = Body->getLocStart();
6489 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006490 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006491 if (ErrorFound != NoError) {
6492 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6493 << ErrorRange;
6494 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6495 << NoteRange;
6496 return StmtError();
6497 } else if (CurContext->isDependentContext())
6498 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006499 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006500 // If clause is update:
6501 // x++;
6502 // x--;
6503 // ++x;
6504 // --x;
6505 // x binop= expr;
6506 // x = x binop expr;
6507 // x = expr binop x;
6508 OpenMPAtomicUpdateChecker Checker(*this);
6509 if (Checker.checkStatement(
6510 Body, (AtomicKind == OMPC_update)
6511 ? diag::err_omp_atomic_update_not_expression_statement
6512 : diag::err_omp_atomic_not_expression_statement,
6513 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006514 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006515 if (!CurContext->isDependentContext()) {
6516 E = Checker.getExpr();
6517 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006518 UE = Checker.getUpdateExpr();
6519 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006520 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006521 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006522 enum {
6523 NotAnAssignmentOp,
6524 NotACompoundStatement,
6525 NotTwoSubstatements,
6526 NotASpecificExpression,
6527 NoError
6528 } ErrorFound = NoError;
6529 SourceLocation ErrorLoc, NoteLoc;
6530 SourceRange ErrorRange, NoteRange;
6531 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6532 // If clause is a capture:
6533 // v = x++;
6534 // v = x--;
6535 // v = ++x;
6536 // v = --x;
6537 // v = x binop= expr;
6538 // v = x = x binop expr;
6539 // v = x = expr binop x;
6540 auto *AtomicBinOp =
6541 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6542 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6543 V = AtomicBinOp->getLHS();
6544 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6545 OpenMPAtomicUpdateChecker Checker(*this);
6546 if (Checker.checkStatement(
6547 Body, diag::err_omp_atomic_capture_not_expression_statement,
6548 diag::note_omp_atomic_update))
6549 return StmtError();
6550 E = Checker.getExpr();
6551 X = Checker.getX();
6552 UE = Checker.getUpdateExpr();
6553 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6554 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006555 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006556 ErrorLoc = AtomicBody->getExprLoc();
6557 ErrorRange = AtomicBody->getSourceRange();
6558 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6559 : AtomicBody->getExprLoc();
6560 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6561 : AtomicBody->getSourceRange();
6562 ErrorFound = NotAnAssignmentOp;
6563 }
6564 if (ErrorFound != NoError) {
6565 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6566 << ErrorRange;
6567 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6568 return StmtError();
6569 } else if (CurContext->isDependentContext()) {
6570 UE = V = E = X = nullptr;
6571 }
6572 } else {
6573 // If clause is a capture:
6574 // { v = x; x = expr; }
6575 // { v = x; x++; }
6576 // { v = x; x--; }
6577 // { v = x; ++x; }
6578 // { v = x; --x; }
6579 // { v = x; x binop= expr; }
6580 // { v = x; x = x binop expr; }
6581 // { v = x; x = expr binop x; }
6582 // { x++; v = x; }
6583 // { x--; v = x; }
6584 // { ++x; v = x; }
6585 // { --x; v = x; }
6586 // { x binop= expr; v = x; }
6587 // { x = x binop expr; v = x; }
6588 // { x = expr binop x; v = x; }
6589 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6590 // Check that this is { expr1; expr2; }
6591 if (CS->size() == 2) {
6592 auto *First = CS->body_front();
6593 auto *Second = CS->body_back();
6594 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6595 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6596 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6597 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6598 // Need to find what subexpression is 'v' and what is 'x'.
6599 OpenMPAtomicUpdateChecker Checker(*this);
6600 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6601 BinaryOperator *BinOp = nullptr;
6602 if (IsUpdateExprFound) {
6603 BinOp = dyn_cast<BinaryOperator>(First);
6604 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6605 }
6606 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6607 // { v = x; x++; }
6608 // { v = x; x--; }
6609 // { v = x; ++x; }
6610 // { v = x; --x; }
6611 // { v = x; x binop= expr; }
6612 // { v = x; x = x binop expr; }
6613 // { v = x; x = expr binop x; }
6614 // Check that the first expression has form v = x.
6615 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6616 llvm::FoldingSetNodeID XId, PossibleXId;
6617 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6618 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6619 IsUpdateExprFound = XId == PossibleXId;
6620 if (IsUpdateExprFound) {
6621 V = BinOp->getLHS();
6622 X = Checker.getX();
6623 E = Checker.getExpr();
6624 UE = Checker.getUpdateExpr();
6625 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006626 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006627 }
6628 }
6629 if (!IsUpdateExprFound) {
6630 IsUpdateExprFound = !Checker.checkStatement(First);
6631 BinOp = nullptr;
6632 if (IsUpdateExprFound) {
6633 BinOp = dyn_cast<BinaryOperator>(Second);
6634 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6635 }
6636 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6637 // { x++; v = x; }
6638 // { x--; v = x; }
6639 // { ++x; v = x; }
6640 // { --x; v = x; }
6641 // { x binop= expr; v = x; }
6642 // { x = x binop expr; v = x; }
6643 // { x = expr binop x; v = x; }
6644 // Check that the second expression has form v = x.
6645 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6646 llvm::FoldingSetNodeID XId, PossibleXId;
6647 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6648 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6649 IsUpdateExprFound = XId == PossibleXId;
6650 if (IsUpdateExprFound) {
6651 V = BinOp->getLHS();
6652 X = Checker.getX();
6653 E = Checker.getExpr();
6654 UE = Checker.getUpdateExpr();
6655 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006656 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006657 }
6658 }
6659 }
6660 if (!IsUpdateExprFound) {
6661 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006662 auto *FirstExpr = dyn_cast<Expr>(First);
6663 auto *SecondExpr = dyn_cast<Expr>(Second);
6664 if (!FirstExpr || !SecondExpr ||
6665 !(FirstExpr->isInstantiationDependent() ||
6666 SecondExpr->isInstantiationDependent())) {
6667 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6668 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006669 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006670 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6671 : First->getLocStart();
6672 NoteRange = ErrorRange = FirstBinOp
6673 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006674 : SourceRange(ErrorLoc, ErrorLoc);
6675 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006676 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6677 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6678 ErrorFound = NotAnAssignmentOp;
6679 NoteLoc = ErrorLoc = SecondBinOp
6680 ? SecondBinOp->getOperatorLoc()
6681 : Second->getLocStart();
6682 NoteRange = ErrorRange =
6683 SecondBinOp ? SecondBinOp->getSourceRange()
6684 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006685 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006686 auto *PossibleXRHSInFirst =
6687 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6688 auto *PossibleXLHSInSecond =
6689 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6690 llvm::FoldingSetNodeID X1Id, X2Id;
6691 PossibleXRHSInFirst->Profile(X1Id, Context,
6692 /*Canonical=*/true);
6693 PossibleXLHSInSecond->Profile(X2Id, Context,
6694 /*Canonical=*/true);
6695 IsUpdateExprFound = X1Id == X2Id;
6696 if (IsUpdateExprFound) {
6697 V = FirstBinOp->getLHS();
6698 X = SecondBinOp->getLHS();
6699 E = SecondBinOp->getRHS();
6700 UE = nullptr;
6701 IsXLHSInRHSPart = false;
6702 IsPostfixUpdate = true;
6703 } else {
6704 ErrorFound = NotASpecificExpression;
6705 ErrorLoc = FirstBinOp->getExprLoc();
6706 ErrorRange = FirstBinOp->getSourceRange();
6707 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6708 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6709 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006710 }
6711 }
6712 }
6713 }
6714 } else {
6715 NoteLoc = ErrorLoc = Body->getLocStart();
6716 NoteRange = ErrorRange =
6717 SourceRange(Body->getLocStart(), Body->getLocStart());
6718 ErrorFound = NotTwoSubstatements;
6719 }
6720 } else {
6721 NoteLoc = ErrorLoc = Body->getLocStart();
6722 NoteRange = ErrorRange =
6723 SourceRange(Body->getLocStart(), Body->getLocStart());
6724 ErrorFound = NotACompoundStatement;
6725 }
6726 if (ErrorFound != NoError) {
6727 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6728 << ErrorRange;
6729 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6730 return StmtError();
6731 } else if (CurContext->isDependentContext()) {
6732 UE = V = E = X = nullptr;
6733 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006734 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006735 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006736
6737 getCurFunction()->setHasBranchProtectedScope();
6738
Alexey Bataev62cec442014-11-18 10:14:22 +00006739 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006740 X, V, E, UE, IsXLHSInRHSPart,
6741 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006742}
6743
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006744StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6745 Stmt *AStmt,
6746 SourceLocation StartLoc,
6747 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006748 if (!AStmt)
6749 return StmtError();
6750
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006751 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6752 // 1.2.2 OpenMP Language Terminology
6753 // Structured block - An executable statement with a single entry at the
6754 // top and a single exit at the bottom.
6755 // The point of exit cannot be a branch out of the structured block.
6756 // longjmp() and throw() must not violate the entry/exit criteria.
6757 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006758
Alexey Bataev13314bf2014-10-09 04:18:56 +00006759 // OpenMP [2.16, Nesting of Regions]
6760 // If specified, a teams construct must be contained within a target
6761 // construct. That target construct must contain no statements or directives
6762 // outside of the teams construct.
6763 if (DSAStack->hasInnerTeamsRegion()) {
6764 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6765 bool OMPTeamsFound = true;
6766 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6767 auto I = CS->body_begin();
6768 while (I != CS->body_end()) {
6769 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6770 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6771 OMPTeamsFound = false;
6772 break;
6773 }
6774 ++I;
6775 }
6776 assert(I != CS->body_end() && "Not found statement");
6777 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006778 } else {
6779 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6780 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006781 }
6782 if (!OMPTeamsFound) {
6783 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6784 Diag(DSAStack->getInnerTeamsRegionLoc(),
6785 diag::note_omp_nested_teams_construct_here);
6786 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6787 << isa<OMPExecutableDirective>(S);
6788 return StmtError();
6789 }
6790 }
6791
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006792 getCurFunction()->setHasBranchProtectedScope();
6793
6794 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6795}
6796
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006797StmtResult
6798Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6799 Stmt *AStmt, SourceLocation StartLoc,
6800 SourceLocation EndLoc) {
6801 if (!AStmt)
6802 return StmtError();
6803
6804 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6805 // 1.2.2 OpenMP Language Terminology
6806 // Structured block - An executable statement with a single entry at the
6807 // top and a single exit at the bottom.
6808 // The point of exit cannot be a branch out of the structured block.
6809 // longjmp() and throw() must not violate the entry/exit criteria.
6810 CS->getCapturedDecl()->setNothrow();
6811
6812 getCurFunction()->setHasBranchProtectedScope();
6813
6814 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6815 AStmt);
6816}
6817
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006818StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6819 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6820 SourceLocation EndLoc,
6821 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6822 if (!AStmt)
6823 return StmtError();
6824
6825 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6826 // 1.2.2 OpenMP Language Terminology
6827 // Structured block - An executable statement with a single entry at the
6828 // top and a single exit at the bottom.
6829 // The point of exit cannot be a branch out of the structured block.
6830 // longjmp() and throw() must not violate the entry/exit criteria.
6831 CS->getCapturedDecl()->setNothrow();
6832
6833 OMPLoopDirective::HelperExprs B;
6834 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6835 // define the nested loops number.
6836 unsigned NestedLoopCount =
6837 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6838 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6839 VarsWithImplicitDSA, B);
6840 if (NestedLoopCount == 0)
6841 return StmtError();
6842
6843 assert((CurContext->isDependentContext() || B.builtAll()) &&
6844 "omp target parallel for loop exprs were not built");
6845
6846 if (!CurContext->isDependentContext()) {
6847 // Finalize the clauses that need pre-built expressions for CodeGen.
6848 for (auto C : Clauses) {
6849 if (auto LC = dyn_cast<OMPLinearClause>(C))
6850 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006851 B.NumIterations, *this, CurScope,
6852 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006853 return StmtError();
6854 }
6855 }
6856
6857 getCurFunction()->setHasBranchProtectedScope();
6858 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6859 NestedLoopCount, Clauses, AStmt,
6860 B, DSAStack->isCancelRegion());
6861}
6862
Samuel Antaodf67fc42016-01-19 19:15:56 +00006863/// \brief Check for existence of a map clause in the list of clauses.
6864static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6865 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6866 I != E; ++I) {
6867 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6868 return true;
6869 }
6870 }
6871
6872 return false;
6873}
6874
Michael Wong65f367f2015-07-21 13:44:28 +00006875StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6876 Stmt *AStmt,
6877 SourceLocation StartLoc,
6878 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006879 if (!AStmt)
6880 return StmtError();
6881
6882 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6883
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006884 // OpenMP [2.10.1, Restrictions, p. 97]
6885 // At least one map clause must appear on the directive.
6886 if (!HasMapClause(Clauses)) {
6887 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6888 getOpenMPDirectiveName(OMPD_target_data);
6889 return StmtError();
6890 }
6891
Michael Wong65f367f2015-07-21 13:44:28 +00006892 getCurFunction()->setHasBranchProtectedScope();
6893
6894 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6895 AStmt);
6896}
6897
Samuel Antaodf67fc42016-01-19 19:15:56 +00006898StmtResult
6899Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6900 SourceLocation StartLoc,
6901 SourceLocation EndLoc) {
6902 // OpenMP [2.10.2, Restrictions, p. 99]
6903 // At least one map clause must appear on the directive.
6904 if (!HasMapClause(Clauses)) {
6905 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6906 << getOpenMPDirectiveName(OMPD_target_enter_data);
6907 return StmtError();
6908 }
6909
6910 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6911 Clauses);
6912}
6913
Samuel Antao72590762016-01-19 20:04:50 +00006914StmtResult
6915Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6916 SourceLocation StartLoc,
6917 SourceLocation EndLoc) {
6918 // OpenMP [2.10.3, Restrictions, p. 102]
6919 // At least one map clause must appear on the directive.
6920 if (!HasMapClause(Clauses)) {
6921 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6922 << getOpenMPDirectiveName(OMPD_target_exit_data);
6923 return StmtError();
6924 }
6925
6926 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6927}
6928
Samuel Antao686c70c2016-05-26 17:30:50 +00006929StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6930 SourceLocation StartLoc,
6931 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006932 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00006933 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00006934 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00006935 seenMotionClause = true;
6936 }
Samuel Antao686c70c2016-05-26 17:30:50 +00006937 if (!seenMotionClause) {
6938 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6939 return StmtError();
6940 }
6941 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6942}
6943
Alexey Bataev13314bf2014-10-09 04:18:56 +00006944StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6945 Stmt *AStmt, SourceLocation StartLoc,
6946 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006947 if (!AStmt)
6948 return StmtError();
6949
Alexey Bataev13314bf2014-10-09 04:18:56 +00006950 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6951 // 1.2.2 OpenMP Language Terminology
6952 // Structured block - An executable statement with a single entry at the
6953 // top and a single exit at the bottom.
6954 // The point of exit cannot be a branch out of the structured block.
6955 // longjmp() and throw() must not violate the entry/exit criteria.
6956 CS->getCapturedDecl()->setNothrow();
6957
6958 getCurFunction()->setHasBranchProtectedScope();
6959
6960 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6961}
6962
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006963StmtResult
6964Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6965 SourceLocation EndLoc,
6966 OpenMPDirectiveKind CancelRegion) {
6967 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6968 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6969 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6970 << getOpenMPDirectiveName(CancelRegion);
6971 return StmtError();
6972 }
6973 if (DSAStack->isParentNowaitRegion()) {
6974 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6975 return StmtError();
6976 }
6977 if (DSAStack->isParentOrderedRegion()) {
6978 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6979 return StmtError();
6980 }
6981 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6982 CancelRegion);
6983}
6984
Alexey Bataev87933c72015-09-18 08:07:34 +00006985StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6986 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006987 SourceLocation EndLoc,
6988 OpenMPDirectiveKind CancelRegion) {
6989 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6990 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6991 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6992 << getOpenMPDirectiveName(CancelRegion);
6993 return StmtError();
6994 }
6995 if (DSAStack->isParentNowaitRegion()) {
6996 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6997 return StmtError();
6998 }
6999 if (DSAStack->isParentOrderedRegion()) {
7000 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7001 return StmtError();
7002 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007003 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007004 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7005 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007006}
7007
Alexey Bataev382967a2015-12-08 12:06:20 +00007008static bool checkGrainsizeNumTasksClauses(Sema &S,
7009 ArrayRef<OMPClause *> Clauses) {
7010 OMPClause *PrevClause = nullptr;
7011 bool ErrorFound = false;
7012 for (auto *C : Clauses) {
7013 if (C->getClauseKind() == OMPC_grainsize ||
7014 C->getClauseKind() == OMPC_num_tasks) {
7015 if (!PrevClause)
7016 PrevClause = C;
7017 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
7018 S.Diag(C->getLocStart(),
7019 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7020 << getOpenMPClauseName(C->getClauseKind())
7021 << getOpenMPClauseName(PrevClause->getClauseKind());
7022 S.Diag(PrevClause->getLocStart(),
7023 diag::note_omp_previous_grainsize_num_tasks)
7024 << getOpenMPClauseName(PrevClause->getClauseKind());
7025 ErrorFound = true;
7026 }
7027 }
7028 }
7029 return ErrorFound;
7030}
7031
Alexey Bataev49f6e782015-12-01 04:18:41 +00007032StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7033 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7034 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007035 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007036 if (!AStmt)
7037 return StmtError();
7038
7039 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7040 OMPLoopDirective::HelperExprs B;
7041 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7042 // define the nested loops number.
7043 unsigned NestedLoopCount =
7044 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007045 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007046 VarsWithImplicitDSA, B);
7047 if (NestedLoopCount == 0)
7048 return StmtError();
7049
7050 assert((CurContext->isDependentContext() || B.builtAll()) &&
7051 "omp for loop exprs were not built");
7052
Alexey Bataev382967a2015-12-08 12:06:20 +00007053 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7054 // The grainsize clause and num_tasks clause are mutually exclusive and may
7055 // not appear on the same taskloop directive.
7056 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7057 return StmtError();
7058
Alexey Bataev49f6e782015-12-01 04:18:41 +00007059 getCurFunction()->setHasBranchProtectedScope();
7060 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7061 NestedLoopCount, Clauses, AStmt, B);
7062}
7063
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007064StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7065 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7066 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007067 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007068 if (!AStmt)
7069 return StmtError();
7070
7071 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7072 OMPLoopDirective::HelperExprs B;
7073 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7074 // define the nested loops number.
7075 unsigned NestedLoopCount =
7076 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7077 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7078 VarsWithImplicitDSA, B);
7079 if (NestedLoopCount == 0)
7080 return StmtError();
7081
7082 assert((CurContext->isDependentContext() || B.builtAll()) &&
7083 "omp for loop exprs were not built");
7084
Alexey Bataev5a3af132016-03-29 08:58:54 +00007085 if (!CurContext->isDependentContext()) {
7086 // Finalize the clauses that need pre-built expressions for CodeGen.
7087 for (auto C : Clauses) {
7088 if (auto LC = dyn_cast<OMPLinearClause>(C))
7089 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007090 B.NumIterations, *this, CurScope,
7091 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007092 return StmtError();
7093 }
7094 }
7095
Alexey Bataev382967a2015-12-08 12:06:20 +00007096 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7097 // The grainsize clause and num_tasks clause are mutually exclusive and may
7098 // not appear on the same taskloop directive.
7099 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7100 return StmtError();
7101
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007102 getCurFunction()->setHasBranchProtectedScope();
7103 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7104 NestedLoopCount, Clauses, AStmt, B);
7105}
7106
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007107StmtResult Sema::ActOnOpenMPDistributeDirective(
7108 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7109 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007110 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007111 if (!AStmt)
7112 return StmtError();
7113
7114 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7115 OMPLoopDirective::HelperExprs B;
7116 // In presence of clause 'collapse' with number of loops, it will
7117 // define the nested loops number.
7118 unsigned NestedLoopCount =
7119 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7120 nullptr /*ordered not a clause on distribute*/, AStmt,
7121 *this, *DSAStack, VarsWithImplicitDSA, B);
7122 if (NestedLoopCount == 0)
7123 return StmtError();
7124
7125 assert((CurContext->isDependentContext() || B.builtAll()) &&
7126 "omp for loop exprs were not built");
7127
7128 getCurFunction()->setHasBranchProtectedScope();
7129 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7130 NestedLoopCount, Clauses, AStmt, B);
7131}
7132
Carlo Bertolli9925f152016-06-27 14:55:37 +00007133StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7134 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7135 SourceLocation EndLoc,
7136 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7137 if (!AStmt)
7138 return StmtError();
7139
7140 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7141 // 1.2.2 OpenMP Language Terminology
7142 // Structured block - An executable statement with a single entry at the
7143 // top and a single exit at the bottom.
7144 // The point of exit cannot be a branch out of the structured block.
7145 // longjmp() and throw() must not violate the entry/exit criteria.
7146 CS->getCapturedDecl()->setNothrow();
7147
7148 OMPLoopDirective::HelperExprs B;
7149 // In presence of clause 'collapse' with number of loops, it will
7150 // define the nested loops number.
7151 unsigned NestedLoopCount = CheckOpenMPLoop(
7152 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7153 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7154 VarsWithImplicitDSA, B);
7155 if (NestedLoopCount == 0)
7156 return StmtError();
7157
7158 assert((CurContext->isDependentContext() || B.builtAll()) &&
7159 "omp for loop exprs were not built");
7160
7161 getCurFunction()->setHasBranchProtectedScope();
7162 return OMPDistributeParallelForDirective::Create(
7163 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7164}
7165
Kelvin Li4a39add2016-07-05 05:00:15 +00007166StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7167 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7168 SourceLocation EndLoc,
7169 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7170 if (!AStmt)
7171 return StmtError();
7172
7173 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7174 // 1.2.2 OpenMP Language Terminology
7175 // Structured block - An executable statement with a single entry at the
7176 // top and a single exit at the bottom.
7177 // The point of exit cannot be a branch out of the structured block.
7178 // longjmp() and throw() must not violate the entry/exit criteria.
7179 CS->getCapturedDecl()->setNothrow();
7180
7181 OMPLoopDirective::HelperExprs B;
7182 // In presence of clause 'collapse' with number of loops, it will
7183 // define the nested loops number.
7184 unsigned NestedLoopCount = CheckOpenMPLoop(
7185 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7186 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7187 VarsWithImplicitDSA, B);
7188 if (NestedLoopCount == 0)
7189 return StmtError();
7190
7191 assert((CurContext->isDependentContext() || B.builtAll()) &&
7192 "omp for loop exprs were not built");
7193
Kelvin Lic5609492016-07-15 04:39:07 +00007194 if (checkSimdlenSafelenSpecified(*this, Clauses))
7195 return StmtError();
7196
Kelvin Li4a39add2016-07-05 05:00:15 +00007197 getCurFunction()->setHasBranchProtectedScope();
7198 return OMPDistributeParallelForSimdDirective::Create(
7199 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7200}
7201
Kelvin Li787f3fc2016-07-06 04:45:38 +00007202StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7203 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7204 SourceLocation EndLoc,
7205 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7206 if (!AStmt)
7207 return StmtError();
7208
7209 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7210 // 1.2.2 OpenMP Language Terminology
7211 // Structured block - An executable statement with a single entry at the
7212 // top and a single exit at the bottom.
7213 // The point of exit cannot be a branch out of the structured block.
7214 // longjmp() and throw() must not violate the entry/exit criteria.
7215 CS->getCapturedDecl()->setNothrow();
7216
7217 OMPLoopDirective::HelperExprs B;
7218 // In presence of clause 'collapse' with number of loops, it will
7219 // define the nested loops number.
7220 unsigned NestedLoopCount =
7221 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7222 nullptr /*ordered not a clause on distribute*/, AStmt,
7223 *this, *DSAStack, VarsWithImplicitDSA, B);
7224 if (NestedLoopCount == 0)
7225 return StmtError();
7226
7227 assert((CurContext->isDependentContext() || B.builtAll()) &&
7228 "omp for loop exprs were not built");
7229
Kelvin Lic5609492016-07-15 04:39:07 +00007230 if (checkSimdlenSafelenSpecified(*this, Clauses))
7231 return StmtError();
7232
Kelvin Li787f3fc2016-07-06 04:45:38 +00007233 getCurFunction()->setHasBranchProtectedScope();
7234 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7235 NestedLoopCount, Clauses, AStmt, B);
7236}
7237
Kelvin Lia579b912016-07-14 02:54:56 +00007238StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7239 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7240 SourceLocation EndLoc,
7241 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7242 if (!AStmt)
7243 return StmtError();
7244
7245 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7246 // 1.2.2 OpenMP Language Terminology
7247 // Structured block - An executable statement with a single entry at the
7248 // top and a single exit at the bottom.
7249 // The point of exit cannot be a branch out of the structured block.
7250 // longjmp() and throw() must not violate the entry/exit criteria.
7251 CS->getCapturedDecl()->setNothrow();
7252
7253 OMPLoopDirective::HelperExprs B;
7254 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7255 // define the nested loops number.
7256 unsigned NestedLoopCount = CheckOpenMPLoop(
7257 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7258 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7259 VarsWithImplicitDSA, B);
7260 if (NestedLoopCount == 0)
7261 return StmtError();
7262
7263 assert((CurContext->isDependentContext() || B.builtAll()) &&
7264 "omp target parallel for simd loop exprs were not built");
7265
7266 if (!CurContext->isDependentContext()) {
7267 // Finalize the clauses that need pre-built expressions for CodeGen.
7268 for (auto C : Clauses) {
7269 if (auto LC = dyn_cast<OMPLinearClause>(C))
7270 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7271 B.NumIterations, *this, CurScope,
7272 DSAStack))
7273 return StmtError();
7274 }
7275 }
Kelvin Lic5609492016-07-15 04:39:07 +00007276 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007277 return StmtError();
7278
7279 getCurFunction()->setHasBranchProtectedScope();
7280 return OMPTargetParallelForSimdDirective::Create(
7281 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7282}
7283
Kelvin Li986330c2016-07-20 22:57:10 +00007284StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7285 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7286 SourceLocation EndLoc,
7287 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7288 if (!AStmt)
7289 return StmtError();
7290
7291 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7292 // 1.2.2 OpenMP Language Terminology
7293 // Structured block - An executable statement with a single entry at the
7294 // top and a single exit at the bottom.
7295 // The point of exit cannot be a branch out of the structured block.
7296 // longjmp() and throw() must not violate the entry/exit criteria.
7297 CS->getCapturedDecl()->setNothrow();
7298
7299 OMPLoopDirective::HelperExprs B;
7300 // In presence of clause 'collapse' with number of loops, it will define the
7301 // nested loops number.
7302 unsigned NestedLoopCount =
7303 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7304 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7305 VarsWithImplicitDSA, B);
7306 if (NestedLoopCount == 0)
7307 return StmtError();
7308
7309 assert((CurContext->isDependentContext() || B.builtAll()) &&
7310 "omp target simd loop exprs were not built");
7311
7312 if (!CurContext->isDependentContext()) {
7313 // Finalize the clauses that need pre-built expressions for CodeGen.
7314 for (auto C : Clauses) {
7315 if (auto LC = dyn_cast<OMPLinearClause>(C))
7316 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7317 B.NumIterations, *this, CurScope,
7318 DSAStack))
7319 return StmtError();
7320 }
7321 }
7322
7323 if (checkSimdlenSafelenSpecified(*this, Clauses))
7324 return StmtError();
7325
7326 getCurFunction()->setHasBranchProtectedScope();
7327 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7328 NestedLoopCount, Clauses, AStmt, B);
7329}
7330
Alexey Bataeved09d242014-05-28 05:53:51 +00007331OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007332 SourceLocation StartLoc,
7333 SourceLocation LParenLoc,
7334 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007335 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007336 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007337 case OMPC_final:
7338 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7339 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007340 case OMPC_num_threads:
7341 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7342 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007343 case OMPC_safelen:
7344 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7345 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007346 case OMPC_simdlen:
7347 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7348 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007349 case OMPC_collapse:
7350 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7351 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007352 case OMPC_ordered:
7353 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7354 break;
Michael Wonge710d542015-08-07 16:16:36 +00007355 case OMPC_device:
7356 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7357 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007358 case OMPC_num_teams:
7359 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7360 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007361 case OMPC_thread_limit:
7362 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7363 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007364 case OMPC_priority:
7365 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7366 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007367 case OMPC_grainsize:
7368 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7369 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007370 case OMPC_num_tasks:
7371 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7372 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007373 case OMPC_hint:
7374 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7375 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007376 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007377 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007378 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007379 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007380 case OMPC_private:
7381 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007382 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007383 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007384 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007385 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007386 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007387 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007388 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007389 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007390 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007391 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007392 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007393 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007394 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007395 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007396 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007397 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007398 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007399 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007400 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007401 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007402 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007403 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007404 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007405 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007406 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007407 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007408 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007409 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007410 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007411 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007412 llvm_unreachable("Clause is not allowed.");
7413 }
7414 return Res;
7415}
7416
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007417OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7418 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007419 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007420 SourceLocation NameModifierLoc,
7421 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007422 SourceLocation EndLoc) {
7423 Expr *ValExpr = Condition;
7424 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7425 !Condition->isInstantiationDependent() &&
7426 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007427 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007428 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007429 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007430
Richard Smith03a4aa32016-06-23 19:02:52 +00007431 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007432 }
7433
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007434 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7435 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007436}
7437
Alexey Bataev3778b602014-07-17 07:32:53 +00007438OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7439 SourceLocation StartLoc,
7440 SourceLocation LParenLoc,
7441 SourceLocation EndLoc) {
7442 Expr *ValExpr = Condition;
7443 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7444 !Condition->isInstantiationDependent() &&
7445 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007446 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007447 if (Val.isInvalid())
7448 return nullptr;
7449
Richard Smith03a4aa32016-06-23 19:02:52 +00007450 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007451 }
7452
7453 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7454}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007455ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7456 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007457 if (!Op)
7458 return ExprError();
7459
7460 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7461 public:
7462 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007463 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007464 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7465 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007466 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7467 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007468 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7469 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007470 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7471 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007472 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7473 QualType T,
7474 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007475 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7476 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007477 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7478 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007479 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007480 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007481 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007482 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7483 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007484 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7485 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007486 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7487 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007488 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007489 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007490 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007491 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7492 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007493 llvm_unreachable("conversion functions are permitted");
7494 }
7495 } ConvertDiagnoser;
7496 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7497}
7498
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007499static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007500 OpenMPClauseKind CKind,
7501 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007502 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7503 !ValExpr->isInstantiationDependent()) {
7504 SourceLocation Loc = ValExpr->getExprLoc();
7505 ExprResult Value =
7506 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7507 if (Value.isInvalid())
7508 return false;
7509
7510 ValExpr = Value.get();
7511 // The expression must evaluate to a non-negative integer value.
7512 llvm::APSInt Result;
7513 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007514 Result.isSigned() &&
7515 !((!StrictlyPositive && Result.isNonNegative()) ||
7516 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007517 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007518 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7519 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007520 return false;
7521 }
7522 }
7523 return true;
7524}
7525
Alexey Bataev568a8332014-03-06 06:15:19 +00007526OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7527 SourceLocation StartLoc,
7528 SourceLocation LParenLoc,
7529 SourceLocation EndLoc) {
7530 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007531
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007532 // OpenMP [2.5, Restrictions]
7533 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007534 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7535 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007536 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007537
Alexey Bataeved09d242014-05-28 05:53:51 +00007538 return new (Context)
7539 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007540}
7541
Alexey Bataev62c87d22014-03-21 04:51:18 +00007542ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007543 OpenMPClauseKind CKind,
7544 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007545 if (!E)
7546 return ExprError();
7547 if (E->isValueDependent() || E->isTypeDependent() ||
7548 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007549 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007550 llvm::APSInt Result;
7551 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7552 if (ICE.isInvalid())
7553 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007554 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7555 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007556 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007557 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7558 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007559 return ExprError();
7560 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007561 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7562 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7563 << E->getSourceRange();
7564 return ExprError();
7565 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007566 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7567 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007568 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007569 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007570 return ICE;
7571}
7572
7573OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7574 SourceLocation LParenLoc,
7575 SourceLocation EndLoc) {
7576 // OpenMP [2.8.1, simd construct, Description]
7577 // The parameter of the safelen clause must be a constant
7578 // positive integer expression.
7579 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7580 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007581 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007582 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007583 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007584}
7585
Alexey Bataev66b15b52015-08-21 11:14:16 +00007586OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7587 SourceLocation LParenLoc,
7588 SourceLocation EndLoc) {
7589 // OpenMP [2.8.1, simd construct, Description]
7590 // The parameter of the simdlen clause must be a constant
7591 // positive integer expression.
7592 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7593 if (Simdlen.isInvalid())
7594 return nullptr;
7595 return new (Context)
7596 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7597}
7598
Alexander Musman64d33f12014-06-04 07:53:32 +00007599OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7600 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007601 SourceLocation LParenLoc,
7602 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007603 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007604 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007605 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007606 // The parameter of the collapse clause must be a constant
7607 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007608 ExprResult NumForLoopsResult =
7609 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7610 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007611 return nullptr;
7612 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007613 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007614}
7615
Alexey Bataev10e775f2015-07-30 11:36:16 +00007616OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7617 SourceLocation EndLoc,
7618 SourceLocation LParenLoc,
7619 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007620 // OpenMP [2.7.1, loop construct, Description]
7621 // OpenMP [2.8.1, simd construct, Description]
7622 // OpenMP [2.9.6, distribute construct, Description]
7623 // The parameter of the ordered clause must be a constant
7624 // positive integer expression if any.
7625 if (NumForLoops && LParenLoc.isValid()) {
7626 ExprResult NumForLoopsResult =
7627 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7628 if (NumForLoopsResult.isInvalid())
7629 return nullptr;
7630 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007631 } else
7632 NumForLoops = nullptr;
7633 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007634 return new (Context)
7635 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7636}
7637
Alexey Bataeved09d242014-05-28 05:53:51 +00007638OMPClause *Sema::ActOnOpenMPSimpleClause(
7639 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7640 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007641 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007642 switch (Kind) {
7643 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007644 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007645 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7646 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007647 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007648 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007649 Res = ActOnOpenMPProcBindClause(
7650 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7651 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007652 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007653 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007654 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007655 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007656 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007657 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007658 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007659 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007660 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007661 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007662 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007663 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007664 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007665 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007666 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007667 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007668 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007669 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007670 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007671 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007672 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007673 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007674 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007675 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007676 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007677 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007678 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007679 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007680 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007681 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007682 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007683 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007684 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007685 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007686 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007687 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007688 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007689 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007690 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007691 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007692 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007693 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007694 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007695 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007696 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007697 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007698 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007699 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007700 llvm_unreachable("Clause is not allowed.");
7701 }
7702 return Res;
7703}
7704
Alexey Bataev6402bca2015-12-28 07:25:51 +00007705static std::string
7706getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7707 ArrayRef<unsigned> Exclude = llvm::None) {
7708 std::string Values;
7709 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7710 unsigned Skipped = Exclude.size();
7711 auto S = Exclude.begin(), E = Exclude.end();
7712 for (unsigned i = First; i < Last; ++i) {
7713 if (std::find(S, E, i) != E) {
7714 --Skipped;
7715 continue;
7716 }
7717 Values += "'";
7718 Values += getOpenMPSimpleClauseTypeName(K, i);
7719 Values += "'";
7720 if (i == Bound - Skipped)
7721 Values += " or ";
7722 else if (i != Bound + 1 - Skipped)
7723 Values += ", ";
7724 }
7725 return Values;
7726}
7727
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007728OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7729 SourceLocation KindKwLoc,
7730 SourceLocation StartLoc,
7731 SourceLocation LParenLoc,
7732 SourceLocation EndLoc) {
7733 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007734 static_assert(OMPC_DEFAULT_unknown > 0,
7735 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007736 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007737 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7738 /*Last=*/OMPC_DEFAULT_unknown)
7739 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007740 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007741 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007742 switch (Kind) {
7743 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007744 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007745 break;
7746 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007747 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007748 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007749 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007750 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007751 break;
7752 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007753 return new (Context)
7754 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007755}
7756
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007757OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7758 SourceLocation KindKwLoc,
7759 SourceLocation StartLoc,
7760 SourceLocation LParenLoc,
7761 SourceLocation EndLoc) {
7762 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007763 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007764 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7765 /*Last=*/OMPC_PROC_BIND_unknown)
7766 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007767 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007768 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007769 return new (Context)
7770 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007771}
7772
Alexey Bataev56dafe82014-06-20 07:16:17 +00007773OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007774 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007775 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007776 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007777 SourceLocation EndLoc) {
7778 OMPClause *Res = nullptr;
7779 switch (Kind) {
7780 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007781 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7782 assert(Argument.size() == NumberOfElements &&
7783 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007784 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007785 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7786 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7787 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7788 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7789 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007790 break;
7791 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007792 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7793 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7794 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7795 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007796 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007797 case OMPC_dist_schedule:
7798 Res = ActOnOpenMPDistScheduleClause(
7799 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7800 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7801 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007802 case OMPC_defaultmap:
7803 enum { Modifier, DefaultmapKind };
7804 Res = ActOnOpenMPDefaultmapClause(
7805 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7806 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7807 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7808 ArgumentLoc[DefaultmapKind], EndLoc);
7809 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007810 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007811 case OMPC_num_threads:
7812 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007813 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007814 case OMPC_collapse:
7815 case OMPC_default:
7816 case OMPC_proc_bind:
7817 case OMPC_private:
7818 case OMPC_firstprivate:
7819 case OMPC_lastprivate:
7820 case OMPC_shared:
7821 case OMPC_reduction:
7822 case OMPC_linear:
7823 case OMPC_aligned:
7824 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007825 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007826 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007827 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007828 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007829 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007830 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007831 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007832 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007833 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007834 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007835 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007836 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007837 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007838 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007839 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007840 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007841 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007842 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007843 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007844 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007845 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007846 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007847 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007848 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007849 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007850 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007851 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007852 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007853 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007854 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007855 llvm_unreachable("Clause is not allowed.");
7856 }
7857 return Res;
7858}
7859
Alexey Bataev6402bca2015-12-28 07:25:51 +00007860static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7861 OpenMPScheduleClauseModifier M2,
7862 SourceLocation M1Loc, SourceLocation M2Loc) {
7863 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7864 SmallVector<unsigned, 2> Excluded;
7865 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7866 Excluded.push_back(M2);
7867 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7868 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7869 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7870 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7871 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7872 << getListOfPossibleValues(OMPC_schedule,
7873 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7874 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7875 Excluded)
7876 << getOpenMPClauseName(OMPC_schedule);
7877 return true;
7878 }
7879 return false;
7880}
7881
Alexey Bataev56dafe82014-06-20 07:16:17 +00007882OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007883 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007884 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007885 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7886 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7887 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7888 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7889 return nullptr;
7890 // OpenMP, 2.7.1, Loop Construct, Restrictions
7891 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7892 // but not both.
7893 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7894 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7895 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7896 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7897 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7898 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7899 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7900 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7901 return nullptr;
7902 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007903 if (Kind == OMPC_SCHEDULE_unknown) {
7904 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007905 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7906 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7907 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7908 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7909 Exclude);
7910 } else {
7911 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7912 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007913 }
7914 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7915 << Values << getOpenMPClauseName(OMPC_schedule);
7916 return nullptr;
7917 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007918 // OpenMP, 2.7.1, Loop Construct, Restrictions
7919 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7920 // schedule(guided).
7921 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7922 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7923 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7924 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7925 diag::err_omp_schedule_nonmonotonic_static);
7926 return nullptr;
7927 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007928 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007929 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007930 if (ChunkSize) {
7931 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7932 !ChunkSize->isInstantiationDependent() &&
7933 !ChunkSize->containsUnexpandedParameterPack()) {
7934 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7935 ExprResult Val =
7936 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7937 if (Val.isInvalid())
7938 return nullptr;
7939
7940 ValExpr = Val.get();
7941
7942 // OpenMP [2.7.1, Restrictions]
7943 // chunk_size must be a loop invariant integer expression with a positive
7944 // value.
7945 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007946 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7947 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7948 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007949 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007950 return nullptr;
7951 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007952 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7953 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007954 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7955 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7956 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007957 }
7958 }
7959 }
7960
Alexey Bataev6402bca2015-12-28 07:25:51 +00007961 return new (Context)
7962 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007963 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007964}
7965
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007966OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7967 SourceLocation StartLoc,
7968 SourceLocation EndLoc) {
7969 OMPClause *Res = nullptr;
7970 switch (Kind) {
7971 case OMPC_ordered:
7972 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7973 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007974 case OMPC_nowait:
7975 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7976 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007977 case OMPC_untied:
7978 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7979 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007980 case OMPC_mergeable:
7981 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7982 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007983 case OMPC_read:
7984 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7985 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007986 case OMPC_write:
7987 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7988 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007989 case OMPC_update:
7990 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7991 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007992 case OMPC_capture:
7993 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7994 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007995 case OMPC_seq_cst:
7996 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7997 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007998 case OMPC_threads:
7999 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8000 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008001 case OMPC_simd:
8002 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8003 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00008004 case OMPC_nogroup:
8005 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8006 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008007 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008008 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008009 case OMPC_num_threads:
8010 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008011 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008012 case OMPC_collapse:
8013 case OMPC_schedule:
8014 case OMPC_private:
8015 case OMPC_firstprivate:
8016 case OMPC_lastprivate:
8017 case OMPC_shared:
8018 case OMPC_reduction:
8019 case OMPC_linear:
8020 case OMPC_aligned:
8021 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008022 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008023 case OMPC_default:
8024 case OMPC_proc_bind:
8025 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008026 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008027 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008028 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008029 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008030 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008031 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008032 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008033 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00008034 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008035 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008036 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008037 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008038 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008039 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008040 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008041 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008042 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008043 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008044 llvm_unreachable("Clause is not allowed.");
8045 }
8046 return Res;
8047}
8048
Alexey Bataev236070f2014-06-20 11:19:47 +00008049OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
8050 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008051 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00008052 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
8053}
8054
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008055OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
8056 SourceLocation EndLoc) {
8057 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
8058}
8059
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008060OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
8061 SourceLocation EndLoc) {
8062 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
8063}
8064
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008065OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
8066 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008067 return new (Context) OMPReadClause(StartLoc, EndLoc);
8068}
8069
Alexey Bataevdea47612014-07-23 07:46:59 +00008070OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
8071 SourceLocation EndLoc) {
8072 return new (Context) OMPWriteClause(StartLoc, EndLoc);
8073}
8074
Alexey Bataev67a4f222014-07-23 10:25:33 +00008075OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
8076 SourceLocation EndLoc) {
8077 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
8078}
8079
Alexey Bataev459dec02014-07-24 06:46:57 +00008080OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
8081 SourceLocation EndLoc) {
8082 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
8083}
8084
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008085OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8086 SourceLocation EndLoc) {
8087 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8088}
8089
Alexey Bataev346265e2015-09-25 10:37:12 +00008090OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8091 SourceLocation EndLoc) {
8092 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8093}
8094
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008095OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8096 SourceLocation EndLoc) {
8097 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8098}
8099
Alexey Bataevb825de12015-12-07 10:51:44 +00008100OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8101 SourceLocation EndLoc) {
8102 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8103}
8104
Alexey Bataevc5e02582014-06-16 07:08:35 +00008105OMPClause *Sema::ActOnOpenMPVarListClause(
8106 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8107 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8108 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008109 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00008110 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8111 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8112 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008113 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008114 switch (Kind) {
8115 case OMPC_private:
8116 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8117 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008118 case OMPC_firstprivate:
8119 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8120 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008121 case OMPC_lastprivate:
8122 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8123 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008124 case OMPC_shared:
8125 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8126 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008127 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008128 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8129 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008130 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008131 case OMPC_linear:
8132 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008133 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008134 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008135 case OMPC_aligned:
8136 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8137 ColonLoc, EndLoc);
8138 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008139 case OMPC_copyin:
8140 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8141 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008142 case OMPC_copyprivate:
8143 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8144 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008145 case OMPC_flush:
8146 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8147 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008148 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008149 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8150 StartLoc, LParenLoc, EndLoc);
8151 break;
8152 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008153 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8154 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8155 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008156 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008157 case OMPC_to:
8158 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8159 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008160 case OMPC_from:
8161 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8162 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008163 case OMPC_use_device_ptr:
8164 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8165 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008166 case OMPC_is_device_ptr:
8167 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8168 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008169 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008170 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008171 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008172 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008173 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008174 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008175 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008176 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008177 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008178 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008179 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008180 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008181 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008182 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008183 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008184 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008185 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008186 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008187 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008188 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008189 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008190 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008191 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008192 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008193 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008194 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008195 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008196 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008197 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008198 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008199 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008200 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008201 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008202 llvm_unreachable("Clause is not allowed.");
8203 }
8204 return Res;
8205}
8206
Alexey Bataev90c228f2016-02-08 09:29:13 +00008207ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008208 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008209 ExprResult Res = BuildDeclRefExpr(
8210 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8211 if (!Res.isUsable())
8212 return ExprError();
8213 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8214 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8215 if (!Res.isUsable())
8216 return ExprError();
8217 }
8218 if (VK != VK_LValue && Res.get()->isGLValue()) {
8219 Res = DefaultLvalueConversion(Res.get());
8220 if (!Res.isUsable())
8221 return ExprError();
8222 }
8223 return Res;
8224}
8225
Alexey Bataev60da77e2016-02-29 05:54:20 +00008226static std::pair<ValueDecl *, bool>
8227getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8228 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008229 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8230 RefExpr->containsUnexpandedParameterPack())
8231 return std::make_pair(nullptr, true);
8232
Alexey Bataevd985eda2016-02-10 11:29:16 +00008233 // OpenMP [3.1, C/C++]
8234 // A list item is a variable name.
8235 // OpenMP [2.9.3.3, Restrictions, p.1]
8236 // A variable that is part of another variable (as an array or
8237 // structure element) cannot appear in a private clause.
8238 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008239 enum {
8240 NoArrayExpr = -1,
8241 ArraySubscript = 0,
8242 OMPArraySection = 1
8243 } IsArrayExpr = NoArrayExpr;
8244 if (AllowArraySection) {
8245 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8246 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8247 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8248 Base = TempASE->getBase()->IgnoreParenImpCasts();
8249 RefExpr = Base;
8250 IsArrayExpr = ArraySubscript;
8251 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8252 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8253 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8254 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8255 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8256 Base = TempASE->getBase()->IgnoreParenImpCasts();
8257 RefExpr = Base;
8258 IsArrayExpr = OMPArraySection;
8259 }
8260 }
8261 ELoc = RefExpr->getExprLoc();
8262 ERange = RefExpr->getSourceRange();
8263 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008264 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8265 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8266 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8267 (S.getCurrentThisType().isNull() || !ME ||
8268 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8269 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008270 if (IsArrayExpr != NoArrayExpr)
8271 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8272 << ERange;
8273 else {
8274 S.Diag(ELoc,
8275 AllowArraySection
8276 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8277 : diag::err_omp_expected_var_name_member_expr)
8278 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8279 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008280 return std::make_pair(nullptr, false);
8281 }
8282 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8283}
8284
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008285OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8286 SourceLocation StartLoc,
8287 SourceLocation LParenLoc,
8288 SourceLocation EndLoc) {
8289 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008290 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008291 for (auto &RefExpr : VarList) {
8292 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008293 SourceLocation ELoc;
8294 SourceRange ERange;
8295 Expr *SimpleRefExpr = RefExpr;
8296 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008297 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008298 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008299 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008300 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008301 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008302 ValueDecl *D = Res.first;
8303 if (!D)
8304 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008305
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008306 QualType Type = D->getType();
8307 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008308
8309 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8310 // A variable that appears in a private clause must not have an incomplete
8311 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008312 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008313 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008314 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008315
Alexey Bataev758e55e2013-09-06 18:03:48 +00008316 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8317 // in a Construct]
8318 // Variables with the predetermined data-sharing attributes may not be
8319 // listed in data-sharing attributes clauses, except for the cases
8320 // listed below. For these exceptions only, listing a predetermined
8321 // variable in a data-sharing attribute clause is allowed and overrides
8322 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008323 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008324 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008325 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8326 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008327 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008328 continue;
8329 }
8330
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008331 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008332 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008333 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008334 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8335 << getOpenMPClauseName(OMPC_private) << Type
8336 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8337 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008338 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008339 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008340 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008341 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008342 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008343 continue;
8344 }
8345
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008346 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8347 // A list item cannot appear in both a map clause and a data-sharing
8348 // attribute clause on the same construct
8349 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008350 if (DSAStack->checkMappableExprComponentListsForDecl(
8351 VD, /* CurrentRegionOnly = */ true,
8352 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8353 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008354 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8355 << getOpenMPClauseName(OMPC_private)
8356 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8357 ReportOriginalDSA(*this, DSAStack, D, DVar);
8358 continue;
8359 }
8360 }
8361
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008362 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8363 // A variable of class type (or array thereof) that appears in a private
8364 // clause requires an accessible, unambiguous default constructor for the
8365 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008366 // Generate helper private variable and initialize it with the default
8367 // value. The address of the original variable is replaced by the address of
8368 // the new private variable in CodeGen. This new variable is not added to
8369 // IdResolver, so the code in the OpenMP region uses original variable for
8370 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008371 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008372 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8373 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008374 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008375 if (VDPrivate->isInvalidDecl())
8376 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008377 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008378 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008379
Alexey Bataev90c228f2016-02-08 09:29:13 +00008380 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008381 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008382 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008383 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008384 Vars.push_back((VD || CurContext->isDependentContext())
8385 ? RefExpr->IgnoreParens()
8386 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008387 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008388 }
8389
Alexey Bataeved09d242014-05-28 05:53:51 +00008390 if (Vars.empty())
8391 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008392
Alexey Bataev03b340a2014-10-21 03:16:40 +00008393 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8394 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008395}
8396
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008397namespace {
8398class DiagsUninitializedSeveretyRAII {
8399private:
8400 DiagnosticsEngine &Diags;
8401 SourceLocation SavedLoc;
8402 bool IsIgnored;
8403
8404public:
8405 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8406 bool IsIgnored)
8407 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8408 if (!IsIgnored) {
8409 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8410 /*Map*/ diag::Severity::Ignored, Loc);
8411 }
8412 }
8413 ~DiagsUninitializedSeveretyRAII() {
8414 if (!IsIgnored)
8415 Diags.popMappings(SavedLoc);
8416 }
8417};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008418}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008419
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008420OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8421 SourceLocation StartLoc,
8422 SourceLocation LParenLoc,
8423 SourceLocation EndLoc) {
8424 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008425 SmallVector<Expr *, 8> PrivateCopies;
8426 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008427 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008428 bool IsImplicitClause =
8429 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8430 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8431
Alexey Bataeved09d242014-05-28 05:53:51 +00008432 for (auto &RefExpr : VarList) {
8433 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008434 SourceLocation ELoc;
8435 SourceRange ERange;
8436 Expr *SimpleRefExpr = RefExpr;
8437 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008438 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008439 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008440 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008441 PrivateCopies.push_back(nullptr);
8442 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008443 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008444 ValueDecl *D = Res.first;
8445 if (!D)
8446 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008447
Alexey Bataev60da77e2016-02-29 05:54:20 +00008448 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008449 QualType Type = D->getType();
8450 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008451
8452 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8453 // A variable that appears in a private clause must not have an incomplete
8454 // type or a reference type.
8455 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008456 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008457 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008458 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008459
8460 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8461 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008462 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008463 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008464 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008465
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008466 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008467 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008468 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008469 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008470 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008471 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008472 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8473 // A list item that specifies a given variable may not appear in more
8474 // than one clause on the same directive, except that a variable may be
8475 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008476 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008477 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008478 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008479 << getOpenMPClauseName(DVar.CKind)
8480 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008481 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008482 continue;
8483 }
8484
8485 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8486 // in a Construct]
8487 // Variables with the predetermined data-sharing attributes may not be
8488 // listed in data-sharing attributes clauses, except for the cases
8489 // listed below. For these exceptions only, listing a predetermined
8490 // variable in a data-sharing attribute clause is allowed and overrides
8491 // the variable's predetermined data-sharing attributes.
8492 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8493 // in a Construct, C/C++, p.2]
8494 // Variables with const-qualified type having no mutable member may be
8495 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008496 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008497 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8498 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008499 << getOpenMPClauseName(DVar.CKind)
8500 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008501 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008502 continue;
8503 }
8504
Alexey Bataevf29276e2014-06-18 04:14:57 +00008505 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008506 // OpenMP [2.9.3.4, Restrictions, p.2]
8507 // A list item that is private within a parallel region must not appear
8508 // in a firstprivate clause on a worksharing construct if any of the
8509 // worksharing regions arising from the worksharing construct ever bind
8510 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008511 if (isOpenMPWorksharingDirective(CurrDir) &&
8512 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008513 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008514 if (DVar.CKind != OMPC_shared &&
8515 (isOpenMPParallelDirective(DVar.DKind) ||
8516 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008517 Diag(ELoc, diag::err_omp_required_access)
8518 << getOpenMPClauseName(OMPC_firstprivate)
8519 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008520 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008521 continue;
8522 }
8523 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008524 // OpenMP [2.9.3.4, Restrictions, p.3]
8525 // A list item that appears in a reduction clause of a parallel construct
8526 // must not appear in a firstprivate clause on a worksharing or task
8527 // construct if any of the worksharing or task regions arising from the
8528 // worksharing or task construct ever bind to any of the parallel regions
8529 // arising from the parallel construct.
8530 // OpenMP [2.9.3.4, Restrictions, p.4]
8531 // A list item that appears in a reduction clause in worksharing
8532 // construct must not appear in a firstprivate clause in a task construct
8533 // encountered during execution of any of the worksharing regions arising
8534 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008535 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008536 DVar = DSAStack->hasInnermostDSA(
8537 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8538 [](OpenMPDirectiveKind K) -> bool {
8539 return isOpenMPParallelDirective(K) ||
8540 isOpenMPWorksharingDirective(K);
8541 },
8542 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008543 if (DVar.CKind == OMPC_reduction &&
8544 (isOpenMPParallelDirective(DVar.DKind) ||
8545 isOpenMPWorksharingDirective(DVar.DKind))) {
8546 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8547 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008548 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008549 continue;
8550 }
8551 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008552
8553 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8554 // A list item that is private within a teams region must not appear in a
8555 // firstprivate clause on a distribute construct if any of the distribute
8556 // regions arising from the distribute construct ever bind to any of the
8557 // teams regions arising from the teams construct.
8558 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8559 // A list item that appears in a reduction clause of a teams construct
8560 // must not appear in a firstprivate clause on a distribute construct if
8561 // any of the distribute regions arising from the distribute construct
8562 // ever bind to any of the teams regions arising from the teams construct.
8563 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8564 // A list item may appear in a firstprivate or lastprivate clause but not
8565 // both.
8566 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008567 DVar = DSAStack->hasInnermostDSA(
8568 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8569 [](OpenMPDirectiveKind K) -> bool {
8570 return isOpenMPTeamsDirective(K);
8571 },
8572 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008573 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8574 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008575 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008576 continue;
8577 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008578 DVar = DSAStack->hasInnermostDSA(
8579 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8580 [](OpenMPDirectiveKind K) -> bool {
8581 return isOpenMPTeamsDirective(K);
8582 },
8583 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008584 if (DVar.CKind == OMPC_reduction &&
8585 isOpenMPTeamsDirective(DVar.DKind)) {
8586 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008587 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008588 continue;
8589 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008590 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008591 if (DVar.CKind == OMPC_lastprivate) {
8592 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008593 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008594 continue;
8595 }
8596 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008597 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8598 // A list item cannot appear in both a map clause and a data-sharing
8599 // attribute clause on the same construct
8600 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008601 if (DSAStack->checkMappableExprComponentListsForDecl(
8602 VD, /* CurrentRegionOnly = */ true,
8603 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8604 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008605 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8606 << getOpenMPClauseName(OMPC_firstprivate)
8607 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8608 ReportOriginalDSA(*this, DSAStack, D, DVar);
8609 continue;
8610 }
8611 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008612 }
8613
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008614 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008615 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008616 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008617 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8618 << getOpenMPClauseName(OMPC_firstprivate) << Type
8619 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8620 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008621 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008622 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008623 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008624 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008625 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008626 continue;
8627 }
8628
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008629 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008630 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8631 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008632 // Generate helper private variable and initialize it with the value of the
8633 // original variable. The address of the original variable is replaced by
8634 // the address of the new private variable in the CodeGen. This new variable
8635 // is not added to IdResolver, so the code in the OpenMP region uses
8636 // original variable for proper diagnostics and variable capturing.
8637 Expr *VDInitRefExpr = nullptr;
8638 // For arrays generate initializer for single element and replace it by the
8639 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008640 if (Type->isArrayType()) {
8641 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008642 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008643 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008644 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008645 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008646 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008647 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008648 InitializedEntity Entity =
8649 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008650 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8651
8652 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8653 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8654 if (Result.isInvalid())
8655 VDPrivate->setInvalidDecl();
8656 else
8657 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008658 // Remove temp variable declaration.
8659 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008660 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008661 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8662 ".firstprivate.temp");
8663 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8664 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008665 AddInitializerToDecl(VDPrivate,
8666 DefaultLvalueConversion(VDInitRefExpr).get(),
8667 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008668 }
8669 if (VDPrivate->isInvalidDecl()) {
8670 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008671 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008672 diag::note_omp_task_predetermined_firstprivate_here);
8673 }
8674 continue;
8675 }
8676 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008677 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008678 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8679 RefExpr->getExprLoc());
8680 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008681 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008682 if (TopDVar.CKind == OMPC_lastprivate)
8683 Ref = TopDVar.PrivateCopy;
8684 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008685 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008686 if (!IsOpenMPCapturedDecl(D))
8687 ExprCaptures.push_back(Ref->getDecl());
8688 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008689 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008690 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008691 Vars.push_back((VD || CurContext->isDependentContext())
8692 ? RefExpr->IgnoreParens()
8693 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008694 PrivateCopies.push_back(VDPrivateRefExpr);
8695 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008696 }
8697
Alexey Bataeved09d242014-05-28 05:53:51 +00008698 if (Vars.empty())
8699 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008700
8701 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008702 Vars, PrivateCopies, Inits,
8703 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008704}
8705
Alexander Musman1bb328c2014-06-04 13:06:39 +00008706OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8707 SourceLocation StartLoc,
8708 SourceLocation LParenLoc,
8709 SourceLocation EndLoc) {
8710 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008711 SmallVector<Expr *, 8> SrcExprs;
8712 SmallVector<Expr *, 8> DstExprs;
8713 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008714 SmallVector<Decl *, 4> ExprCaptures;
8715 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008716 for (auto &RefExpr : VarList) {
8717 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008718 SourceLocation ELoc;
8719 SourceRange ERange;
8720 Expr *SimpleRefExpr = RefExpr;
8721 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008722 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008723 // It will be analyzed later.
8724 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008725 SrcExprs.push_back(nullptr);
8726 DstExprs.push_back(nullptr);
8727 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008728 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008729 ValueDecl *D = Res.first;
8730 if (!D)
8731 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008732
Alexey Bataev74caaf22016-02-20 04:09:36 +00008733 QualType Type = D->getType();
8734 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008735
8736 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8737 // A variable that appears in a lastprivate clause must not have an
8738 // incomplete type or a reference type.
8739 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008740 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008741 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008742 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008743
8744 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8745 // in a Construct]
8746 // Variables with the predetermined data-sharing attributes may not be
8747 // listed in data-sharing attributes clauses, except for the cases
8748 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008749 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008750 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8751 DVar.CKind != OMPC_firstprivate &&
8752 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8753 Diag(ELoc, diag::err_omp_wrong_dsa)
8754 << getOpenMPClauseName(DVar.CKind)
8755 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008756 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008757 continue;
8758 }
8759
Alexey Bataevf29276e2014-06-18 04:14:57 +00008760 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8761 // OpenMP [2.14.3.5, Restrictions, p.2]
8762 // A list item that is private within a parallel region, or that appears in
8763 // the reduction clause of a parallel construct, must not appear in a
8764 // lastprivate clause on a worksharing construct if any of the corresponding
8765 // worksharing regions ever binds to any of the corresponding parallel
8766 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008767 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008768 if (isOpenMPWorksharingDirective(CurrDir) &&
8769 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008770 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008771 if (DVar.CKind != OMPC_shared) {
8772 Diag(ELoc, diag::err_omp_required_access)
8773 << getOpenMPClauseName(OMPC_lastprivate)
8774 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008775 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008776 continue;
8777 }
8778 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008779
8780 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8781 // A list item may appear in a firstprivate or lastprivate clause but not
8782 // both.
8783 if (CurrDir == OMPD_distribute) {
8784 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8785 if (DVar.CKind == OMPC_firstprivate) {
8786 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8787 ReportOriginalDSA(*this, DSAStack, D, DVar);
8788 continue;
8789 }
8790 }
8791
Alexander Musman1bb328c2014-06-04 13:06:39 +00008792 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008793 // A variable of class type (or array thereof) that appears in a
8794 // lastprivate clause requires an accessible, unambiguous default
8795 // constructor for the class type, unless the list item is also specified
8796 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008797 // A variable of class type (or array thereof) that appears in a
8798 // lastprivate clause requires an accessible, unambiguous copy assignment
8799 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008800 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008801 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008802 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008803 D->hasAttrs() ? &D->getAttrs() : nullptr);
8804 auto *PseudoSrcExpr =
8805 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008806 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008807 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008808 D->hasAttrs() ? &D->getAttrs() : nullptr);
8809 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008810 // For arrays generate assignment operation for single element and replace
8811 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008812 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008813 PseudoDstExpr, PseudoSrcExpr);
8814 if (AssignmentOp.isInvalid())
8815 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008816 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008817 /*DiscardedValue=*/true);
8818 if (AssignmentOp.isInvalid())
8819 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008820
Alexey Bataev74caaf22016-02-20 04:09:36 +00008821 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008822 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008823 if (TopDVar.CKind == OMPC_firstprivate)
8824 Ref = TopDVar.PrivateCopy;
8825 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008826 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008827 if (!IsOpenMPCapturedDecl(D))
8828 ExprCaptures.push_back(Ref->getDecl());
8829 }
8830 if (TopDVar.CKind == OMPC_firstprivate ||
8831 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008832 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008833 ExprResult RefRes = DefaultLvalueConversion(Ref);
8834 if (!RefRes.isUsable())
8835 continue;
8836 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008837 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8838 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008839 if (!PostUpdateRes.isUsable())
8840 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008841 ExprPostUpdates.push_back(
8842 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008843 }
8844 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008845 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008846 Vars.push_back((VD || CurContext->isDependentContext())
8847 ? RefExpr->IgnoreParens()
8848 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008849 SrcExprs.push_back(PseudoSrcExpr);
8850 DstExprs.push_back(PseudoDstExpr);
8851 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008852 }
8853
8854 if (Vars.empty())
8855 return nullptr;
8856
8857 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008858 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008859 buildPreInits(Context, ExprCaptures),
8860 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008861}
8862
Alexey Bataev758e55e2013-09-06 18:03:48 +00008863OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8864 SourceLocation StartLoc,
8865 SourceLocation LParenLoc,
8866 SourceLocation EndLoc) {
8867 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008868 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008869 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008870 SourceLocation ELoc;
8871 SourceRange ERange;
8872 Expr *SimpleRefExpr = RefExpr;
8873 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008874 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008875 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008876 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008877 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008878 ValueDecl *D = Res.first;
8879 if (!D)
8880 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008881
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008882 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008883 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8884 // in a Construct]
8885 // Variables with the predetermined data-sharing attributes may not be
8886 // listed in data-sharing attributes clauses, except for the cases
8887 // listed below. For these exceptions only, listing a predetermined
8888 // variable in a data-sharing attribute clause is allowed and overrides
8889 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008890 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008891 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8892 DVar.RefExpr) {
8893 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8894 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008895 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008896 continue;
8897 }
8898
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008899 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008900 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008901 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008902 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008903 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8904 ? RefExpr->IgnoreParens()
8905 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008906 }
8907
Alexey Bataeved09d242014-05-28 05:53:51 +00008908 if (Vars.empty())
8909 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008910
8911 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8912}
8913
Alexey Bataevc5e02582014-06-16 07:08:35 +00008914namespace {
8915class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8916 DSAStackTy *Stack;
8917
8918public:
8919 bool VisitDeclRefExpr(DeclRefExpr *E) {
8920 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008921 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008922 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8923 return false;
8924 if (DVar.CKind != OMPC_unknown)
8925 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008926 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8927 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8928 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008929 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008930 return true;
8931 return false;
8932 }
8933 return false;
8934 }
8935 bool VisitStmt(Stmt *S) {
8936 for (auto Child : S->children()) {
8937 if (Child && Visit(Child))
8938 return true;
8939 }
8940 return false;
8941 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008942 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008943};
Alexey Bataev23b69422014-06-18 07:08:49 +00008944} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008945
Alexey Bataev60da77e2016-02-29 05:54:20 +00008946namespace {
8947// Transform MemberExpression for specified FieldDecl of current class to
8948// DeclRefExpr to specified OMPCapturedExprDecl.
8949class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8950 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8951 ValueDecl *Field;
8952 DeclRefExpr *CapturedExpr;
8953
8954public:
8955 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8956 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8957
8958 ExprResult TransformMemberExpr(MemberExpr *E) {
8959 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8960 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008961 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008962 return CapturedExpr;
8963 }
8964 return BaseTransform::TransformMemberExpr(E);
8965 }
8966 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8967};
8968} // namespace
8969
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008970template <typename T>
8971static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8972 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8973 for (auto &Set : Lookups) {
8974 for (auto *D : Set) {
8975 if (auto Res = Gen(cast<ValueDecl>(D)))
8976 return Res;
8977 }
8978 }
8979 return T();
8980}
8981
8982static ExprResult
8983buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8984 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8985 const DeclarationNameInfo &ReductionId, QualType Ty,
8986 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8987 if (ReductionIdScopeSpec.isInvalid())
8988 return ExprError();
8989 SmallVector<UnresolvedSet<8>, 4> Lookups;
8990 if (S) {
8991 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8992 Lookup.suppressDiagnostics();
8993 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8994 auto *D = Lookup.getRepresentativeDecl();
8995 do {
8996 S = S->getParent();
8997 } while (S && !S->isDeclScope(D));
8998 if (S)
8999 S = S->getParent();
9000 Lookups.push_back(UnresolvedSet<8>());
9001 Lookups.back().append(Lookup.begin(), Lookup.end());
9002 Lookup.clear();
9003 }
9004 } else if (auto *ULE =
9005 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9006 Lookups.push_back(UnresolvedSet<8>());
9007 Decl *PrevD = nullptr;
9008 for(auto *D : ULE->decls()) {
9009 if (D == PrevD)
9010 Lookups.push_back(UnresolvedSet<8>());
9011 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9012 Lookups.back().addDecl(DRD);
9013 PrevD = D;
9014 }
9015 }
9016 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
9017 Ty->containsUnexpandedParameterPack() ||
9018 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9019 return !D->isInvalidDecl() &&
9020 (D->getType()->isDependentType() ||
9021 D->getType()->isInstantiationDependentType() ||
9022 D->getType()->containsUnexpandedParameterPack());
9023 })) {
9024 UnresolvedSet<8> ResSet;
9025 for (auto &Set : Lookups) {
9026 ResSet.append(Set.begin(), Set.end());
9027 // The last item marks the end of all declarations at the specified scope.
9028 ResSet.addDecl(Set[Set.size() - 1]);
9029 }
9030 return UnresolvedLookupExpr::Create(
9031 SemaRef.Context, /*NamingClass=*/nullptr,
9032 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9033 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9034 }
9035 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9036 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9037 if (!D->isInvalidDecl() &&
9038 SemaRef.Context.hasSameType(D->getType(), Ty))
9039 return D;
9040 return nullptr;
9041 }))
9042 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9043 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9044 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
9045 if (!D->isInvalidDecl() &&
9046 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
9047 !Ty.isMoreQualifiedThan(D->getType()))
9048 return D;
9049 return nullptr;
9050 })) {
9051 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
9052 /*DetectVirtual=*/false);
9053 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
9054 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
9055 VD->getType().getUnqualifiedType()))) {
9056 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
9057 /*DiagID=*/0) !=
9058 Sema::AR_inaccessible) {
9059 SemaRef.BuildBasePathArray(Paths, BasePath);
9060 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9061 }
9062 }
9063 }
9064 }
9065 if (ReductionIdScopeSpec.isSet()) {
9066 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
9067 return ExprError();
9068 }
9069 return ExprEmpty();
9070}
9071
Alexey Bataevc5e02582014-06-16 07:08:35 +00009072OMPClause *Sema::ActOnOpenMPReductionClause(
9073 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9074 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009075 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
9076 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00009077 auto DN = ReductionId.getName();
9078 auto OOK = DN.getCXXOverloadedOperator();
9079 BinaryOperatorKind BOK = BO_Comma;
9080
9081 // OpenMP [2.14.3.6, reduction clause]
9082 // C
9083 // reduction-identifier is either an identifier or one of the following
9084 // operators: +, -, *, &, |, ^, && and ||
9085 // C++
9086 // reduction-identifier is either an id-expression or one of the following
9087 // operators: +, -, *, &, |, ^, && and ||
9088 // FIXME: Only 'min' and 'max' identifiers are supported for now.
9089 switch (OOK) {
9090 case OO_Plus:
9091 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009092 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009093 break;
9094 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009095 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009096 break;
9097 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009098 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009099 break;
9100 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009101 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009102 break;
9103 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009104 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009105 break;
9106 case OO_AmpAmp:
9107 BOK = BO_LAnd;
9108 break;
9109 case OO_PipePipe:
9110 BOK = BO_LOr;
9111 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009112 case OO_New:
9113 case OO_Delete:
9114 case OO_Array_New:
9115 case OO_Array_Delete:
9116 case OO_Slash:
9117 case OO_Percent:
9118 case OO_Tilde:
9119 case OO_Exclaim:
9120 case OO_Equal:
9121 case OO_Less:
9122 case OO_Greater:
9123 case OO_LessEqual:
9124 case OO_GreaterEqual:
9125 case OO_PlusEqual:
9126 case OO_MinusEqual:
9127 case OO_StarEqual:
9128 case OO_SlashEqual:
9129 case OO_PercentEqual:
9130 case OO_CaretEqual:
9131 case OO_AmpEqual:
9132 case OO_PipeEqual:
9133 case OO_LessLess:
9134 case OO_GreaterGreater:
9135 case OO_LessLessEqual:
9136 case OO_GreaterGreaterEqual:
9137 case OO_EqualEqual:
9138 case OO_ExclaimEqual:
9139 case OO_PlusPlus:
9140 case OO_MinusMinus:
9141 case OO_Comma:
9142 case OO_ArrowStar:
9143 case OO_Arrow:
9144 case OO_Call:
9145 case OO_Subscript:
9146 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009147 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009148 case NUM_OVERLOADED_OPERATORS:
9149 llvm_unreachable("Unexpected reduction identifier");
9150 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009151 if (auto II = DN.getAsIdentifierInfo()) {
9152 if (II->isStr("max"))
9153 BOK = BO_GT;
9154 else if (II->isStr("min"))
9155 BOK = BO_LT;
9156 }
9157 break;
9158 }
9159 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009160 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009161 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009162 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009163
9164 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009165 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009166 SmallVector<Expr *, 8> LHSs;
9167 SmallVector<Expr *, 8> RHSs;
9168 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009169 SmallVector<Decl *, 4> ExprCaptures;
9170 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009171 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9172 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009173 for (auto RefExpr : VarList) {
9174 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009175 // OpenMP [2.1, C/C++]
9176 // A list item is a variable or array section, subject to the restrictions
9177 // specified in Section 2.4 on page 42 and in each of the sections
9178 // describing clauses and directives for which a list appears.
9179 // OpenMP [2.14.3.3, Restrictions, p.1]
9180 // A variable that is part of another variable (as an array or
9181 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009182 if (!FirstIter && IR != ER)
9183 ++IR;
9184 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009185 SourceLocation ELoc;
9186 SourceRange ERange;
9187 Expr *SimpleRefExpr = RefExpr;
9188 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9189 /*AllowArraySection=*/true);
9190 if (Res.second) {
9191 // It will be analyzed later.
9192 Vars.push_back(RefExpr);
9193 Privates.push_back(nullptr);
9194 LHSs.push_back(nullptr);
9195 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009196 // Try to find 'declare reduction' corresponding construct before using
9197 // builtin/overloaded operators.
9198 QualType Type = Context.DependentTy;
9199 CXXCastPath BasePath;
9200 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9201 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9202 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9203 if (CurContext->isDependentContext() &&
9204 (DeclareReductionRef.isUnset() ||
9205 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9206 ReductionOps.push_back(DeclareReductionRef.get());
9207 else
9208 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009209 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009210 ValueDecl *D = Res.first;
9211 if (!D)
9212 continue;
9213
Alexey Bataeva1764212015-09-30 09:22:36 +00009214 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009215 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9216 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9217 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009218 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009219 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009220 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9221 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9222 Type = ATy->getElementType();
9223 else
9224 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009225 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009226 } else
9227 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9228 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009229
Alexey Bataevc5e02582014-06-16 07:08:35 +00009230 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9231 // A variable that appears in a private clause must not have an incomplete
9232 // type or a reference type.
9233 if (RequireCompleteType(ELoc, Type,
9234 diag::err_omp_reduction_incomplete_type))
9235 continue;
9236 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009237 // A list item that appears in a reduction clause must not be
9238 // const-qualified.
9239 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009240 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009241 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009242 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009243 bool IsDecl = !VD ||
9244 VD->isThisDeclarationADefinition(Context) ==
9245 VarDecl::DeclarationOnly;
9246 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009247 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009248 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009249 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009250 continue;
9251 }
9252 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9253 // If a list-item is a reference type then it must bind to the same object
9254 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009255 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009256 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009257 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009258 DSARefChecker Check(DSAStack);
9259 if (Check.Visit(VDDef->getInit())) {
9260 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9261 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9262 continue;
9263 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009264 }
9265 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009266
Alexey Bataevc5e02582014-06-16 07:08:35 +00009267 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9268 // in a Construct]
9269 // Variables with the predetermined data-sharing attributes may not be
9270 // listed in data-sharing attributes clauses, except for the cases
9271 // listed below. For these exceptions only, listing a predetermined
9272 // variable in a data-sharing attribute clause is allowed and overrides
9273 // the variable's predetermined data-sharing attributes.
9274 // OpenMP [2.14.3.6, Restrictions, p.3]
9275 // Any number of reduction clauses can be specified on the directive,
9276 // but a list item can appear only once in the reduction clauses for that
9277 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009278 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009279 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009280 if (DVar.CKind == OMPC_reduction) {
9281 Diag(ELoc, diag::err_omp_once_referenced)
9282 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009283 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009284 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009285 } else if (DVar.CKind != OMPC_unknown) {
9286 Diag(ELoc, diag::err_omp_wrong_dsa)
9287 << getOpenMPClauseName(DVar.CKind)
9288 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009289 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009290 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009291 }
9292
9293 // OpenMP [2.14.3.6, Restrictions, p.1]
9294 // A list item that appears in a reduction clause of a worksharing
9295 // construct must be shared in the parallel regions to which any of the
9296 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009297 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9298 if (isOpenMPWorksharingDirective(CurrDir) &&
9299 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009300 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009301 if (DVar.CKind != OMPC_shared) {
9302 Diag(ELoc, diag::err_omp_required_access)
9303 << getOpenMPClauseName(OMPC_reduction)
9304 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009305 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009306 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009307 }
9308 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009309
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009310 // Try to find 'declare reduction' corresponding construct before using
9311 // builtin/overloaded operators.
9312 CXXCastPath BasePath;
9313 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9314 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9315 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9316 if (DeclareReductionRef.isInvalid())
9317 continue;
9318 if (CurContext->isDependentContext() &&
9319 (DeclareReductionRef.isUnset() ||
9320 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9321 Vars.push_back(RefExpr);
9322 Privates.push_back(nullptr);
9323 LHSs.push_back(nullptr);
9324 RHSs.push_back(nullptr);
9325 ReductionOps.push_back(DeclareReductionRef.get());
9326 continue;
9327 }
9328 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9329 // Not allowed reduction identifier is found.
9330 Diag(ReductionId.getLocStart(),
9331 diag::err_omp_unknown_reduction_identifier)
9332 << Type << ReductionIdRange;
9333 continue;
9334 }
9335
9336 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9337 // The type of a list item that appears in a reduction clause must be valid
9338 // for the reduction-identifier. For a max or min reduction in C, the type
9339 // of the list item must be an allowed arithmetic data type: char, int,
9340 // float, double, or _Bool, possibly modified with long, short, signed, or
9341 // unsigned. For a max or min reduction in C++, the type of the list item
9342 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9343 // double, or bool, possibly modified with long, short, signed, or unsigned.
9344 if (DeclareReductionRef.isUnset()) {
9345 if ((BOK == BO_GT || BOK == BO_LT) &&
9346 !(Type->isScalarType() ||
9347 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9348 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9349 << getLangOpts().CPlusPlus;
9350 if (!ASE && !OASE) {
9351 bool IsDecl = !VD ||
9352 VD->isThisDeclarationADefinition(Context) ==
9353 VarDecl::DeclarationOnly;
9354 Diag(D->getLocation(),
9355 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9356 << D;
9357 }
9358 continue;
9359 }
9360 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9361 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9362 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9363 if (!ASE && !OASE) {
9364 bool IsDecl = !VD ||
9365 VD->isThisDeclarationADefinition(Context) ==
9366 VarDecl::DeclarationOnly;
9367 Diag(D->getLocation(),
9368 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9369 << D;
9370 }
9371 continue;
9372 }
9373 }
9374
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009375 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009376 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009377 D->hasAttrs() ? &D->getAttrs() : nullptr);
9378 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9379 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009380 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009381 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009382 (!ASE &&
9383 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009384 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009385 // Create pseudo array type for private copy. The size for this array will
9386 // be generated during codegen.
9387 // For array subscripts or single variables Private Ty is the same as Type
9388 // (type of the variable or single array element).
9389 PrivateTy = Context.getVariableArrayType(
9390 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9391 Context.getSizeType(), VK_RValue),
9392 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009393 } else if (!ASE && !OASE &&
9394 Context.getAsArrayType(D->getType().getNonReferenceType()))
9395 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009396 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009397 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9398 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009399 // Add initializer for private variable.
9400 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009401 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9402 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9403 if (DeclareReductionRef.isUsable()) {
9404 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9405 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9406 if (DRD->getInitializer()) {
9407 Init = DRDRef;
9408 RHSVD->setInit(DRDRef);
9409 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009410 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009411 } else {
9412 switch (BOK) {
9413 case BO_Add:
9414 case BO_Xor:
9415 case BO_Or:
9416 case BO_LOr:
9417 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9418 if (Type->isScalarType() || Type->isAnyComplexType())
9419 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9420 break;
9421 case BO_Mul:
9422 case BO_LAnd:
9423 if (Type->isScalarType() || Type->isAnyComplexType()) {
9424 // '*' and '&&' reduction ops - initializer is '1'.
9425 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009426 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009427 break;
9428 case BO_And: {
9429 // '&' reduction op - initializer is '~0'.
9430 QualType OrigType = Type;
9431 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9432 Type = ComplexTy->getElementType();
9433 if (Type->isRealFloatingType()) {
9434 llvm::APFloat InitValue =
9435 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9436 /*isIEEE=*/true);
9437 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9438 Type, ELoc);
9439 } else if (Type->isScalarType()) {
9440 auto Size = Context.getTypeSize(Type);
9441 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9442 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9443 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9444 }
9445 if (Init && OrigType->isAnyComplexType()) {
9446 // Init = 0xFFFF + 0xFFFFi;
9447 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9448 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9449 }
9450 Type = OrigType;
9451 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009452 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009453 case BO_LT:
9454 case BO_GT: {
9455 // 'min' reduction op - initializer is 'Largest representable number in
9456 // the reduction list item type'.
9457 // 'max' reduction op - initializer is 'Least representable number in
9458 // the reduction list item type'.
9459 if (Type->isIntegerType() || Type->isPointerType()) {
9460 bool IsSigned = Type->hasSignedIntegerRepresentation();
9461 auto Size = Context.getTypeSize(Type);
9462 QualType IntTy =
9463 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9464 llvm::APInt InitValue =
9465 (BOK != BO_LT)
9466 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9467 : llvm::APInt::getMinValue(Size)
9468 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9469 : llvm::APInt::getMaxValue(Size);
9470 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9471 if (Type->isPointerType()) {
9472 // Cast to pointer type.
9473 auto CastExpr = BuildCStyleCastExpr(
9474 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9475 SourceLocation(), Init);
9476 if (CastExpr.isInvalid())
9477 continue;
9478 Init = CastExpr.get();
9479 }
9480 } else if (Type->isRealFloatingType()) {
9481 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9482 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9483 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9484 Type, ELoc);
9485 }
9486 break;
9487 }
9488 case BO_PtrMemD:
9489 case BO_PtrMemI:
9490 case BO_MulAssign:
9491 case BO_Div:
9492 case BO_Rem:
9493 case BO_Sub:
9494 case BO_Shl:
9495 case BO_Shr:
9496 case BO_LE:
9497 case BO_GE:
9498 case BO_EQ:
9499 case BO_NE:
9500 case BO_AndAssign:
9501 case BO_XorAssign:
9502 case BO_OrAssign:
9503 case BO_Assign:
9504 case BO_AddAssign:
9505 case BO_SubAssign:
9506 case BO_DivAssign:
9507 case BO_RemAssign:
9508 case BO_ShlAssign:
9509 case BO_ShrAssign:
9510 case BO_Comma:
9511 llvm_unreachable("Unexpected reduction operation");
9512 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009513 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009514 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009515 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9516 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009517 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009518 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009519 if (RHSVD->isInvalidDecl())
9520 continue;
9521 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009522 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9523 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009524 bool IsDecl =
9525 !VD ||
9526 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9527 Diag(D->getLocation(),
9528 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9529 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009530 continue;
9531 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009532 // Store initializer for single element in private copy. Will be used during
9533 // codegen.
9534 PrivateVD->setInit(RHSVD->getInit());
9535 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009536 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009537 ExprResult ReductionOp;
9538 if (DeclareReductionRef.isUsable()) {
9539 QualType RedTy = DeclareReductionRef.get()->getType();
9540 QualType PtrRedTy = Context.getPointerType(RedTy);
9541 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9542 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9543 if (!BasePath.empty()) {
9544 LHS = DefaultLvalueConversion(LHS.get());
9545 RHS = DefaultLvalueConversion(RHS.get());
9546 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9547 CK_UncheckedDerivedToBase, LHS.get(),
9548 &BasePath, LHS.get()->getValueKind());
9549 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9550 CK_UncheckedDerivedToBase, RHS.get(),
9551 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009552 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009553 FunctionProtoType::ExtProtoInfo EPI;
9554 QualType Params[] = {PtrRedTy, PtrRedTy};
9555 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9556 auto *OVE = new (Context) OpaqueValueExpr(
9557 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9558 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9559 Expr *Args[] = {LHS.get(), RHS.get()};
9560 ReductionOp = new (Context)
9561 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9562 } else {
9563 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9564 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9565 if (ReductionOp.isUsable()) {
9566 if (BOK != BO_LT && BOK != BO_GT) {
9567 ReductionOp =
9568 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9569 BO_Assign, LHSDRE, ReductionOp.get());
9570 } else {
9571 auto *ConditionalOp = new (Context) ConditionalOperator(
9572 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9573 RHSDRE, Type, VK_LValue, OK_Ordinary);
9574 ReductionOp =
9575 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9576 BO_Assign, LHSDRE, ConditionalOp);
9577 }
9578 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9579 }
9580 if (ReductionOp.isInvalid())
9581 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009582 }
9583
Alexey Bataev60da77e2016-02-29 05:54:20 +00009584 DeclRefExpr *Ref = nullptr;
9585 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009586 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009587 if (ASE || OASE) {
9588 TransformExprToCaptures RebuildToCapture(*this, D);
9589 VarsExpr =
9590 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9591 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009592 } else {
9593 VarsExpr = Ref =
9594 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009595 }
9596 if (!IsOpenMPCapturedDecl(D)) {
9597 ExprCaptures.push_back(Ref->getDecl());
9598 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9599 ExprResult RefRes = DefaultLvalueConversion(Ref);
9600 if (!RefRes.isUsable())
9601 continue;
9602 ExprResult PostUpdateRes =
9603 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9604 SimpleRefExpr, RefRes.get());
9605 if (!PostUpdateRes.isUsable())
9606 continue;
9607 ExprPostUpdates.push_back(
9608 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009609 }
9610 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009611 }
9612 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9613 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009614 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009615 LHSs.push_back(LHSDRE);
9616 RHSs.push_back(RHSDRE);
9617 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009618 }
9619
9620 if (Vars.empty())
9621 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009622
Alexey Bataevc5e02582014-06-16 07:08:35 +00009623 return OMPReductionClause::Create(
9624 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009625 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009626 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9627 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009628}
9629
Alexey Bataevecba70f2016-04-12 11:02:11 +00009630bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9631 SourceLocation LinLoc) {
9632 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9633 LinKind == OMPC_LINEAR_unknown) {
9634 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9635 return true;
9636 }
9637 return false;
9638}
9639
9640bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9641 OpenMPLinearClauseKind LinKind,
9642 QualType Type) {
9643 auto *VD = dyn_cast_or_null<VarDecl>(D);
9644 // A variable must not have an incomplete type or a reference type.
9645 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9646 return true;
9647 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9648 !Type->isReferenceType()) {
9649 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9650 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9651 return true;
9652 }
9653 Type = Type.getNonReferenceType();
9654
9655 // A list item must not be const-qualified.
9656 if (Type.isConstant(Context)) {
9657 Diag(ELoc, diag::err_omp_const_variable)
9658 << getOpenMPClauseName(OMPC_linear);
9659 if (D) {
9660 bool IsDecl =
9661 !VD ||
9662 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9663 Diag(D->getLocation(),
9664 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9665 << D;
9666 }
9667 return true;
9668 }
9669
9670 // A list item must be of integral or pointer type.
9671 Type = Type.getUnqualifiedType().getCanonicalType();
9672 const auto *Ty = Type.getTypePtrOrNull();
9673 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9674 !Ty->isPointerType())) {
9675 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9676 if (D) {
9677 bool IsDecl =
9678 !VD ||
9679 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9680 Diag(D->getLocation(),
9681 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9682 << D;
9683 }
9684 return true;
9685 }
9686 return false;
9687}
9688
Alexey Bataev182227b2015-08-20 10:54:39 +00009689OMPClause *Sema::ActOnOpenMPLinearClause(
9690 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9691 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9692 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009693 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009694 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009695 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009696 SmallVector<Decl *, 4> ExprCaptures;
9697 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009698 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009699 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009700 for (auto &RefExpr : VarList) {
9701 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009702 SourceLocation ELoc;
9703 SourceRange ERange;
9704 Expr *SimpleRefExpr = RefExpr;
9705 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9706 /*AllowArraySection=*/false);
9707 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009708 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009709 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009710 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009711 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009712 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009713 ValueDecl *D = Res.first;
9714 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009715 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009716
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009717 QualType Type = D->getType();
9718 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009719
9720 // OpenMP [2.14.3.7, linear clause]
9721 // A list-item cannot appear in more than one linear clause.
9722 // A list-item that appears in a linear clause cannot appear in any
9723 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009724 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009725 if (DVar.RefExpr) {
9726 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9727 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009728 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009729 continue;
9730 }
9731
Alexey Bataevecba70f2016-04-12 11:02:11 +00009732 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009733 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009734 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009735
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009736 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009737 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9738 D->hasAttrs() ? &D->getAttrs() : nullptr);
9739 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009740 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009741 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009742 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009743 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009744 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009745 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9746 if (!IsOpenMPCapturedDecl(D)) {
9747 ExprCaptures.push_back(Ref->getDecl());
9748 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9749 ExprResult RefRes = DefaultLvalueConversion(Ref);
9750 if (!RefRes.isUsable())
9751 continue;
9752 ExprResult PostUpdateRes =
9753 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9754 SimpleRefExpr, RefRes.get());
9755 if (!PostUpdateRes.isUsable())
9756 continue;
9757 ExprPostUpdates.push_back(
9758 IgnoredValueConversions(PostUpdateRes.get()).get());
9759 }
9760 }
9761 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009762 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009763 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009764 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009765 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009766 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009767 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9768 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9769
9770 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009771 Vars.push_back((VD || CurContext->isDependentContext())
9772 ? RefExpr->IgnoreParens()
9773 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009774 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009775 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009776 }
9777
9778 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009779 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009780
9781 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009782 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009783 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9784 !Step->isInstantiationDependent() &&
9785 !Step->containsUnexpandedParameterPack()) {
9786 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009787 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009788 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009789 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009790 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009791
Alexander Musman3276a272015-03-21 10:12:56 +00009792 // Build var to save the step value.
9793 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009794 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009795 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009796 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009797 ExprResult CalcStep =
9798 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009799 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009800
Alexander Musman8dba6642014-04-22 13:09:42 +00009801 // Warn about zero linear step (it would be probably better specified as
9802 // making corresponding variables 'const').
9803 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009804 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9805 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009806 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9807 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009808 if (!IsConstant && CalcStep.isUsable()) {
9809 // Calculate the step beforehand instead of doing this on each iteration.
9810 // (This is not used if the number of iterations may be kfold-ed).
9811 CalcStepExpr = CalcStep.get();
9812 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009813 }
9814
Alexey Bataev182227b2015-08-20 10:54:39 +00009815 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9816 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009817 StepExpr, CalcStepExpr,
9818 buildPreInits(Context, ExprCaptures),
9819 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009820}
9821
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009822static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9823 Expr *NumIterations, Sema &SemaRef,
9824 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009825 // Walk the vars and build update/final expressions for the CodeGen.
9826 SmallVector<Expr *, 8> Updates;
9827 SmallVector<Expr *, 8> Finals;
9828 Expr *Step = Clause.getStep();
9829 Expr *CalcStep = Clause.getCalcStep();
9830 // OpenMP [2.14.3.7, linear clause]
9831 // If linear-step is not specified it is assumed to be 1.
9832 if (Step == nullptr)
9833 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009834 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009835 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009836 }
Alexander Musman3276a272015-03-21 10:12:56 +00009837 bool HasErrors = false;
9838 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009839 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009840 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009841 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009842 SourceLocation ELoc;
9843 SourceRange ERange;
9844 Expr *SimpleRefExpr = RefExpr;
9845 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9846 /*AllowArraySection=*/false);
9847 ValueDecl *D = Res.first;
9848 if (Res.second || !D) {
9849 Updates.push_back(nullptr);
9850 Finals.push_back(nullptr);
9851 HasErrors = true;
9852 continue;
9853 }
9854 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9855 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9856 ->getMemberDecl();
9857 }
9858 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009859 Expr *InitExpr = *CurInit;
9860
9861 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009862 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009863 Expr *CapturedRef;
9864 if (LinKind == OMPC_LINEAR_uval)
9865 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9866 else
9867 CapturedRef =
9868 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9869 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9870 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009871
9872 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009873 ExprResult Update;
9874 if (!Info.first) {
9875 Update =
9876 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9877 InitExpr, IV, Step, /* Subtract */ false);
9878 } else
9879 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009880 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9881 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009882
9883 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009884 ExprResult Final;
9885 if (!Info.first) {
9886 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9887 InitExpr, NumIterations, Step,
9888 /* Subtract */ false);
9889 } else
9890 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009891 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9892 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009893
Alexander Musman3276a272015-03-21 10:12:56 +00009894 if (!Update.isUsable() || !Final.isUsable()) {
9895 Updates.push_back(nullptr);
9896 Finals.push_back(nullptr);
9897 HasErrors = true;
9898 } else {
9899 Updates.push_back(Update.get());
9900 Finals.push_back(Final.get());
9901 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009902 ++CurInit;
9903 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009904 }
9905 Clause.setUpdates(Updates);
9906 Clause.setFinals(Finals);
9907 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009908}
9909
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009910OMPClause *Sema::ActOnOpenMPAlignedClause(
9911 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9912 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9913
9914 SmallVector<Expr *, 8> Vars;
9915 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009916 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9917 SourceLocation ELoc;
9918 SourceRange ERange;
9919 Expr *SimpleRefExpr = RefExpr;
9920 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9921 /*AllowArraySection=*/false);
9922 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009923 // It will be analyzed later.
9924 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009925 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009926 ValueDecl *D = Res.first;
9927 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009928 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009929
Alexey Bataev1efd1662016-03-29 10:59:56 +00009930 QualType QType = D->getType();
9931 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009932
9933 // OpenMP [2.8.1, simd construct, Restrictions]
9934 // The type of list items appearing in the aligned clause must be
9935 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009936 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009937 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009938 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009939 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009940 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009941 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009942 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009943 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009944 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009945 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009946 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009947 continue;
9948 }
9949
9950 // OpenMP [2.8.1, simd construct, Restrictions]
9951 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009952 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009953 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009954 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9955 << getOpenMPClauseName(OMPC_aligned);
9956 continue;
9957 }
9958
Alexey Bataev1efd1662016-03-29 10:59:56 +00009959 DeclRefExpr *Ref = nullptr;
9960 if (!VD && IsOpenMPCapturedDecl(D))
9961 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9962 Vars.push_back(DefaultFunctionArrayConversion(
9963 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9964 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009965 }
9966
9967 // OpenMP [2.8.1, simd construct, Description]
9968 // The parameter of the aligned clause, alignment, must be a constant
9969 // positive integer expression.
9970 // If no optional parameter is specified, implementation-defined default
9971 // alignments for SIMD instructions on the target platforms are assumed.
9972 if (Alignment != nullptr) {
9973 ExprResult AlignResult =
9974 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9975 if (AlignResult.isInvalid())
9976 return nullptr;
9977 Alignment = AlignResult.get();
9978 }
9979 if (Vars.empty())
9980 return nullptr;
9981
9982 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9983 EndLoc, Vars, Alignment);
9984}
9985
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009986OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9987 SourceLocation StartLoc,
9988 SourceLocation LParenLoc,
9989 SourceLocation EndLoc) {
9990 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009991 SmallVector<Expr *, 8> SrcExprs;
9992 SmallVector<Expr *, 8> DstExprs;
9993 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009994 for (auto &RefExpr : VarList) {
9995 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9996 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009997 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009998 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009999 SrcExprs.push_back(nullptr);
10000 DstExprs.push_back(nullptr);
10001 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010002 continue;
10003 }
10004
Alexey Bataeved09d242014-05-28 05:53:51 +000010005 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010006 // OpenMP [2.1, C/C++]
10007 // A list item is a variable name.
10008 // OpenMP [2.14.4.1, Restrictions, p.1]
10009 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +000010010 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010011 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010012 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
10013 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010014 continue;
10015 }
10016
10017 Decl *D = DE->getDecl();
10018 VarDecl *VD = cast<VarDecl>(D);
10019
10020 QualType Type = VD->getType();
10021 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
10022 // It will be analyzed later.
10023 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010024 SrcExprs.push_back(nullptr);
10025 DstExprs.push_back(nullptr);
10026 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010027 continue;
10028 }
10029
10030 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
10031 // A list item that appears in a copyin clause must be threadprivate.
10032 if (!DSAStack->isThreadPrivate(VD)) {
10033 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000010034 << getOpenMPClauseName(OMPC_copyin)
10035 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010036 continue;
10037 }
10038
10039 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10040 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000010041 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010042 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010043 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010044 auto *SrcVD =
10045 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
10046 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +000010047 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010048 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
10049 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010050 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
10051 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010052 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010053 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010054 // For arrays generate assignment operation for single element and replace
10055 // it by the original array element in CodeGen.
10056 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
10057 PseudoDstExpr, PseudoSrcExpr);
10058 if (AssignmentOp.isInvalid())
10059 continue;
10060 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
10061 /*DiscardedValue=*/true);
10062 if (AssignmentOp.isInvalid())
10063 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010064
10065 DSAStack->addDSA(VD, DE, OMPC_copyin);
10066 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010067 SrcExprs.push_back(PseudoSrcExpr);
10068 DstExprs.push_back(PseudoDstExpr);
10069 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010070 }
10071
Alexey Bataeved09d242014-05-28 05:53:51 +000010072 if (Vars.empty())
10073 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010074
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010075 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10076 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010077}
10078
Alexey Bataevbae9a792014-06-27 10:37:06 +000010079OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
10080 SourceLocation StartLoc,
10081 SourceLocation LParenLoc,
10082 SourceLocation EndLoc) {
10083 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000010084 SmallVector<Expr *, 8> SrcExprs;
10085 SmallVector<Expr *, 8> DstExprs;
10086 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010087 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010088 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10089 SourceLocation ELoc;
10090 SourceRange ERange;
10091 Expr *SimpleRefExpr = RefExpr;
10092 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10093 /*AllowArraySection=*/false);
10094 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010095 // It will be analyzed later.
10096 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010097 SrcExprs.push_back(nullptr);
10098 DstExprs.push_back(nullptr);
10099 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010100 }
Alexey Bataeve122da12016-03-17 10:50:17 +000010101 ValueDecl *D = Res.first;
10102 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000010103 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010104
Alexey Bataeve122da12016-03-17 10:50:17 +000010105 QualType Type = D->getType();
10106 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010107
10108 // OpenMP [2.14.4.2, Restrictions, p.2]
10109 // A list item that appears in a copyprivate clause may not appear in a
10110 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000010111 if (!VD || !DSAStack->isThreadPrivate(VD)) {
10112 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010113 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10114 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010115 Diag(ELoc, diag::err_omp_wrong_dsa)
10116 << getOpenMPClauseName(DVar.CKind)
10117 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +000010118 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010119 continue;
10120 }
10121
10122 // OpenMP [2.11.4.2, Restrictions, p.1]
10123 // All list items that appear in a copyprivate clause must be either
10124 // threadprivate or private in the enclosing context.
10125 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010126 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010127 if (DVar.CKind == OMPC_shared) {
10128 Diag(ELoc, diag::err_omp_required_access)
10129 << getOpenMPClauseName(OMPC_copyprivate)
10130 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000010131 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010132 continue;
10133 }
10134 }
10135 }
10136
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010137 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010138 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010139 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010140 << getOpenMPClauseName(OMPC_copyprivate) << Type
10141 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010142 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000010143 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010144 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010145 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010146 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010147 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010148 continue;
10149 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010150
Alexey Bataevbae9a792014-06-27 10:37:06 +000010151 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10152 // A variable of class type (or array thereof) that appears in a
10153 // copyin clause requires an accessible, unambiguous copy assignment
10154 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010155 Type = Context.getBaseElementType(Type.getNonReferenceType())
10156 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010157 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010158 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10159 D->hasAttrs() ? &D->getAttrs() : nullptr);
10160 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010161 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010162 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10163 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010164 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010165 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10166 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010167 PseudoDstExpr, PseudoSrcExpr);
10168 if (AssignmentOp.isInvalid())
10169 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010170 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010171 /*DiscardedValue=*/true);
10172 if (AssignmentOp.isInvalid())
10173 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010174
10175 // No need to mark vars as copyprivate, they are already threadprivate or
10176 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010177 assert(VD || IsOpenMPCapturedDecl(D));
10178 Vars.push_back(
10179 VD ? RefExpr->IgnoreParens()
10180 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010181 SrcExprs.push_back(PseudoSrcExpr);
10182 DstExprs.push_back(PseudoDstExpr);
10183 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010184 }
10185
10186 if (Vars.empty())
10187 return nullptr;
10188
Alexey Bataeva63048e2015-03-23 06:18:07 +000010189 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10190 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010191}
10192
Alexey Bataev6125da92014-07-21 11:26:11 +000010193OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10194 SourceLocation StartLoc,
10195 SourceLocation LParenLoc,
10196 SourceLocation EndLoc) {
10197 if (VarList.empty())
10198 return nullptr;
10199
10200 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10201}
Alexey Bataevdea47612014-07-23 07:46:59 +000010202
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010203OMPClause *
10204Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10205 SourceLocation DepLoc, SourceLocation ColonLoc,
10206 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10207 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010208 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010209 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010210 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010211 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010212 return nullptr;
10213 }
10214 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010215 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10216 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010217 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010218 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010219 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10220 /*Last=*/OMPC_DEPEND_unknown, Except)
10221 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010222 return nullptr;
10223 }
10224 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010225 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010226 llvm::APSInt DepCounter(/*BitWidth=*/32);
10227 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10228 if (DepKind == OMPC_DEPEND_sink) {
10229 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10230 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10231 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010232 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010233 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010234 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10235 DSAStack->getParentOrderedRegionParam()) {
10236 for (auto &RefExpr : VarList) {
10237 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010238 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010239 // It will be analyzed later.
10240 Vars.push_back(RefExpr);
10241 continue;
10242 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010243
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010244 SourceLocation ELoc = RefExpr->getExprLoc();
10245 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10246 if (DepKind == OMPC_DEPEND_sink) {
10247 if (DepCounter >= TotalDepCount) {
10248 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10249 continue;
10250 }
10251 ++DepCounter;
10252 // OpenMP [2.13.9, Summary]
10253 // depend(dependence-type : vec), where dependence-type is:
10254 // 'sink' and where vec is the iteration vector, which has the form:
10255 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10256 // where n is the value specified by the ordered clause in the loop
10257 // directive, xi denotes the loop iteration variable of the i-th nested
10258 // loop associated with the loop directive, and di is a constant
10259 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010260 if (CurContext->isDependentContext()) {
10261 // It will be analyzed later.
10262 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010263 continue;
10264 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010265 SimpleExpr = SimpleExpr->IgnoreImplicit();
10266 OverloadedOperatorKind OOK = OO_None;
10267 SourceLocation OOLoc;
10268 Expr *LHS = SimpleExpr;
10269 Expr *RHS = nullptr;
10270 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10271 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10272 OOLoc = BO->getOperatorLoc();
10273 LHS = BO->getLHS()->IgnoreParenImpCasts();
10274 RHS = BO->getRHS()->IgnoreParenImpCasts();
10275 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10276 OOK = OCE->getOperator();
10277 OOLoc = OCE->getOperatorLoc();
10278 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10279 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10280 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10281 OOK = MCE->getMethodDecl()
10282 ->getNameInfo()
10283 .getName()
10284 .getCXXOverloadedOperator();
10285 OOLoc = MCE->getCallee()->getExprLoc();
10286 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10287 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10288 }
10289 SourceLocation ELoc;
10290 SourceRange ERange;
10291 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10292 /*AllowArraySection=*/false);
10293 if (Res.second) {
10294 // It will be analyzed later.
10295 Vars.push_back(RefExpr);
10296 }
10297 ValueDecl *D = Res.first;
10298 if (!D)
10299 continue;
10300
10301 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10302 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10303 continue;
10304 }
10305 if (RHS) {
10306 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10307 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10308 if (RHSRes.isInvalid())
10309 continue;
10310 }
10311 if (!CurContext->isDependentContext() &&
10312 DSAStack->getParentOrderedRegionParam() &&
10313 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10314 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10315 << DSAStack->getParentLoopControlVariable(
10316 DepCounter.getZExtValue());
10317 continue;
10318 }
10319 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010320 } else {
10321 // OpenMP [2.11.1.1, Restrictions, p.3]
10322 // A variable that is part of another variable (such as a field of a
10323 // structure) but is not an array element or an array section cannot
10324 // appear in a depend clause.
10325 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10326 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10327 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10328 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10329 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010330 (ASE &&
10331 !ASE->getBase()
10332 ->getType()
10333 .getNonReferenceType()
10334 ->isPointerType() &&
10335 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010336 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10337 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010338 continue;
10339 }
10340 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010341 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10342 }
10343
10344 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10345 TotalDepCount > VarList.size() &&
10346 DSAStack->getParentOrderedRegionParam()) {
10347 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10348 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10349 }
10350 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10351 Vars.empty())
10352 return nullptr;
10353 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010354 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10355 DepKind, DepLoc, ColonLoc, Vars);
10356 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10357 DSAStack->addDoacrossDependClause(C, OpsOffs);
10358 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010359}
Michael Wonge710d542015-08-07 16:16:36 +000010360
10361OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10362 SourceLocation LParenLoc,
10363 SourceLocation EndLoc) {
10364 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010365
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010366 // OpenMP [2.9.1, Restrictions]
10367 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010368 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10369 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010370 return nullptr;
10371
Michael Wonge710d542015-08-07 16:16:36 +000010372 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10373}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010374
10375static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10376 DSAStackTy *Stack, CXXRecordDecl *RD) {
10377 if (!RD || RD->isInvalidDecl())
10378 return true;
10379
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010380 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10381 if (auto *CTD = CTSD->getSpecializedTemplate())
10382 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010383 auto QTy = SemaRef.Context.getRecordType(RD);
10384 if (RD->isDynamicClass()) {
10385 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10386 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10387 return false;
10388 }
10389 auto *DC = RD;
10390 bool IsCorrect = true;
10391 for (auto *I : DC->decls()) {
10392 if (I) {
10393 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10394 if (MD->isStatic()) {
10395 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10396 SemaRef.Diag(MD->getLocation(),
10397 diag::note_omp_static_member_in_target);
10398 IsCorrect = false;
10399 }
10400 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10401 if (VD->isStaticDataMember()) {
10402 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10403 SemaRef.Diag(VD->getLocation(),
10404 diag::note_omp_static_member_in_target);
10405 IsCorrect = false;
10406 }
10407 }
10408 }
10409 }
10410
10411 for (auto &I : RD->bases()) {
10412 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10413 I.getType()->getAsCXXRecordDecl()))
10414 IsCorrect = false;
10415 }
10416 return IsCorrect;
10417}
10418
10419static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10420 DSAStackTy *Stack, QualType QTy) {
10421 NamedDecl *ND;
10422 if (QTy->isIncompleteType(&ND)) {
10423 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10424 return false;
10425 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10426 if (!RD->isInvalidDecl() &&
10427 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10428 return false;
10429 }
10430 return true;
10431}
10432
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010433/// \brief Return true if it can be proven that the provided array expression
10434/// (array section or array subscript) does NOT specify the whole size of the
10435/// array whose base type is \a BaseQTy.
10436static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10437 const Expr *E,
10438 QualType BaseQTy) {
10439 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10440
10441 // If this is an array subscript, it refers to the whole size if the size of
10442 // the dimension is constant and equals 1. Also, an array section assumes the
10443 // format of an array subscript if no colon is used.
10444 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10445 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10446 return ATy->getSize().getSExtValue() != 1;
10447 // Size can't be evaluated statically.
10448 return false;
10449 }
10450
10451 assert(OASE && "Expecting array section if not an array subscript.");
10452 auto *LowerBound = OASE->getLowerBound();
10453 auto *Length = OASE->getLength();
10454
10455 // If there is a lower bound that does not evaluates to zero, we are not
10456 // convering the whole dimension.
10457 if (LowerBound) {
10458 llvm::APSInt ConstLowerBound;
10459 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10460 return false; // Can't get the integer value as a constant.
10461 if (ConstLowerBound.getSExtValue())
10462 return true;
10463 }
10464
10465 // If we don't have a length we covering the whole dimension.
10466 if (!Length)
10467 return false;
10468
10469 // If the base is a pointer, we don't have a way to get the size of the
10470 // pointee.
10471 if (BaseQTy->isPointerType())
10472 return false;
10473
10474 // We can only check if the length is the same as the size of the dimension
10475 // if we have a constant array.
10476 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10477 if (!CATy)
10478 return false;
10479
10480 llvm::APSInt ConstLength;
10481 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10482 return false; // Can't get the integer value as a constant.
10483
10484 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10485}
10486
10487// Return true if it can be proven that the provided array expression (array
10488// section or array subscript) does NOT specify a single element of the array
10489// whose base type is \a BaseQTy.
10490static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10491 const Expr *E,
10492 QualType BaseQTy) {
10493 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10494
10495 // An array subscript always refer to a single element. Also, an array section
10496 // assumes the format of an array subscript if no colon is used.
10497 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10498 return false;
10499
10500 assert(OASE && "Expecting array section if not an array subscript.");
10501 auto *Length = OASE->getLength();
10502
10503 // If we don't have a length we have to check if the array has unitary size
10504 // for this dimension. Also, we should always expect a length if the base type
10505 // is pointer.
10506 if (!Length) {
10507 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10508 return ATy->getSize().getSExtValue() != 1;
10509 // We cannot assume anything.
10510 return false;
10511 }
10512
10513 // Check if the length evaluates to 1.
10514 llvm::APSInt ConstLength;
10515 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10516 return false; // Can't get the integer value as a constant.
10517
10518 return ConstLength.getSExtValue() != 1;
10519}
10520
Samuel Antao661c0902016-05-26 17:39:58 +000010521// Return the expression of the base of the mappable expression or null if it
10522// cannot be determined and do all the necessary checks to see if the expression
10523// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010524// components of the expression.
10525static Expr *CheckMapClauseExpressionBase(
10526 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010527 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10528 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010529 SourceLocation ELoc = E->getExprLoc();
10530 SourceRange ERange = E->getSourceRange();
10531
10532 // The base of elements of list in a map clause have to be either:
10533 // - a reference to variable or field.
10534 // - a member expression.
10535 // - an array expression.
10536 //
10537 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10538 // reference to 'r'.
10539 //
10540 // If we have:
10541 //
10542 // struct SS {
10543 // Bla S;
10544 // foo() {
10545 // #pragma omp target map (S.Arr[:12]);
10546 // }
10547 // }
10548 //
10549 // We want to retrieve the member expression 'this->S';
10550
10551 Expr *RelevantExpr = nullptr;
10552
Samuel Antao5de996e2016-01-22 20:21:36 +000010553 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10554 // If a list item is an array section, it must specify contiguous storage.
10555 //
10556 // For this restriction it is sufficient that we make sure only references
10557 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010558 // exist except in the rightmost expression (unless they cover the whole
10559 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010560 //
10561 // r.ArrS[3:5].Arr[6:7]
10562 //
10563 // r.ArrS[3:5].x
10564 //
10565 // but these would be valid:
10566 // r.ArrS[3].Arr[6:7]
10567 //
10568 // r.ArrS[3].x
10569
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010570 bool AllowUnitySizeArraySection = true;
10571 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010572
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010573 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010574 E = E->IgnoreParenImpCasts();
10575
10576 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10577 if (!isa<VarDecl>(CurE->getDecl()))
10578 break;
10579
10580 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010581
10582 // If we got a reference to a declaration, we should not expect any array
10583 // section before that.
10584 AllowUnitySizeArraySection = false;
10585 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010586
10587 // Record the component.
10588 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10589 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010590 continue;
10591 }
10592
10593 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10594 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10595
10596 if (isa<CXXThisExpr>(BaseE))
10597 // We found a base expression: this->Val.
10598 RelevantExpr = CurE;
10599 else
10600 E = BaseE;
10601
10602 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10603 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10604 << CurE->getSourceRange();
10605 break;
10606 }
10607
10608 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10609
10610 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10611 // A bit-field cannot appear in a map clause.
10612 //
10613 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010614 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10615 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010616 break;
10617 }
10618
10619 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10620 // If the type of a list item is a reference to a type T then the type
10621 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010622 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010623
10624 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10625 // A list item cannot be a variable that is a member of a structure with
10626 // a union type.
10627 //
10628 if (auto *RT = CurType->getAs<RecordType>())
10629 if (RT->isUnionType()) {
10630 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10631 << CurE->getSourceRange();
10632 break;
10633 }
10634
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010635 // If we got a member expression, we should not expect any array section
10636 // before that:
10637 //
10638 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10639 // If a list item is an element of a structure, only the rightmost symbol
10640 // of the variable reference can be an array section.
10641 //
10642 AllowUnitySizeArraySection = false;
10643 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010644
10645 // Record the component.
10646 CurComponents.push_back(
10647 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010648 continue;
10649 }
10650
10651 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10652 E = CurE->getBase()->IgnoreParenImpCasts();
10653
10654 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10655 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10656 << 0 << CurE->getSourceRange();
10657 break;
10658 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010659
10660 // If we got an array subscript that express the whole dimension we
10661 // can have any array expressions before. If it only expressing part of
10662 // the dimension, we can only have unitary-size array expressions.
10663 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10664 E->getType()))
10665 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010666
10667 // Record the component - we don't have any declaration associated.
10668 CurComponents.push_back(
10669 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010670 continue;
10671 }
10672
10673 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010674 E = CurE->getBase()->IgnoreParenImpCasts();
10675
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010676 auto CurType =
10677 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10678
Samuel Antao5de996e2016-01-22 20:21:36 +000010679 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10680 // If the type of a list item is a reference to a type T then the type
10681 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010682 if (CurType->isReferenceType())
10683 CurType = CurType->getPointeeType();
10684
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010685 bool IsPointer = CurType->isAnyPointerType();
10686
10687 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010688 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10689 << 0 << CurE->getSourceRange();
10690 break;
10691 }
10692
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010693 bool NotWhole =
10694 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10695 bool NotUnity =
10696 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10697
Samuel Antaodab51bb2016-07-18 23:22:11 +000010698 if (AllowWholeSizeArraySection) {
10699 // Any array section is currently allowed. Allowing a whole size array
10700 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010701 //
10702 // If this array section refers to the whole dimension we can still
10703 // accept other array sections before this one, except if the base is a
10704 // pointer. Otherwise, only unitary sections are accepted.
10705 if (NotWhole || IsPointer)
10706 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000010707 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010708 // A unity or whole array section is not allowed and that is not
10709 // compatible with the properties of the current array section.
10710 SemaRef.Diag(
10711 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10712 << CurE->getSourceRange();
10713 break;
10714 }
Samuel Antao90927002016-04-26 14:54:23 +000010715
10716 // Record the component - we don't have any declaration associated.
10717 CurComponents.push_back(
10718 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010719 continue;
10720 }
10721
10722 // If nothing else worked, this is not a valid map clause expression.
10723 SemaRef.Diag(ELoc,
10724 diag::err_omp_expected_named_var_member_or_array_expression)
10725 << ERange;
10726 break;
10727 }
10728
10729 return RelevantExpr;
10730}
10731
10732// Return true if expression E associated with value VD has conflicts with other
10733// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010734static bool CheckMapConflicts(
10735 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10736 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010737 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10738 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010739 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010740 SourceLocation ELoc = E->getExprLoc();
10741 SourceRange ERange = E->getSourceRange();
10742
10743 // In order to easily check the conflicts we need to match each component of
10744 // the expression under test with the components of the expressions that are
10745 // already in the stack.
10746
Samuel Antao5de996e2016-01-22 20:21:36 +000010747 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010748 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010749 "Map clause expression with unexpected base!");
10750
10751 // Variables to help detecting enclosing problems in data environment nests.
10752 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010753 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010754
Samuel Antao90927002016-04-26 14:54:23 +000010755 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10756 VD, CurrentRegionOnly,
10757 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10758 StackComponents) -> bool {
10759
Samuel Antao5de996e2016-01-22 20:21:36 +000010760 assert(!StackComponents.empty() &&
10761 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010762 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010763 "Map clause expression with unexpected base!");
10764
Samuel Antao90927002016-04-26 14:54:23 +000010765 // The whole expression in the stack.
10766 auto *RE = StackComponents.front().getAssociatedExpression();
10767
Samuel Antao5de996e2016-01-22 20:21:36 +000010768 // Expressions must start from the same base. Here we detect at which
10769 // point both expressions diverge from each other and see if we can
10770 // detect if the memory referred to both expressions is contiguous and
10771 // do not overlap.
10772 auto CI = CurComponents.rbegin();
10773 auto CE = CurComponents.rend();
10774 auto SI = StackComponents.rbegin();
10775 auto SE = StackComponents.rend();
10776 for (; CI != CE && SI != SE; ++CI, ++SI) {
10777
10778 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10779 // At most one list item can be an array item derived from a given
10780 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010781 if (CurrentRegionOnly &&
10782 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10783 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10784 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10785 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10786 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010787 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010788 << CI->getAssociatedExpression()->getSourceRange();
10789 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10790 diag::note_used_here)
10791 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010792 return true;
10793 }
10794
10795 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010796 if (CI->getAssociatedExpression()->getStmtClass() !=
10797 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010798 break;
10799
10800 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010801 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010802 break;
10803 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000010804 // Check if the extra components of the expressions in the enclosing
10805 // data environment are redundant for the current base declaration.
10806 // If they are, the maps completely overlap, which is legal.
10807 for (; SI != SE; ++SI) {
10808 QualType Type;
10809 if (auto *ASE =
10810 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
10811 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
10812 } else if (auto *OASE =
10813 dyn_cast<OMPArraySectionExpr>(SI->getAssociatedExpression())) {
10814 auto *E = OASE->getBase()->IgnoreParenImpCasts();
10815 Type =
10816 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10817 }
10818 if (Type.isNull() || Type->isAnyPointerType() ||
10819 CheckArrayExpressionDoesNotReferToWholeSize(
10820 SemaRef, SI->getAssociatedExpression(), Type))
10821 break;
10822 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010823
10824 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10825 // List items of map clauses in the same construct must not share
10826 // original storage.
10827 //
10828 // If the expressions are exactly the same or one is a subset of the
10829 // other, it means they are sharing storage.
10830 if (CI == CE && SI == SE) {
10831 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010832 if (CKind == OMPC_map)
10833 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10834 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010835 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010836 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10837 << ERange;
10838 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010839 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10840 << RE->getSourceRange();
10841 return true;
10842 } else {
10843 // If we find the same expression in the enclosing data environment,
10844 // that is legal.
10845 IsEnclosedByDataEnvironmentExpr = true;
10846 return false;
10847 }
10848 }
10849
Samuel Antao90927002016-04-26 14:54:23 +000010850 QualType DerivedType =
10851 std::prev(CI)->getAssociatedDeclaration()->getType();
10852 SourceLocation DerivedLoc =
10853 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010854
10855 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10856 // If the type of a list item is a reference to a type T then the type
10857 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010858 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010859
10860 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10861 // A variable for which the type is pointer and an array section
10862 // derived from that variable must not appear as list items of map
10863 // clauses of the same construct.
10864 //
10865 // Also, cover one of the cases in:
10866 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10867 // If any part of the original storage of a list item has corresponding
10868 // storage in the device data environment, all of the original storage
10869 // must have corresponding storage in the device data environment.
10870 //
10871 if (DerivedType->isAnyPointerType()) {
10872 if (CI == CE || SI == SE) {
10873 SemaRef.Diag(
10874 DerivedLoc,
10875 diag::err_omp_pointer_mapped_along_with_derived_section)
10876 << DerivedLoc;
10877 } else {
10878 assert(CI != CE && SI != SE);
10879 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10880 << DerivedLoc;
10881 }
10882 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10883 << RE->getSourceRange();
10884 return true;
10885 }
10886
10887 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10888 // List items of map clauses in the same construct must not share
10889 // original storage.
10890 //
10891 // An expression is a subset of the other.
10892 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010893 if (CKind == OMPC_map)
10894 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10895 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010896 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010897 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10898 << ERange;
10899 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010900 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10901 << RE->getSourceRange();
10902 return true;
10903 }
10904
10905 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010906 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010907 if (!CurrentRegionOnly && SI != SE)
10908 EnclosingExpr = RE;
10909
10910 // The current expression is a subset of the expression in the data
10911 // environment.
10912 IsEnclosedByDataEnvironmentExpr |=
10913 (!CurrentRegionOnly && CI != CE && SI == SE);
10914
10915 return false;
10916 });
10917
10918 if (CurrentRegionOnly)
10919 return FoundError;
10920
10921 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10922 // If any part of the original storage of a list item has corresponding
10923 // storage in the device data environment, all of the original storage must
10924 // have corresponding storage in the device data environment.
10925 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10926 // If a list item is an element of a structure, and a different element of
10927 // the structure has a corresponding list item in the device data environment
10928 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010929 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010930 // data environment prior to the task encountering the construct.
10931 //
10932 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10933 SemaRef.Diag(ELoc,
10934 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10935 << ERange;
10936 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10937 << EnclosingExpr->getSourceRange();
10938 return true;
10939 }
10940
10941 return FoundError;
10942}
10943
Samuel Antao661c0902016-05-26 17:39:58 +000010944namespace {
10945// Utility struct that gathers all the related lists associated with a mappable
10946// expression.
10947struct MappableVarListInfo final {
10948 // The list of expressions.
10949 ArrayRef<Expr *> VarList;
10950 // The list of processed expressions.
10951 SmallVector<Expr *, 16> ProcessedVarList;
10952 // The mappble components for each expression.
10953 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10954 // The base declaration of the variable.
10955 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10956
10957 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10958 // We have a list of components and base declarations for each entry in the
10959 // variable list.
10960 VarComponents.reserve(VarList.size());
10961 VarBaseDeclarations.reserve(VarList.size());
10962 }
10963};
10964}
10965
10966// Check the validity of the provided variable list for the provided clause kind
10967// \a CKind. In the check process the valid expressions, and mappable expression
10968// components and variables are extracted and used to fill \a Vars,
10969// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10970// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10971static void
10972checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10973 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10974 SourceLocation StartLoc,
10975 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10976 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010977 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10978 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010979 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010980
Samuel Antao90927002016-04-26 14:54:23 +000010981 // Keep track of the mappable components and base declarations in this clause.
10982 // Each entry in the list is going to have a list of components associated. We
10983 // record each set of the components so that we can build the clause later on.
10984 // In the end we should have the same amount of declarations and component
10985 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010986
Samuel Antao661c0902016-05-26 17:39:58 +000010987 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010988 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010989 SourceLocation ELoc = RE->getExprLoc();
10990
Kelvin Li0bff7af2015-11-23 05:32:03 +000010991 auto *VE = RE->IgnoreParenLValueCasts();
10992
10993 if (VE->isValueDependent() || VE->isTypeDependent() ||
10994 VE->isInstantiationDependent() ||
10995 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010996 // We can only analyze this information once the missing information is
10997 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000010998 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010999 continue;
11000 }
11001
11002 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011003
Samuel Antao5de996e2016-01-22 20:21:36 +000011004 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000011005 SemaRef.Diag(ELoc,
11006 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000011007 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011008 continue;
11009 }
11010
Samuel Antao90927002016-04-26 14:54:23 +000011011 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
11012 ValueDecl *CurDeclaration = nullptr;
11013
11014 // Obtain the array or member expression bases if required. Also, fill the
11015 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000011016 auto *BE =
11017 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000011018 if (!BE)
11019 continue;
11020
Samuel Antao90927002016-04-26 14:54:23 +000011021 assert(!CurComponents.empty() &&
11022 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011023
Samuel Antao90927002016-04-26 14:54:23 +000011024 // For the following checks, we rely on the base declaration which is
11025 // expected to be associated with the last component. The declaration is
11026 // expected to be a variable or a field (if 'this' is being mapped).
11027 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
11028 assert(CurDeclaration && "Null decl on map clause.");
11029 assert(
11030 CurDeclaration->isCanonicalDecl() &&
11031 "Expecting components to have associated only canonical declarations.");
11032
11033 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
11034 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000011035
11036 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000011037 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000011038
11039 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000011040 // threadprivate variables cannot appear in a map clause.
11041 // OpenMP 4.5 [2.10.5, target update Construct]
11042 // threadprivate variables cannot appear in a from clause.
11043 if (VD && DSAS->isThreadPrivate(VD)) {
11044 auto DVar = DSAS->getTopDSA(VD, false);
11045 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
11046 << getOpenMPClauseName(CKind);
11047 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011048 continue;
11049 }
11050
Samuel Antao5de996e2016-01-22 20:21:36 +000011051 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
11052 // A list item cannot appear in both a map clause and a data-sharing
11053 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000011054
Samuel Antao5de996e2016-01-22 20:21:36 +000011055 // Check conflicts with other map clause expressions. We check the conflicts
11056 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000011057 // environment, because the restrictions are different. We only have to
11058 // check conflicts across regions for the map clauses.
11059 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11060 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011061 break;
Samuel Antao661c0902016-05-26 17:39:58 +000011062 if (CKind == OMPC_map &&
11063 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11064 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011065 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011066
Samuel Antao661c0902016-05-26 17:39:58 +000011067 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000011068 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11069 // If the type of a list item is a reference to a type T then the type will
11070 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011071 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011072
Samuel Antao661c0902016-05-26 17:39:58 +000011073 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
11074 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000011075 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000011076 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000011077 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
11078 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000011079 continue;
11080
Samuel Antao661c0902016-05-26 17:39:58 +000011081 if (CKind == OMPC_map) {
11082 // target enter data
11083 // OpenMP [2.10.2, Restrictions, p. 99]
11084 // A map-type must be specified in all map clauses and must be either
11085 // to or alloc.
11086 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
11087 if (DKind == OMPD_target_enter_data &&
11088 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
11089 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11090 << (IsMapTypeImplicit ? 1 : 0)
11091 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11092 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011093 continue;
11094 }
Samuel Antao661c0902016-05-26 17:39:58 +000011095
11096 // target exit_data
11097 // OpenMP [2.10.3, Restrictions, p. 102]
11098 // A map-type must be specified in all map clauses and must be either
11099 // from, release, or delete.
11100 if (DKind == OMPD_target_exit_data &&
11101 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
11102 MapType == OMPC_MAP_delete)) {
11103 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11104 << (IsMapTypeImplicit ? 1 : 0)
11105 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11106 << getOpenMPDirectiveName(DKind);
11107 continue;
11108 }
11109
11110 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11111 // A list item cannot appear in both a map clause and a data-sharing
11112 // attribute clause on the same construct
11113 if (DKind == OMPD_target && VD) {
11114 auto DVar = DSAS->getTopDSA(VD, false);
11115 if (isOpenMPPrivate(DVar.CKind)) {
11116 SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
11117 << getOpenMPClauseName(DVar.CKind)
11118 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11119 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11120 continue;
11121 }
11122 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011123 }
11124
Samuel Antao90927002016-04-26 14:54:23 +000011125 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000011126 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000011127
11128 // Store the components in the stack so that they can be used to check
11129 // against other clauses later on.
Samuel Antao661c0902016-05-26 17:39:58 +000011130 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
Samuel Antao90927002016-04-26 14:54:23 +000011131
11132 // Save the components and declaration to create the clause. For purposes of
11133 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000011134 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000011135 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11136 MVLI.VarComponents.back().append(CurComponents.begin(),
11137 CurComponents.end());
11138 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11139 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011140 }
Samuel Antao661c0902016-05-26 17:39:58 +000011141}
11142
11143OMPClause *
11144Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11145 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11146 SourceLocation MapLoc, SourceLocation ColonLoc,
11147 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11148 SourceLocation LParenLoc, SourceLocation EndLoc) {
11149 MappableVarListInfo MVLI(VarList);
11150 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11151 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011152
Samuel Antao5de996e2016-01-22 20:21:36 +000011153 // We need to produce a map clause even if we don't have variables so that
11154 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000011155 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11156 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11157 MVLI.VarComponents, MapTypeModifier, MapType,
11158 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011159}
Kelvin Li099bb8c2015-11-24 20:50:12 +000011160
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011161QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11162 TypeResult ParsedType) {
11163 assert(ParsedType.isUsable());
11164
11165 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11166 if (ReductionType.isNull())
11167 return QualType();
11168
11169 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11170 // A type name in a declare reduction directive cannot be a function type, an
11171 // array type, a reference type, or a type qualified with const, volatile or
11172 // restrict.
11173 if (ReductionType.hasQualifiers()) {
11174 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11175 return QualType();
11176 }
11177
11178 if (ReductionType->isFunctionType()) {
11179 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11180 return QualType();
11181 }
11182 if (ReductionType->isReferenceType()) {
11183 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11184 return QualType();
11185 }
11186 if (ReductionType->isArrayType()) {
11187 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11188 return QualType();
11189 }
11190 return ReductionType;
11191}
11192
11193Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11194 Scope *S, DeclContext *DC, DeclarationName Name,
11195 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11196 AccessSpecifier AS, Decl *PrevDeclInScope) {
11197 SmallVector<Decl *, 8> Decls;
11198 Decls.reserve(ReductionTypes.size());
11199
11200 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11201 ForRedeclaration);
11202 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11203 // A reduction-identifier may not be re-declared in the current scope for the
11204 // same type or for a type that is compatible according to the base language
11205 // rules.
11206 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11207 OMPDeclareReductionDecl *PrevDRD = nullptr;
11208 bool InCompoundScope = true;
11209 if (S != nullptr) {
11210 // Find previous declaration with the same name not referenced in other
11211 // declarations.
11212 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11213 InCompoundScope =
11214 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11215 LookupName(Lookup, S);
11216 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11217 /*AllowInlineNamespace=*/false);
11218 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11219 auto Filter = Lookup.makeFilter();
11220 while (Filter.hasNext()) {
11221 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11222 if (InCompoundScope) {
11223 auto I = UsedAsPrevious.find(PrevDecl);
11224 if (I == UsedAsPrevious.end())
11225 UsedAsPrevious[PrevDecl] = false;
11226 if (auto *D = PrevDecl->getPrevDeclInScope())
11227 UsedAsPrevious[D] = true;
11228 }
11229 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11230 PrevDecl->getLocation();
11231 }
11232 Filter.done();
11233 if (InCompoundScope) {
11234 for (auto &PrevData : UsedAsPrevious) {
11235 if (!PrevData.second) {
11236 PrevDRD = PrevData.first;
11237 break;
11238 }
11239 }
11240 }
11241 } else if (PrevDeclInScope != nullptr) {
11242 auto *PrevDRDInScope = PrevDRD =
11243 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11244 do {
11245 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11246 PrevDRDInScope->getLocation();
11247 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11248 } while (PrevDRDInScope != nullptr);
11249 }
11250 for (auto &TyData : ReductionTypes) {
11251 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11252 bool Invalid = false;
11253 if (I != PreviousRedeclTypes.end()) {
11254 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11255 << TyData.first;
11256 Diag(I->second, diag::note_previous_definition);
11257 Invalid = true;
11258 }
11259 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11260 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11261 Name, TyData.first, PrevDRD);
11262 DC->addDecl(DRD);
11263 DRD->setAccess(AS);
11264 Decls.push_back(DRD);
11265 if (Invalid)
11266 DRD->setInvalidDecl();
11267 else
11268 PrevDRD = DRD;
11269 }
11270
11271 return DeclGroupPtrTy::make(
11272 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11273}
11274
11275void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11276 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11277
11278 // Enter new function scope.
11279 PushFunctionScope();
11280 getCurFunction()->setHasBranchProtectedScope();
11281 getCurFunction()->setHasOMPDeclareReductionCombiner();
11282
11283 if (S != nullptr)
11284 PushDeclContext(S, DRD);
11285 else
11286 CurContext = DRD;
11287
11288 PushExpressionEvaluationContext(PotentiallyEvaluated);
11289
11290 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011291 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11292 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11293 // uses semantics of argument handles by value, but it should be passed by
11294 // reference. C lang does not support references, so pass all parameters as
11295 // pointers.
11296 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011297 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011298 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011299 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11300 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11301 // uses semantics of argument handles by value, but it should be passed by
11302 // reference. C lang does not support references, so pass all parameters as
11303 // pointers.
11304 // Create 'T omp_out;' variable.
11305 auto *OmpOutParm =
11306 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11307 if (S != nullptr) {
11308 PushOnScopeChains(OmpInParm, S);
11309 PushOnScopeChains(OmpOutParm, S);
11310 } else {
11311 DRD->addDecl(OmpInParm);
11312 DRD->addDecl(OmpOutParm);
11313 }
11314}
11315
11316void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11317 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11318 DiscardCleanupsInEvaluationContext();
11319 PopExpressionEvaluationContext();
11320
11321 PopDeclContext();
11322 PopFunctionScopeInfo();
11323
11324 if (Combiner != nullptr)
11325 DRD->setCombiner(Combiner);
11326 else
11327 DRD->setInvalidDecl();
11328}
11329
11330void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11331 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11332
11333 // Enter new function scope.
11334 PushFunctionScope();
11335 getCurFunction()->setHasBranchProtectedScope();
11336
11337 if (S != nullptr)
11338 PushDeclContext(S, DRD);
11339 else
11340 CurContext = DRD;
11341
11342 PushExpressionEvaluationContext(PotentiallyEvaluated);
11343
11344 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011345 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11346 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11347 // uses semantics of argument handles by value, but it should be passed by
11348 // reference. C lang does not support references, so pass all parameters as
11349 // pointers.
11350 // Create 'T omp_priv;' variable.
11351 auto *OmpPrivParm =
11352 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011353 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11354 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11355 // uses semantics of argument handles by value, but it should be passed by
11356 // reference. C lang does not support references, so pass all parameters as
11357 // pointers.
11358 // Create 'T omp_orig;' variable.
11359 auto *OmpOrigParm =
11360 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011361 if (S != nullptr) {
11362 PushOnScopeChains(OmpPrivParm, S);
11363 PushOnScopeChains(OmpOrigParm, S);
11364 } else {
11365 DRD->addDecl(OmpPrivParm);
11366 DRD->addDecl(OmpOrigParm);
11367 }
11368}
11369
11370void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11371 Expr *Initializer) {
11372 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11373 DiscardCleanupsInEvaluationContext();
11374 PopExpressionEvaluationContext();
11375
11376 PopDeclContext();
11377 PopFunctionScopeInfo();
11378
11379 if (Initializer != nullptr)
11380 DRD->setInitializer(Initializer);
11381 else
11382 DRD->setInvalidDecl();
11383}
11384
11385Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11386 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11387 for (auto *D : DeclReductions.get()) {
11388 if (IsValid) {
11389 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11390 if (S != nullptr)
11391 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11392 } else
11393 D->setInvalidDecl();
11394 }
11395 return DeclReductions;
11396}
11397
Kelvin Li099bb8c2015-11-24 20:50:12 +000011398OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11399 SourceLocation StartLoc,
11400 SourceLocation LParenLoc,
11401 SourceLocation EndLoc) {
11402 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011403
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011404 // OpenMP [teams Constrcut, Restrictions]
11405 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011406 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11407 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011408 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011409
11410 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11411}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011412
11413OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11414 SourceLocation StartLoc,
11415 SourceLocation LParenLoc,
11416 SourceLocation EndLoc) {
11417 Expr *ValExpr = ThreadLimit;
11418
11419 // OpenMP [teams Constrcut, Restrictions]
11420 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011421 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11422 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011423 return nullptr;
11424
11425 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11426 EndLoc);
11427}
Alexey Bataeva0569352015-12-01 10:17:31 +000011428
11429OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11430 SourceLocation StartLoc,
11431 SourceLocation LParenLoc,
11432 SourceLocation EndLoc) {
11433 Expr *ValExpr = Priority;
11434
11435 // OpenMP [2.9.1, task Constrcut]
11436 // The priority-value is a non-negative numerical scalar expression.
11437 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11438 /*StrictlyPositive=*/false))
11439 return nullptr;
11440
11441 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11442}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011443
11444OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11445 SourceLocation StartLoc,
11446 SourceLocation LParenLoc,
11447 SourceLocation EndLoc) {
11448 Expr *ValExpr = Grainsize;
11449
11450 // OpenMP [2.9.2, taskloop Constrcut]
11451 // The parameter of the grainsize clause must be a positive integer
11452 // expression.
11453 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11454 /*StrictlyPositive=*/true))
11455 return nullptr;
11456
11457 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11458}
Alexey Bataev382967a2015-12-08 12:06:20 +000011459
11460OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11461 SourceLocation StartLoc,
11462 SourceLocation LParenLoc,
11463 SourceLocation EndLoc) {
11464 Expr *ValExpr = NumTasks;
11465
11466 // OpenMP [2.9.2, taskloop Constrcut]
11467 // The parameter of the num_tasks clause must be a positive integer
11468 // expression.
11469 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11470 /*StrictlyPositive=*/true))
11471 return nullptr;
11472
11473 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11474}
11475
Alexey Bataev28c75412015-12-15 08:19:24 +000011476OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11477 SourceLocation LParenLoc,
11478 SourceLocation EndLoc) {
11479 // OpenMP [2.13.2, critical construct, Description]
11480 // ... where hint-expression is an integer constant expression that evaluates
11481 // to a valid lock hint.
11482 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11483 if (HintExpr.isInvalid())
11484 return nullptr;
11485 return new (Context)
11486 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11487}
11488
Carlo Bertollib4adf552016-01-15 18:50:31 +000011489OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11490 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11491 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11492 SourceLocation EndLoc) {
11493 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11494 std::string Values;
11495 Values += "'";
11496 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11497 Values += "'";
11498 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11499 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11500 return nullptr;
11501 }
11502 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011503 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011504 if (ChunkSize) {
11505 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11506 !ChunkSize->isInstantiationDependent() &&
11507 !ChunkSize->containsUnexpandedParameterPack()) {
11508 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11509 ExprResult Val =
11510 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11511 if (Val.isInvalid())
11512 return nullptr;
11513
11514 ValExpr = Val.get();
11515
11516 // OpenMP [2.7.1, Restrictions]
11517 // chunk_size must be a loop invariant integer expression with a positive
11518 // value.
11519 llvm::APSInt Result;
11520 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11521 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11522 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11523 << "dist_schedule" << ChunkSize->getSourceRange();
11524 return nullptr;
11525 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011526 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11527 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011528 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11529 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11530 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011531 }
11532 }
11533 }
11534
11535 return new (Context)
11536 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011537 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011538}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011539
11540OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11541 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11542 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11543 SourceLocation KindLoc, SourceLocation EndLoc) {
11544 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11545 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11546 Kind != OMPC_DEFAULTMAP_scalar) {
11547 std::string Value;
11548 SourceLocation Loc;
11549 Value += "'";
11550 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11551 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11552 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11553 Loc = MLoc;
11554 } else {
11555 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11556 OMPC_DEFAULTMAP_scalar);
11557 Loc = KindLoc;
11558 }
11559 Value += "'";
11560 Diag(Loc, diag::err_omp_unexpected_clause_value)
11561 << Value << getOpenMPClauseName(OMPC_defaultmap);
11562 return nullptr;
11563 }
11564
11565 return new (Context)
11566 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11567}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011568
11569bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11570 DeclContext *CurLexicalContext = getCurLexicalContext();
11571 if (!CurLexicalContext->isFileContext() &&
11572 !CurLexicalContext->isExternCContext() &&
11573 !CurLexicalContext->isExternCXXContext()) {
11574 Diag(Loc, diag::err_omp_region_not_file_context);
11575 return false;
11576 }
11577 if (IsInOpenMPDeclareTargetContext) {
11578 Diag(Loc, diag::err_omp_enclosed_declare_target);
11579 return false;
11580 }
11581
11582 IsInOpenMPDeclareTargetContext = true;
11583 return true;
11584}
11585
11586void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11587 assert(IsInOpenMPDeclareTargetContext &&
11588 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11589
11590 IsInOpenMPDeclareTargetContext = false;
11591}
11592
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011593void
11594Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11595 const DeclarationNameInfo &Id,
11596 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11597 NamedDeclSetType &SameDirectiveDecls) {
11598 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11599 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11600
11601 if (Lookup.isAmbiguous())
11602 return;
11603 Lookup.suppressDiagnostics();
11604
11605 if (!Lookup.isSingleResult()) {
11606 if (TypoCorrection Corrected =
11607 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11608 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11609 CTK_ErrorRecovery)) {
11610 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11611 << Id.getName());
11612 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11613 return;
11614 }
11615
11616 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11617 return;
11618 }
11619
11620 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11621 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11622 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11623 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11624
11625 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11626 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11627 ND->addAttr(A);
11628 if (ASTMutationListener *ML = Context.getASTMutationListener())
11629 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11630 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11631 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11632 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11633 << Id.getName();
11634 }
11635 } else
11636 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11637}
11638
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011639static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11640 Sema &SemaRef, Decl *D) {
11641 if (!D)
11642 return;
11643 Decl *LD = nullptr;
11644 if (isa<TagDecl>(D)) {
11645 LD = cast<TagDecl>(D)->getDefinition();
11646 } else if (isa<VarDecl>(D)) {
11647 LD = cast<VarDecl>(D)->getDefinition();
11648
11649 // If this is an implicit variable that is legal and we do not need to do
11650 // anything.
11651 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011652 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11653 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11654 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011655 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011656 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011657 return;
11658 }
11659
11660 } else if (isa<FunctionDecl>(D)) {
11661 const FunctionDecl *FD = nullptr;
11662 if (cast<FunctionDecl>(D)->hasBody(FD))
11663 LD = const_cast<FunctionDecl *>(FD);
11664
11665 // If the definition is associated with the current declaration in the
11666 // target region (it can be e.g. a lambda) that is legal and we do not need
11667 // to do anything else.
11668 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011669 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11670 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11671 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011672 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011673 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011674 return;
11675 }
11676 }
11677 if (!LD)
11678 LD = D;
11679 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11680 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11681 // Outlined declaration is not declared target.
11682 if (LD->isOutOfLine()) {
11683 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11684 SemaRef.Diag(SL, diag::note_used_here) << SR;
11685 } else {
11686 DeclContext *DC = LD->getDeclContext();
11687 while (DC) {
11688 if (isa<FunctionDecl>(DC) &&
11689 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11690 break;
11691 DC = DC->getParent();
11692 }
11693 if (DC)
11694 return;
11695
11696 // Is not declared in target context.
11697 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11698 SemaRef.Diag(SL, diag::note_used_here) << SR;
11699 }
11700 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011701 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11702 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11703 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011704 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011705 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011706 }
11707}
11708
11709static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11710 Sema &SemaRef, DSAStackTy *Stack,
11711 ValueDecl *VD) {
11712 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11713 return true;
11714 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11715 return false;
11716 return true;
11717}
11718
11719void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11720 if (!D || D->isInvalidDecl())
11721 return;
11722 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11723 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11724 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11725 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11726 if (DSAStack->isThreadPrivate(VD)) {
11727 Diag(SL, diag::err_omp_threadprivate_in_target);
11728 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11729 return;
11730 }
11731 }
11732 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11733 // Problem if any with var declared with incomplete type will be reported
11734 // as normal, so no need to check it here.
11735 if ((E || !VD->getType()->isIncompleteType()) &&
11736 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11737 // Mark decl as declared target to prevent further diagnostic.
11738 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011739 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11740 Context, OMPDeclareTargetDeclAttr::MT_To);
11741 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011742 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011743 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011744 }
11745 return;
11746 }
11747 }
11748 if (!E) {
11749 // Checking declaration inside declare target region.
11750 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11751 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011752 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11753 Context, OMPDeclareTargetDeclAttr::MT_To);
11754 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011755 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011756 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011757 }
11758 return;
11759 }
11760 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11761}
Samuel Antao661c0902016-05-26 17:39:58 +000011762
11763OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11764 SourceLocation StartLoc,
11765 SourceLocation LParenLoc,
11766 SourceLocation EndLoc) {
11767 MappableVarListInfo MVLI(VarList);
11768 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11769 if (MVLI.ProcessedVarList.empty())
11770 return nullptr;
11771
11772 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11773 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11774 MVLI.VarComponents);
11775}
Samuel Antaoec172c62016-05-26 17:49:04 +000011776
11777OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11778 SourceLocation StartLoc,
11779 SourceLocation LParenLoc,
11780 SourceLocation EndLoc) {
11781 MappableVarListInfo MVLI(VarList);
11782 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11783 if (MVLI.ProcessedVarList.empty())
11784 return nullptr;
11785
11786 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11787 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11788 MVLI.VarComponents);
11789}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011790
11791OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11792 SourceLocation StartLoc,
11793 SourceLocation LParenLoc,
11794 SourceLocation EndLoc) {
11795 SmallVector<Expr *, 8> Vars;
11796 for (auto &RefExpr : VarList) {
11797 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11798 SourceLocation ELoc;
11799 SourceRange ERange;
11800 Expr *SimpleRefExpr = RefExpr;
11801 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11802 if (Res.second) {
11803 // It will be analyzed later.
11804 Vars.push_back(RefExpr);
11805 }
11806 ValueDecl *D = Res.first;
11807 if (!D)
11808 continue;
11809
11810 QualType Type = D->getType();
11811 // item should be a pointer or reference to pointer
11812 if (!Type.getNonReferenceType()->isPointerType()) {
11813 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11814 << 0 << RefExpr->getSourceRange();
11815 continue;
11816 }
11817 Vars.push_back(RefExpr->IgnoreParens());
11818 }
11819
11820 if (Vars.empty())
11821 return nullptr;
11822
11823 return OMPUseDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11824 Vars);
11825}
Carlo Bertolli70594e92016-07-13 17:16:49 +000011826
11827OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11828 SourceLocation StartLoc,
11829 SourceLocation LParenLoc,
11830 SourceLocation EndLoc) {
11831 SmallVector<Expr *, 8> Vars;
11832 for (auto &RefExpr : VarList) {
11833 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11834 SourceLocation ELoc;
11835 SourceRange ERange;
11836 Expr *SimpleRefExpr = RefExpr;
11837 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11838 if (Res.second) {
11839 // It will be analyzed later.
11840 Vars.push_back(RefExpr);
11841 }
11842 ValueDecl *D = Res.first;
11843 if (!D)
11844 continue;
11845
11846 QualType Type = D->getType();
11847 // item should be a pointer or array or reference to pointer or array
11848 if (!Type.getNonReferenceType()->isPointerType() &&
11849 !Type.getNonReferenceType()->isArrayType()) {
11850 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11851 << 0 << RefExpr->getSourceRange();
11852 continue;
11853 }
11854 Vars.push_back(RefExpr->IgnoreParens());
11855 }
11856
11857 if (Vars.empty())
11858 return nullptr;
11859
11860 return OMPIsDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11861 Vars);
11862}