blob: 70c775499d2a57400747165e54686290cc9e778d [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000018#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000025#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000027#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000028#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000031#include "clang/Sema/Scope.h"
32#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000033#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000034using namespace clang;
35
Alexey Bataev758e55e2013-09-06 18:03:48 +000036//===----------------------------------------------------------------------===//
37// Stack of data-sharing attributes for variables
38//===----------------------------------------------------------------------===//
39
40namespace {
41/// \brief Default data sharing attributes, which can be applied to directive.
42enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000043 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000046};
Alexey Bataev7ff55242014-06-19 09:13:45 +000047
Alexey Bataev758e55e2013-09-06 18:03:48 +000048/// \brief Stack for tracking declarations used in OpenMP directives and
49/// clauses and their data-sharing attributes.
Alexey Bataev7ace49d2016-05-17 08:55:33 +000050class DSAStackTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000051public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000052 struct DSAVarData final {
53 OpenMPDirectiveKind DKind = OMPD_unknown;
54 OpenMPClauseKind CKind = OMPC_unknown;
55 Expr *RefExpr = nullptr;
56 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000057 SourceLocation ImplicitDSALoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000058 DSAVarData() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000059 };
Alexey Bataev8b427062016-05-25 12:36:08 +000060 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
61 OperatorOffsetTy;
Alexey Bataeved09d242014-05-28 05:53:51 +000062
Alexey Bataev758e55e2013-09-06 18:03:48 +000063private:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000064 struct DSAInfo final {
65 OpenMPClauseKind Attributes = OMPC_unknown;
66 /// Pointer to a reference expression and a flag which shows that the
67 /// variable is marked as lastprivate(true) or not (false).
68 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000070 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000071 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000073 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Samuel Antao90927002016-04-26 14:54:23 +000075 typedef llvm::DenseMap<
76 ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists>
77 MappedExprComponentsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000078 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
79 CriticalsWithHintsTy;
Alexey Bataev8b427062016-05-25 12:36:08 +000080 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
81 DoacrossDependMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000082
Alexey Bataev7ace49d2016-05-17 08:55:33 +000083 struct SharingMapTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000085 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +000086 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000087 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000088 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000090 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000092 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +000094 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
95 /// get the data (loop counters etc.) about enclosing loop-based construct.
96 /// This data is required during codegen.
97 DoacrossDependMapTy DoacrossDepends;
Alexey Bataev346265e2015-09-25 10:37:12 +000098 /// \brief first argument (Expr *) contains optional argument of the
99 /// 'ordered' clause, the second one is true if the regions has 'ordered'
100 /// clause, false otherwise.
101 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000102 bool NowaitRegion = false;
103 bool CancelRegion = false;
104 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000105 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000106 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000108 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
109 ConstructLoc(Loc) {}
110 SharingMapTy() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000111 };
112
Axel Naumann323862e2016-02-03 10:45:22 +0000113 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114
115 /// \brief Stack of used declaration and their data-sharing attributes.
116 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000117 /// \brief true, if check for DSA must be from parent directive, false, if
118 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000119 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000120 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000121 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000122 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123
124 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
125
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000126 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000127
128 /// \brief Checks if the variable is a local for OpenMP region.
129 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000130
Alexey Bataev758e55e2013-09-06 18:03:48 +0000131public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000132 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
135 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000137 bool isForceVarCapturing() const { return ForceCapturing; }
138 void setForceVarCapturing(bool V) { ForceCapturing = V; }
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000141 Scope *CurScope, SourceLocation Loc) {
142 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
143 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 }
145
146 void pop() {
147 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
148 Stack.pop_back();
149 }
150
Alexey Bataev28c75412015-12-15 08:19:24 +0000151 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
152 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
153 }
154 const std::pair<OMPCriticalDirective *, llvm::APSInt>
155 getCriticalWithHint(const DeclarationNameInfo &Name) const {
156 auto I = Criticals.find(Name.getAsString());
157 if (I != Criticals.end())
158 return I->second;
159 return std::make_pair(nullptr, llvm::APSInt());
160 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000161 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000162 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000163 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000164 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000165
Alexey Bataev9c821032015-04-30 04:23:23 +0000166 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000167 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000168 /// \brief Check if the specified variable is a loop control variable for
169 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000170 /// \return The index of the loop control variable in the list of associated
171 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000172 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000173 /// \brief Check if the specified variable is a loop control variable for
174 /// parent region.
175 /// \return The index of the loop control variable in the list of associated
176 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000177 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000178 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
179 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000180 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000181
Alexey Bataev758e55e2013-09-06 18:03:48 +0000182 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000183 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
184 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000185
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186 /// \brief Returns data sharing attributes from top of the stack for the
187 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000188 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000189 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000190 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 /// \brief Checks if the specified variables has data-sharing attributes which
192 /// match specified \a CPred predicate in any directive which matches \a DPred
193 /// predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000194 DSAVarData hasDSA(ValueDecl *D,
195 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
196 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
197 bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000198 /// \brief Checks if the specified variables has data-sharing attributes which
199 /// match specified \a CPred predicate in any innermost directive which
200 /// matches \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000201 DSAVarData
202 hasInnermostDSA(ValueDecl *D,
203 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
204 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
205 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000206 /// \brief Checks if the specified variables has explicit data-sharing
207 /// attributes which match specified \a CPred predicate at the specified
208 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000209 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000210 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000211 unsigned Level, bool NotLastprivate = false);
Samuel Antao4be30e92015-10-02 17:14:03 +0000212
213 /// \brief Returns true if the directive at level \Level matches in the
214 /// specified \a DPred predicate.
215 bool hasExplicitDirective(
216 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
217 unsigned Level);
218
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000219 /// \brief Finds a directive which matches specified \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000220 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
221 const DeclarationNameInfo &,
222 SourceLocation)> &DPred,
223 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000224
Alexey Bataev758e55e2013-09-06 18:03:48 +0000225 /// \brief Returns currently analyzed directive.
226 OpenMPDirectiveKind getCurrentDirective() const {
227 return Stack.back().Directive;
228 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000229 /// \brief Returns parent directive.
230 OpenMPDirectiveKind getParentDirective() const {
231 if (Stack.size() > 2)
232 return Stack[Stack.size() - 2].Directive;
233 return OMPD_unknown;
234 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000235
236 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000237 void setDefaultDSANone(SourceLocation Loc) {
238 Stack.back().DefaultAttr = DSA_none;
239 Stack.back().DefaultAttrLoc = Loc;
240 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000241 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 void setDefaultDSAShared(SourceLocation Loc) {
243 Stack.back().DefaultAttr = DSA_shared;
244 Stack.back().DefaultAttrLoc = Loc;
245 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246
247 DefaultDataSharingAttributes getDefaultDSA() const {
248 return Stack.back().DefaultAttr;
249 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000250 SourceLocation getDefaultDSALocation() const {
251 return Stack.back().DefaultAttrLoc;
252 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000253
Alexey Bataevf29276e2014-06-18 04:14:57 +0000254 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000255 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000256 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000257 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000258 }
259
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000260 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000261 void setOrderedRegion(bool IsOrdered, Expr *Param) {
262 Stack.back().OrderedRegion.setInt(IsOrdered);
263 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000264 }
265 /// \brief Returns true, if parent region is ordered (has associated
266 /// 'ordered' clause), false - otherwise.
267 bool isParentOrderedRegion() const {
268 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000269 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000270 return false;
271 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000272 /// \brief Returns optional parameter for the ordered region.
273 Expr *getParentOrderedRegionParam() const {
274 if (Stack.size() > 2)
275 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
276 return nullptr;
277 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000278 /// \brief Marks current region as nowait (it has a 'nowait' clause).
279 void setNowaitRegion(bool IsNowait = true) {
280 Stack.back().NowaitRegion = IsNowait;
281 }
282 /// \brief Returns true, if parent region is nowait (has associated
283 /// 'nowait' clause), false - otherwise.
284 bool isParentNowaitRegion() const {
285 if (Stack.size() > 2)
286 return Stack[Stack.size() - 2].NowaitRegion;
287 return false;
288 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000289 /// \brief Marks parent region as cancel region.
290 void setParentCancelRegion(bool Cancel = true) {
291 if (Stack.size() > 2)
292 Stack[Stack.size() - 2].CancelRegion =
293 Stack[Stack.size() - 2].CancelRegion || Cancel;
294 }
295 /// \brief Return true if current region has inner cancel construct.
296 bool isCancelRegion() const {
297 return Stack.back().CancelRegion;
298 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000299
Alexey Bataev9c821032015-04-30 04:23:23 +0000300 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000301 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000302 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000303 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000304
Alexey Bataev13314bf2014-10-09 04:18:56 +0000305 /// \brief Marks current target region as one with closely nested teams
306 /// region.
307 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
308 if (Stack.size() > 2)
309 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
310 }
311 /// \brief Returns true, if current region has closely nested teams region.
312 bool hasInnerTeamsRegion() const {
313 return getInnerTeamsRegionLoc().isValid();
314 }
315 /// \brief Returns location of the nested teams region (if any).
316 SourceLocation getInnerTeamsRegionLoc() const {
317 if (Stack.size() > 1)
318 return Stack.back().InnerTeamsRegionLoc;
319 return SourceLocation();
320 }
321
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000322 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000324 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000325
Samuel Antao90927002016-04-26 14:54:23 +0000326 // Do the check specified in \a Check to all component lists and return true
327 // if any issue is found.
328 bool checkMappableExprComponentListsForDecl(
329 ValueDecl *VD, bool CurrentRegionOnly,
330 const llvm::function_ref<bool(
331 OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000332 auto SI = Stack.rbegin();
333 auto SE = Stack.rend();
334
335 if (SI == SE)
336 return false;
337
338 if (CurrentRegionOnly) {
339 SE = std::next(SI);
340 } else {
341 ++SI;
342 }
343
344 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000345 auto MI = SI->MappedExprComponents.find(VD);
346 if (MI != SI->MappedExprComponents.end())
347 for (auto &L : MI->second)
348 if (Check(L))
Samuel Antao5de996e2016-01-22 20:21:36 +0000349 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000350 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000351 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000352 }
353
Samuel Antao90927002016-04-26 14:54:23 +0000354 // Create a new mappable expression component list associated with a given
355 // declaration and initialize it with the provided list of components.
356 void addMappableExpressionComponents(
357 ValueDecl *VD,
358 OMPClauseMappableExprCommon::MappableExprComponentListRef Components) {
359 assert(Stack.size() > 1 &&
360 "Not expecting to retrieve components from a empty stack!");
361 auto &MEC = Stack.back().MappedExprComponents[VD];
362 // Create new entry and append the new components there.
363 MEC.resize(MEC.size() + 1);
364 MEC.back().append(Components.begin(), Components.end());
Kelvin Li0bff7af2015-11-23 05:32:03 +0000365 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000366
367 unsigned getNestingLevel() const {
368 assert(Stack.size() > 1);
369 return Stack.size() - 2;
370 }
Alexey Bataev8b427062016-05-25 12:36:08 +0000371 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
372 assert(Stack.size() > 2);
373 assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive));
374 Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
375 }
376 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
377 getDoacrossDependClauses() const {
378 assert(Stack.size() > 1);
379 if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
380 auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
381 return llvm::make_range(Ref.begin(), Ref.end());
382 }
383 return llvm::make_range(Stack[0].DoacrossDepends.end(),
384 Stack[0].DoacrossDepends.end());
385 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000387bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000388 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
389 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000390}
Alexey Bataeved09d242014-05-28 05:53:51 +0000391} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000393static ValueDecl *getCanonicalDecl(ValueDecl *D) {
394 auto *VD = dyn_cast<VarDecl>(D);
395 auto *FD = dyn_cast<FieldDecl>(D);
396 if (VD != nullptr) {
397 VD = VD->getCanonicalDecl();
398 D = VD;
399 } else {
400 assert(FD);
401 FD = FD->getCanonicalDecl();
402 D = FD;
403 }
404 return D;
405}
406
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000407DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000408 ValueDecl *D) {
409 D = getCanonicalDecl(D);
410 auto *VD = dyn_cast<VarDecl>(D);
411 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000412 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000413 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000414 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
415 // in a region but not in construct]
416 // File-scope or namespace-scope variables referenced in called routines
417 // in the region are shared unless they appear in a threadprivate
418 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000419 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 DVar.CKind = OMPC_shared;
421
422 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
423 // in a region but not in construct]
424 // Variables with static storage duration that are declared in called
425 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000426 if (VD && VD->hasGlobalStorage())
427 DVar.CKind = OMPC_shared;
428
429 // Non-static data members are shared by default.
430 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000431 DVar.CKind = OMPC_shared;
432
Alexey Bataev758e55e2013-09-06 18:03:48 +0000433 return DVar;
434 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000437 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
438 // in a Construct, C/C++, predetermined, p.1]
439 // Variables with automatic storage duration that are declared in a scope
440 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000441 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
442 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000443 DVar.CKind = OMPC_private;
444 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000445 }
446
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 // Explicitly specified attributes and local variables with predetermined
448 // attributes.
449 if (Iter->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000450 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000451 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000452 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000453 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 return DVar;
455 }
456
457 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
458 // in a Construct, C/C++, implicitly determined, p.1]
459 // In a parallel or task construct, the data-sharing attributes of these
460 // variables are determined by the default clause, if present.
461 switch (Iter->DefaultAttr) {
462 case DSA_shared:
463 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000464 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 return DVar;
466 case DSA_none:
467 return DVar;
468 case DSA_unspecified:
469 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470 // in a Construct, implicitly determined, p.2]
471 // In a parallel construct, if no default clause is present, these
472 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000473 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000474 if (isOpenMPParallelDirective(DVar.DKind) ||
475 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 DVar.CKind = OMPC_shared;
477 return DVar;
478 }
479
480 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
481 // in a Construct, implicitly determined, p.4]
482 // In a task construct, if no default clause is present, a variable that in
483 // the enclosing context is determined to be shared by all implicit tasks
484 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000485 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000486 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000487 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000488 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000489 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000490 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 // In a task construct, if no default clause is present, a variable
492 // whose data-sharing attribute is not determined by the rules above is
493 // firstprivate.
494 DVarTemp = getDSA(I, D);
495 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000496 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000497 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000498 return DVar;
499 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000500 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000501 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000502 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000504 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000505 return DVar;
506 }
507 }
508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
509 // in a Construct, implicitly determined, p.3]
510 // For constructs other than task, if no default clause is present, these
511 // variables inherit their data-sharing attributes from the enclosing
512 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000513 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000514}
515
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000516Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000517 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000518 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000519 auto It = Stack.back().AlignedMap.find(D);
520 if (It == Stack.back().AlignedMap.end()) {
521 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
522 Stack.back().AlignedMap[D] = NewDE;
523 return nullptr;
524 } else {
525 assert(It->second && "Unexpected nullptr expr in the aligned map");
526 return It->second;
527 }
528 return nullptr;
529}
530
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000531void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000532 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000533 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000534 Stack.back().LCVMap.insert(
535 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000536}
537
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000538DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000539 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000540 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000541 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
542 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000543}
544
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000545DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000546 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000547 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000548 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
549 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000550 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000551}
552
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000553ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000554 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
555 if (Stack[Stack.size() - 2].LCVMap.size() < I)
556 return nullptr;
557 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000558 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000559 return Pair.first;
560 }
561 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000562}
563
Alexey Bataev90c228f2016-02-08 09:29:13 +0000564void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
565 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000566 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000567 if (A == OMPC_threadprivate) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000568 auto &Data = Stack[0].SharingMap[D];
569 Data.Attributes = A;
570 Data.RefExpr.setPointer(E);
571 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000572 } else {
573 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000574 auto &Data = Stack.back().SharingMap[D];
575 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
576 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
577 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
578 (isLoopControlVariable(D).first && A == OMPC_private));
579 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
580 Data.RefExpr.setInt(/*IntVal=*/true);
581 return;
582 }
583 const bool IsLastprivate =
584 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
585 Data.Attributes = A;
586 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
587 Data.PrivateCopy = PrivateCopy;
588 if (PrivateCopy) {
589 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
590 Data.Attributes = A;
591 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
592 Data.PrivateCopy = nullptr;
593 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594 }
595}
596
Alexey Bataeved09d242014-05-28 05:53:51 +0000597bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000598 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000599 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000600 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000601 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000602 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000603 ++I;
604 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000605 if (I == E)
606 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000607 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000608 Scope *CurScope = getCurScope();
609 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000610 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000611 }
612 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000613 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000614 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000615}
616
Alexey Bataev39f915b82015-05-08 10:41:21 +0000617/// \brief Build a variable declaration for OpenMP loop iteration variable.
618static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000619 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000620 DeclContext *DC = SemaRef.CurContext;
621 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
622 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
623 VarDecl *Decl =
624 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000625 if (Attrs) {
626 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
627 I != E; ++I)
628 Decl->addAttr(*I);
629 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000630 Decl->setImplicit();
631 return Decl;
632}
633
634static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
635 SourceLocation Loc,
636 bool RefersToCapture = false) {
637 D->setReferenced();
638 D->markUsed(S.Context);
639 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
640 SourceLocation(), D, RefersToCapture, Loc, Ty,
641 VK_LValue);
642}
643
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000644DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
645 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000646 DSAVarData DVar;
647
648 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
649 // in a Construct, C/C++, predetermined, p.1]
650 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000651 auto *VD = dyn_cast<VarDecl>(D);
652 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
653 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000654 SemaRef.getLangOpts().OpenMPUseTLS &&
655 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000656 (VD && VD->getStorageClass() == SC_Register &&
657 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
658 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000659 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000660 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000661 }
662 if (Stack[0].SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000663 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 DVar.CKind = OMPC_threadprivate;
665 return DVar;
666 }
667
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000668 if (Stack.size() == 1) {
669 // Not in OpenMP execution region and top scope was already checked.
670 return DVar;
671 }
672
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000674 // in a Construct, C/C++, predetermined, p.4]
675 // Static data members are shared.
676 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
677 // in a Construct, C/C++, predetermined, p.7]
678 // Variables with static storage duration that are declared in a scope
679 // inside the construct are shared.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000680 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000681 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000682 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000683 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000684 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000686 DVar.CKind = OMPC_shared;
687 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000688 }
689
690 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000691 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
692 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000693 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
694 // in a Construct, C/C++, predetermined, p.6]
695 // Variables with const qualified type having no mutable member are
696 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000697 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000698 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000699 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
700 if (auto *CTD = CTSD->getSpecializedTemplate())
701 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000703 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
704 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000705 // Variables with const-qualified type having no mutable member may be
706 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000707 DSAVarData DVarTemp = hasDSA(
708 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
709 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000710 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
711 return DVar;
712
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713 DVar.CKind = OMPC_shared;
714 return DVar;
715 }
716
Alexey Bataev758e55e2013-09-06 18:03:48 +0000717 // Explicitly specified attributes and local variables with predetermined
718 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000724 auto I = std::prev(StartI);
725 if (I->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000726 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000727 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000728 DVar.CKind = I->SharingMap[D].Attributes;
729 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000730 }
731
732 return DVar;
733}
734
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000735DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
736 bool FromParent) {
737 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000738 auto StartI = Stack.rbegin();
739 auto EndI = std::prev(Stack.rend());
740 if (FromParent && StartI != EndI) {
741 StartI = std::next(StartI);
742 }
743 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000744}
745
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000746DSAStackTy::DSAVarData
747DSAStackTy::hasDSA(ValueDecl *D,
748 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
749 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
750 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000751 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000752 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000753 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000754 if (FromParent && StartI != EndI) {
755 StartI = std::next(StartI);
756 }
757 for (auto I = StartI, EE = EndI; I != EE; ++I) {
758 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000759 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000760 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000761 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000762 return DVar;
763 }
764 return DSAVarData();
765}
766
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000767DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
768 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
769 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
770 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000771 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000772 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000773 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000774 if (FromParent && StartI != EndI) {
775 StartI = std::next(StartI);
776 }
777 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000778 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000779 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000780 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000781 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000782 return DVar;
783 return DSAVarData();
784 }
785 return DSAVarData();
786}
787
Alexey Bataevaac108a2015-06-23 04:51:00 +0000788bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000789 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000790 unsigned Level, bool NotLastprivate) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000791 if (CPred(ClauseKindMode))
792 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000793 D = getCanonicalDecl(D);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000794 auto StartI = std::next(Stack.begin());
795 auto EndI = Stack.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000796 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000797 return false;
798 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000799 return (StartI->SharingMap.count(D) > 0) &&
800 StartI->SharingMap[D].RefExpr.getPointer() &&
801 CPred(StartI->SharingMap[D].Attributes) &&
802 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +0000803}
804
Samuel Antao4be30e92015-10-02 17:14:03 +0000805bool DSAStackTy::hasExplicitDirective(
806 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
807 unsigned Level) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000808 auto StartI = std::next(Stack.begin());
809 auto EndI = Stack.end();
Samuel Antao4be30e92015-10-02 17:14:03 +0000810 if (std::distance(StartI, EndI) <= (int)Level)
811 return false;
812 std::advance(StartI, Level);
813 return DPred(StartI->Directive);
814}
815
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000816bool DSAStackTy::hasDirective(
817 const llvm::function_ref<bool(OpenMPDirectiveKind,
818 const DeclarationNameInfo &, SourceLocation)>
819 &DPred,
820 bool FromParent) {
Samuel Antaof0d79752016-05-27 15:21:27 +0000821 // We look only in the enclosing region.
822 if (Stack.size() < 2)
823 return false;
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000824 auto StartI = std::next(Stack.rbegin());
825 auto EndI = std::prev(Stack.rend());
826 if (FromParent && StartI != EndI) {
827 StartI = std::next(StartI);
828 }
829 for (auto I = StartI, EE = EndI; I != EE; ++I) {
830 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
831 return true;
832 }
833 return false;
834}
835
Alexey Bataev758e55e2013-09-06 18:03:48 +0000836void Sema::InitDataSharingAttributesStack() {
837 VarDataSharingAttributesStack = new DSAStackTy(*this);
838}
839
840#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
841
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000842bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000843 assert(LangOpts.OpenMP && "OpenMP is not allowed");
844
845 auto &Ctx = getASTContext();
846 bool IsByRef = true;
847
848 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000849 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000850
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000851 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000852 // This table summarizes how a given variable should be passed to the device
853 // given its type and the clauses where it appears. This table is based on
854 // the description in OpenMP 4.5 [2.10.4, target Construct] and
855 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
856 //
857 // =========================================================================
858 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
859 // | |(tofrom:scalar)| | pvt | | | |
860 // =========================================================================
861 // | scl | | | | - | | bycopy|
862 // | scl | | - | x | - | - | bycopy|
863 // | scl | | x | - | - | - | null |
864 // | scl | x | | | - | | byref |
865 // | scl | x | - | x | - | - | bycopy|
866 // | scl | x | x | - | - | - | null |
867 // | scl | | - | - | - | x | byref |
868 // | scl | x | - | - | - | x | byref |
869 //
870 // | agg | n.a. | | | - | | byref |
871 // | agg | n.a. | - | x | - | - | byref |
872 // | agg | n.a. | x | - | - | - | null |
873 // | agg | n.a. | - | - | - | x | byref |
874 // | agg | n.a. | - | - | - | x[] | byref |
875 //
876 // | ptr | n.a. | | | - | | bycopy|
877 // | ptr | n.a. | - | x | - | - | bycopy|
878 // | ptr | n.a. | x | - | - | - | null |
879 // | ptr | n.a. | - | - | - | x | byref |
880 // | ptr | n.a. | - | - | - | x[] | bycopy|
881 // | ptr | n.a. | - | - | x | | bycopy|
882 // | ptr | n.a. | - | - | x | x | bycopy|
883 // | ptr | n.a. | - | - | x | x[] | bycopy|
884 // =========================================================================
885 // Legend:
886 // scl - scalar
887 // ptr - pointer
888 // agg - aggregate
889 // x - applies
890 // - - invalid in this combination
891 // [] - mapped with an array section
892 // byref - should be mapped by reference
893 // byval - should be mapped by value
894 // null - initialize a local variable to null on the device
895 //
896 // Observations:
897 // - All scalar declarations that show up in a map clause have to be passed
898 // by reference, because they may have been mapped in the enclosing data
899 // environment.
900 // - If the scalar value does not fit the size of uintptr, it has to be
901 // passed by reference, regardless the result in the table above.
902 // - For pointers mapped by value that have either an implicit map or an
903 // array section, the runtime library may pass the NULL value to the
904 // device instead of the value passed to it by the compiler.
905
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000906
907 if (Ty->isReferenceType())
908 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000909
910 // Locate map clauses and see if the variable being captured is referred to
911 // in any of those clauses. Here we only care about variables, not fields,
912 // because fields are part of aggregates.
913 bool IsVariableUsedInMapClause = false;
914 bool IsVariableAssociatedWithSection = false;
915
916 DSAStack->checkMappableExprComponentListsForDecl(
917 D, /*CurrentRegionOnly=*/true,
918 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
919 MapExprComponents) {
920
921 auto EI = MapExprComponents.rbegin();
922 auto EE = MapExprComponents.rend();
923
924 assert(EI != EE && "Invalid map expression!");
925
926 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
927 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
928
929 ++EI;
930 if (EI == EE)
931 return false;
932
933 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
934 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
935 isa<MemberExpr>(EI->getAssociatedExpression())) {
936 IsVariableAssociatedWithSection = true;
937 // There is nothing more we need to know about this variable.
938 return true;
939 }
940
941 // Keep looking for more map info.
942 return false;
943 });
944
945 if (IsVariableUsedInMapClause) {
946 // If variable is identified in a map clause it is always captured by
947 // reference except if it is a pointer that is dereferenced somehow.
948 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
949 } else {
950 // By default, all the data that has a scalar type is mapped by copy.
951 IsByRef = !Ty->isScalarType();
952 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000953 }
954
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000955 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
956 IsByRef = !DSAStack->hasExplicitDSA(
957 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
958 Level, /*NotLastprivate=*/true);
959 }
960
Samuel Antao86ace552016-04-27 22:40:57 +0000961 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000962 // and alignment, because the runtime library only deals with uintptr types.
963 // If it does not fit the uintptr size, we need to pass the data by reference
964 // instead.
965 if (!IsByRef &&
966 (Ctx.getTypeSizeInChars(Ty) >
967 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000968 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000969 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000970 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000971
972 return IsByRef;
973}
974
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000975unsigned Sema::getOpenMPNestingLevel() const {
976 assert(getLangOpts().OpenMP);
977 return DSAStack->getNestingLevel();
978}
979
Alexey Bataev90c228f2016-02-08 09:29:13 +0000980VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000981 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000982 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000983
984 // If we are attempting to capture a global variable in a directive with
985 // 'target' we return true so that this global is also mapped to the device.
986 //
987 // FIXME: If the declaration is enclosed in a 'declare target' directive,
988 // then it should not be captured. Therefore, an extra check has to be
989 // inserted here once support for 'declare target' is added.
990 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000991 auto *VD = dyn_cast<VarDecl>(D);
992 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000993 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000994 !DSAStack->isClauseParsingMode())
995 return VD;
Samuel Antaof0d79752016-05-27 15:21:27 +0000996 if (DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000997 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
998 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000999 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +00001000 },
Alexey Bataev90c228f2016-02-08 09:29:13 +00001001 false))
1002 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +00001003 }
1004
Alexey Bataev48977c32015-08-04 08:10:48 +00001005 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1006 (!DSAStack->isClauseParsingMode() ||
1007 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001008 auto &&Info = DSAStack->isLoopControlVariable(D);
1009 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001010 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001011 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001012 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001013 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001014 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001015 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001016 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001017 DVarPrivate = DSAStack->hasDSA(
1018 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1019 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001020 if (DVarPrivate.CKind != OMPC_unknown)
1021 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001022 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001023 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001024}
1025
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001026bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001027 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1028 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001029 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001030}
1031
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001032bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001033 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1034 // Return true if the current level is no longer enclosed in a target region.
1035
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001036 auto *VD = dyn_cast<VarDecl>(D);
1037 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001038 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1039 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001040}
1041
Alexey Bataeved09d242014-05-28 05:53:51 +00001042void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001043
1044void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1045 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001046 Scope *CurScope, SourceLocation Loc) {
1047 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001048 PushExpressionEvaluationContext(PotentiallyEvaluated);
1049}
1050
Alexey Bataevaac108a2015-06-23 04:51:00 +00001051void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1052 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001053}
1054
Alexey Bataevaac108a2015-06-23 04:51:00 +00001055void Sema::EndOpenMPClause() {
1056 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001057}
1058
Alexey Bataev758e55e2013-09-06 18:03:48 +00001059void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001060 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1061 // A variable of class type (or array thereof) that appears in a lastprivate
1062 // clause requires an accessible, unambiguous default constructor for the
1063 // class type, unless the list item is also specified in a firstprivate
1064 // clause.
1065 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001066 for (auto *C : D->clauses()) {
1067 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1068 SmallVector<Expr *, 8> PrivateCopies;
1069 for (auto *DE : Clause->varlists()) {
1070 if (DE->isValueDependent() || DE->isTypeDependent()) {
1071 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001072 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001073 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001074 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001075 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1076 QualType Type = VD->getType().getNonReferenceType();
1077 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001078 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001079 // Generate helper private variable and initialize it with the
1080 // default value. The address of the original variable is replaced
1081 // by the address of the new private variable in CodeGen. This new
1082 // variable is not added to IdResolver, so the code in the OpenMP
1083 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001084 auto *VDPrivate = buildVarDecl(
1085 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001086 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001087 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1088 if (VDPrivate->isInvalidDecl())
1089 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001090 PrivateCopies.push_back(buildDeclRefExpr(
1091 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001092 } else {
1093 // The variable is also a firstprivate, so initialization sequence
1094 // for private copy is generated already.
1095 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001096 }
1097 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001098 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001099 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001100 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001101 }
1102 }
1103 }
1104
Alexey Bataev758e55e2013-09-06 18:03:48 +00001105 DSAStack->pop();
1106 DiscardCleanupsInEvaluationContext();
1107 PopExpressionEvaluationContext();
1108}
1109
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001110static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1111 Expr *NumIterations, Sema &SemaRef,
1112 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001113
Alexey Bataeva769e072013-03-22 06:34:35 +00001114namespace {
1115
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001116class VarDeclFilterCCC : public CorrectionCandidateCallback {
1117private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001118 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001119
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001120public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001121 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001122 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001123 NamedDecl *ND = Candidate.getCorrectionDecl();
1124 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1125 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001126 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1127 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001128 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001129 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001130 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001131};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001132
1133class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1134private:
1135 Sema &SemaRef;
1136
1137public:
1138 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1139 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1140 NamedDecl *ND = Candidate.getCorrectionDecl();
1141 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1142 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1143 SemaRef.getCurScope());
1144 }
1145 return false;
1146 }
1147};
1148
Alexey Bataeved09d242014-05-28 05:53:51 +00001149} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001150
1151ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1152 CXXScopeSpec &ScopeSpec,
1153 const DeclarationNameInfo &Id) {
1154 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1155 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1156
1157 if (Lookup.isAmbiguous())
1158 return ExprError();
1159
1160 VarDecl *VD;
1161 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001162 if (TypoCorrection Corrected = CorrectTypo(
1163 Id, LookupOrdinaryName, CurScope, nullptr,
1164 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001165 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001166 PDiag(Lookup.empty()
1167 ? diag::err_undeclared_var_use_suggest
1168 : diag::err_omp_expected_var_arg_suggest)
1169 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001170 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001172 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1173 : diag::err_omp_expected_var_arg)
1174 << Id.getName();
1175 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001177 } else {
1178 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001179 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1181 return ExprError();
1182 }
1183 }
1184 Lookup.suppressDiagnostics();
1185
1186 // OpenMP [2.9.2, Syntax, C/C++]
1187 // Variables must be file-scope, namespace-scope, or static block-scope.
1188 if (!VD->hasGlobalStorage()) {
1189 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001190 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1191 bool IsDecl =
1192 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1195 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001196 return ExprError();
1197 }
1198
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001199 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1200 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001201 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1202 // A threadprivate directive for file-scope variables must appear outside
1203 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001204 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1205 !getCurLexicalContext()->isTranslationUnit()) {
1206 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001207 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1208 bool IsDecl =
1209 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1210 Diag(VD->getLocation(),
1211 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1212 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001213 return ExprError();
1214 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001215 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1216 // A threadprivate directive for static class member variables must appear
1217 // in the class definition, in the same scope in which the member
1218 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001219 if (CanonicalVD->isStaticDataMember() &&
1220 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1221 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001222 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1223 bool IsDecl =
1224 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1225 Diag(VD->getLocation(),
1226 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1227 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001228 return ExprError();
1229 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1231 // A threadprivate directive for namespace-scope variables must appear
1232 // outside any definition or declaration other than the namespace
1233 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001234 if (CanonicalVD->getDeclContext()->isNamespace() &&
1235 (!getCurLexicalContext()->isFileContext() ||
1236 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1237 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001238 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1239 bool IsDecl =
1240 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1241 Diag(VD->getLocation(),
1242 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1243 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001244 return ExprError();
1245 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001246 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1247 // A threadprivate directive for static block-scope variables must appear
1248 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001249 if (CanonicalVD->isStaticLocal() && CurScope &&
1250 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001251 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001252 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1253 bool IsDecl =
1254 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1255 Diag(VD->getLocation(),
1256 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1257 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001258 return ExprError();
1259 }
1260
1261 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1262 // A threadprivate directive must lexically precede all references to any
1263 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001264 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001265 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001266 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001267 return ExprError();
1268 }
1269
1270 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001271 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1272 SourceLocation(), VD,
1273 /*RefersToEnclosingVariableOrCapture=*/false,
1274 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001275}
1276
Alexey Bataeved09d242014-05-28 05:53:51 +00001277Sema::DeclGroupPtrTy
1278Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1279 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001280 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001281 CurContext->addDecl(D);
1282 return DeclGroupPtrTy::make(DeclGroupRef(D));
1283 }
David Blaikie0403cb12016-01-15 23:43:25 +00001284 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001285}
1286
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001287namespace {
1288class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1289 Sema &SemaRef;
1290
1291public:
1292 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1293 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1294 if (VD->hasLocalStorage()) {
1295 SemaRef.Diag(E->getLocStart(),
1296 diag::err_omp_local_var_in_threadprivate_init)
1297 << E->getSourceRange();
1298 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1299 << VD << VD->getSourceRange();
1300 return true;
1301 }
1302 }
1303 return false;
1304 }
1305 bool VisitStmt(const Stmt *S) {
1306 for (auto Child : S->children()) {
1307 if (Child && Visit(Child))
1308 return true;
1309 }
1310 return false;
1311 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001312 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001313};
1314} // namespace
1315
Alexey Bataeved09d242014-05-28 05:53:51 +00001316OMPThreadPrivateDecl *
1317Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001318 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001319 for (auto &RefExpr : VarList) {
1320 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001321 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1322 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001323
Alexey Bataev376b4a42016-02-09 09:41:09 +00001324 // Mark variable as used.
1325 VD->setReferenced();
1326 VD->markUsed(Context);
1327
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001328 QualType QType = VD->getType();
1329 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1330 // It will be analyzed later.
1331 Vars.push_back(DE);
1332 continue;
1333 }
1334
Alexey Bataeva769e072013-03-22 06:34:35 +00001335 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1336 // A threadprivate variable must not have an incomplete type.
1337 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001338 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001339 continue;
1340 }
1341
1342 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1343 // A threadprivate variable must not have a reference type.
1344 if (VD->getType()->isReferenceType()) {
1345 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001346 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1347 bool IsDecl =
1348 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1349 Diag(VD->getLocation(),
1350 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1351 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001352 continue;
1353 }
1354
Samuel Antaof8b50122015-07-13 22:54:53 +00001355 // Check if this is a TLS variable. If TLS is not being supported, produce
1356 // the corresponding diagnostic.
1357 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1358 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1359 getLangOpts().OpenMPUseTLS &&
1360 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001361 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1362 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001363 Diag(ILoc, diag::err_omp_var_thread_local)
1364 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001365 bool IsDecl =
1366 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1367 Diag(VD->getLocation(),
1368 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1369 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001370 continue;
1371 }
1372
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001373 // Check if initial value of threadprivate variable reference variable with
1374 // local storage (it is not supported by runtime).
1375 if (auto Init = VD->getAnyInitializer()) {
1376 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001377 if (Checker.Visit(Init))
1378 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001379 }
1380
Alexey Bataeved09d242014-05-28 05:53:51 +00001381 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001382 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001383 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1384 Context, SourceRange(Loc, Loc)));
1385 if (auto *ML = Context.getASTMutationListener())
1386 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001387 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001388 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001389 if (!Vars.empty()) {
1390 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1391 Vars);
1392 D->setAccess(AS_public);
1393 }
1394 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001395}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001396
Alexey Bataev7ff55242014-06-19 09:13:45 +00001397static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001398 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001399 bool IsLoopIterVar = false) {
1400 if (DVar.RefExpr) {
1401 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1402 << getOpenMPClauseName(DVar.CKind);
1403 return;
1404 }
1405 enum {
1406 PDSA_StaticMemberShared,
1407 PDSA_StaticLocalVarShared,
1408 PDSA_LoopIterVarPrivate,
1409 PDSA_LoopIterVarLinear,
1410 PDSA_LoopIterVarLastprivate,
1411 PDSA_ConstVarShared,
1412 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001413 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001414 PDSA_LocalVarPrivate,
1415 PDSA_Implicit
1416 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001417 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001418 auto ReportLoc = D->getLocation();
1419 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001420 if (IsLoopIterVar) {
1421 if (DVar.CKind == OMPC_private)
1422 Reason = PDSA_LoopIterVarPrivate;
1423 else if (DVar.CKind == OMPC_lastprivate)
1424 Reason = PDSA_LoopIterVarLastprivate;
1425 else
1426 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001427 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1428 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001429 Reason = PDSA_TaskVarFirstprivate;
1430 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001431 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001432 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001433 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001434 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001435 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001436 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001437 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001438 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001439 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001440 ReportHint = true;
1441 Reason = PDSA_LocalVarPrivate;
1442 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001443 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001444 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001445 << Reason << ReportHint
1446 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1447 } else if (DVar.ImplicitDSALoc.isValid()) {
1448 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1449 << getOpenMPClauseName(DVar.CKind);
1450 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001451}
1452
Alexey Bataev758e55e2013-09-06 18:03:48 +00001453namespace {
1454class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1455 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001456 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001457 bool ErrorFound;
1458 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001459 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001460 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001461
Alexey Bataev758e55e2013-09-06 18:03:48 +00001462public:
1463 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001464 if (E->isTypeDependent() || E->isValueDependent() ||
1465 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1466 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001467 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001469 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1470 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001471
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001472 auto DVar = Stack->getTopDSA(VD, false);
1473 // Check if the variable has explicit DSA set and stop analysis if it so.
1474 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001476 auto ELoc = E->getExprLoc();
1477 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001478 // The default(none) clause requires that each variable that is referenced
1479 // in the construct, and does not have a predetermined data-sharing
1480 // attribute, must have its data-sharing attribute explicitly determined
1481 // by being listed in a data-sharing attribute clause.
1482 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001483 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001484 VarsWithInheritedDSA.count(VD) == 0) {
1485 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486 return;
1487 }
1488
1489 // OpenMP [2.9.3.6, Restrictions, p.2]
1490 // A list item that appears in a reduction clause of the innermost
1491 // enclosing worksharing or parallel construct may not be accessed in an
1492 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001493 DVar = Stack->hasInnermostDSA(
1494 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1495 [](OpenMPDirectiveKind K) -> bool {
1496 return isOpenMPParallelDirective(K) ||
1497 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1498 },
1499 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001500 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001501 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001502 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1503 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001504 return;
1505 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001506
1507 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001508 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001509 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1510 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001511 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001512 }
1513 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001514 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001515 if (E->isTypeDependent() || E->isValueDependent() ||
1516 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1517 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001518 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1519 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1520 auto DVar = Stack->getTopDSA(FD, false);
1521 // Check if the variable has explicit DSA set and stop analysis if it
1522 // so.
1523 if (DVar.RefExpr)
1524 return;
1525
1526 auto ELoc = E->getExprLoc();
1527 auto DKind = Stack->getCurrentDirective();
1528 // OpenMP [2.9.3.6, Restrictions, p.2]
1529 // A list item that appears in a reduction clause of the innermost
1530 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001531 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001532 DVar = Stack->hasInnermostDSA(
1533 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1534 [](OpenMPDirectiveKind K) -> bool {
1535 return isOpenMPParallelDirective(K) ||
1536 isOpenMPWorksharingDirective(K) ||
1537 isOpenMPTeamsDirective(K);
1538 },
1539 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001540 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001541 ErrorFound = true;
1542 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1543 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1544 return;
1545 }
1546
1547 // Define implicit data-sharing attributes for task.
1548 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001549 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1550 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001551 ImplicitFirstprivate.push_back(E);
1552 }
1553 }
1554 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001555 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001556 for (auto *C : S->clauses()) {
1557 // Skip analysis of arguments of implicitly defined firstprivate clause
1558 // for task directives.
1559 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1560 for (auto *CC : C->children()) {
1561 if (CC)
1562 Visit(CC);
1563 }
1564 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001565 }
1566 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001567 for (auto *C : S->children()) {
1568 if (C && !isa<OMPExecutableDirective>(C))
1569 Visit(C);
1570 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001571 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001572
1573 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001574 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001575 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001576 return VarsWithInheritedDSA;
1577 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001578
Alexey Bataev7ff55242014-06-19 09:13:45 +00001579 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1580 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001581};
Alexey Bataeved09d242014-05-28 05:53:51 +00001582} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001583
Alexey Bataevbae9a792014-06-27 10:37:06 +00001584void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001585 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00001586 case OMPD_parallel:
1587 case OMPD_parallel_for:
1588 case OMPD_parallel_for_simd:
1589 case OMPD_parallel_sections:
1590 case OMPD_teams: {
Alexey Bataev9959db52014-05-06 10:08:46 +00001591 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001592 QualType KmpInt32PtrTy =
1593 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001594 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001595 std::make_pair(".global_tid.", KmpInt32PtrTy),
1596 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1597 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001598 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001599 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1600 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001601 break;
1602 }
Kelvin Li70a12c52016-07-13 21:51:49 +00001603 case OMPD_simd:
1604 case OMPD_for:
1605 case OMPD_for_simd:
1606 case OMPD_sections:
1607 case OMPD_section:
1608 case OMPD_single:
1609 case OMPD_master:
1610 case OMPD_critical:
1611 case OMPD_ordered:
1612 case OMPD_atomic:
1613 case OMPD_target_data:
1614 case OMPD_target:
1615 case OMPD_target_parallel:
1616 case OMPD_target_parallel_for:
1617 case OMPD_taskgroup:
1618 case OMPD_distribute: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001619 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001620 std::make_pair(StringRef(), QualType()) // __context with shared vars
1621 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001622 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1623 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001624 break;
1625 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001626 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001627 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001628 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1629 FunctionProtoType::ExtProtoInfo EPI;
1630 EPI.Variadic = true;
1631 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001632 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001633 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001634 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1635 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1636 std::make_pair(".copy_fn.",
1637 Context.getPointerType(CopyFnType).withConst()),
1638 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001639 std::make_pair(StringRef(), QualType()) // __context with shared vars
1640 };
1641 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1642 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001643 // Mark this captured region as inlined, because we don't use outlined
1644 // function directly.
1645 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1646 AlwaysInlineAttr::CreateImplicit(
1647 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001648 break;
1649 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001650 case OMPD_taskloop:
1651 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001652 QualType KmpInt32Ty =
1653 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1654 QualType KmpUInt64Ty =
1655 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1656 QualType KmpInt64Ty =
1657 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1658 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1659 FunctionProtoType::ExtProtoInfo EPI;
1660 EPI.Variadic = true;
1661 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001662 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001663 std::make_pair(".global_tid.", KmpInt32Ty),
1664 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1665 std::make_pair(".privates.",
1666 Context.VoidPtrTy.withConst().withRestrict()),
1667 std::make_pair(
1668 ".copy_fn.",
1669 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1670 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1671 std::make_pair(".lb.", KmpUInt64Ty),
1672 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1673 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001674 std::make_pair(StringRef(), QualType()) // __context with shared vars
1675 };
1676 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1677 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001678 // Mark this captured region as inlined, because we don't use outlined
1679 // function directly.
1680 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1681 AlwaysInlineAttr::CreateImplicit(
1682 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001683 break;
1684 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001685 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001686 case OMPD_distribute_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001687 case OMPD_distribute_parallel_for: {
1688 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1689 QualType KmpInt32PtrTy =
1690 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1691 Sema::CapturedParamNameType Params[] = {
1692 std::make_pair(".global_tid.", KmpInt32PtrTy),
1693 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1694 std::make_pair(".previous.lb.", Context.getSizeType()),
1695 std::make_pair(".previous.ub.", Context.getSizeType()),
1696 std::make_pair(StringRef(), QualType()) // __context with shared vars
1697 };
1698 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1699 Params);
1700 break;
1701 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001702 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001703 case OMPD_taskyield:
1704 case OMPD_barrier:
1705 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001706 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001707 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001708 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001709 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001710 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001711 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001712 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001713 case OMPD_declare_target:
1714 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001715 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001716 llvm_unreachable("OpenMP Directive is not allowed");
1717 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001718 llvm_unreachable("Unknown OpenMP directive");
1719 }
1720}
1721
Alexey Bataev3392d762016-02-16 11:18:12 +00001722static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001723 Expr *CaptureExpr, bool WithInit,
1724 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001725 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001726 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001727 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001728 QualType Ty = Init->getType();
1729 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1730 if (S.getLangOpts().CPlusPlus)
1731 Ty = C.getLValueReferenceType(Ty);
1732 else {
1733 Ty = C.getPointerType(Ty);
1734 ExprResult Res =
1735 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1736 if (!Res.isUsable())
1737 return nullptr;
1738 Init = Res.get();
1739 }
Alexey Bataev61205072016-03-02 04:57:40 +00001740 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001741 }
1742 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001743 if (!WithInit)
1744 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001745 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001746 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1747 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001748 return CED;
1749}
1750
Alexey Bataev61205072016-03-02 04:57:40 +00001751static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1752 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001753 OMPCapturedExprDecl *CD;
1754 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1755 CD = cast<OMPCapturedExprDecl>(VD);
1756 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001757 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1758 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001759 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001760 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001761}
1762
Alexey Bataev5a3af132016-03-29 08:58:54 +00001763static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1764 if (!Ref) {
1765 auto *CD =
1766 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1767 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1768 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1769 CaptureExpr->getExprLoc());
1770 }
1771 ExprResult Res = Ref;
1772 if (!S.getLangOpts().CPlusPlus &&
1773 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1774 Ref->getType()->isPointerType())
1775 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1776 if (!Res.isUsable())
1777 return ExprError();
1778 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001779}
1780
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001781StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1782 ArrayRef<OMPClause *> Clauses) {
1783 if (!S.isUsable()) {
1784 ActOnCapturedRegionError();
1785 return StmtError();
1786 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001787
1788 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001789 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001790 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001791 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001792 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001793 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001794 Clause->getClauseKind() == OMPC_copyprivate ||
1795 (getLangOpts().OpenMPUseTLS &&
1796 getASTContext().getTargetInfo().isTLSSupported() &&
1797 Clause->getClauseKind() == OMPC_copyin)) {
1798 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001799 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001800 for (auto *VarRef : Clause->children()) {
1801 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001802 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001803 }
1804 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001805 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001806 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001807 // Mark all variables in private list clauses as used in inner region.
1808 // Required for proper codegen of combined directives.
1809 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001810 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001811 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1812 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001813 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1814 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001815 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001816 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1817 if (auto *E = C->getPostUpdateExpr())
1818 MarkDeclarationsReferencedInExpr(E);
1819 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001820 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001821 if (Clause->getClauseKind() == OMPC_schedule)
1822 SC = cast<OMPScheduleClause>(Clause);
1823 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001824 OC = cast<OMPOrderedClause>(Clause);
1825 else if (Clause->getClauseKind() == OMPC_linear)
1826 LCs.push_back(cast<OMPLinearClause>(Clause));
1827 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001828 bool ErrorFound = false;
1829 // OpenMP, 2.7.1 Loop Construct, Restrictions
1830 // The nonmonotonic modifier cannot be specified if an ordered clause is
1831 // specified.
1832 if (SC &&
1833 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1834 SC->getSecondScheduleModifier() ==
1835 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1836 OC) {
1837 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1838 ? SC->getFirstScheduleModifierLoc()
1839 : SC->getSecondScheduleModifierLoc(),
1840 diag::err_omp_schedule_nonmonotonic_ordered)
1841 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1842 ErrorFound = true;
1843 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001844 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1845 for (auto *C : LCs) {
1846 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1847 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1848 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001849 ErrorFound = true;
1850 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001851 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1852 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1853 OC->getNumForLoops()) {
1854 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1855 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1856 ErrorFound = true;
1857 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001858 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001859 ActOnCapturedRegionError();
1860 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001861 }
1862 return ActOnCapturedRegionEnd(S.get());
1863}
1864
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001865static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1866 OpenMPDirectiveKind CurrentRegion,
1867 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001868 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001869 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001870 // Allowed nesting of constructs
1871 // +------------------+-----------------+------------------------------------+
1872 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1873 // +------------------+-----------------+------------------------------------+
1874 // | parallel | parallel | * |
1875 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001876 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001877 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001878 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001879 // | parallel | simd | * |
1880 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001881 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001882 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001883 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001884 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001885 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001886 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001887 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001888 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001889 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001890 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001891 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001892 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001893 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001894 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001895 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001896 // | parallel | target parallel | * |
1897 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001898 // | parallel | target enter | * |
1899 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001900 // | parallel | target exit | * |
1901 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001902 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001903 // | parallel | cancellation | |
1904 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001905 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001906 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001907 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001908 // | parallel | distribute | + |
1909 // | parallel | distribute | + |
1910 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001911 // | parallel | distribute | + |
1912 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001913 // | parallel | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001914 // +------------------+-----------------+------------------------------------+
1915 // | for | parallel | * |
1916 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001917 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001918 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001919 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001920 // | for | simd | * |
1921 // | for | sections | + |
1922 // | for | section | + |
1923 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001924 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001925 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001926 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001927 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001928 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001929 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001930 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001931 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001932 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001933 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001934 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001935 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001936 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001937 // | for | target parallel | * |
1938 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001939 // | for | target enter | * |
1940 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001941 // | for | target exit | * |
1942 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001943 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001944 // | for | cancellation | |
1945 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001946 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001947 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001948 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001949 // | for | distribute | + |
1950 // | for | distribute | + |
1951 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001952 // | for | distribute | + |
1953 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001954 // | for | distribute 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 | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001996 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001997 // | critical | parallel | * |
1998 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001999 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002000 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002001 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002002 // | critical | simd | * |
2003 // | critical | sections | + |
2004 // | critical | section | + |
2005 // | critical | single | + |
2006 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002007 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002008 // | critical |parallel sections| * |
2009 // | critical | task | * |
2010 // | critical | taskyield | * |
2011 // | critical | barrier | + |
2012 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002013 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002014 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002015 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002016 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002017 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002018 // | critical | target parallel | * |
2019 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002020 // | critical | target enter | * |
2021 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002022 // | critical | target exit | * |
2023 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002024 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002025 // | critical | cancellation | |
2026 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002027 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002028 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002029 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002030 // | critical | distribute | + |
2031 // | critical | distribute | + |
2032 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002033 // | critical | distribute | + |
2034 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002035 // | critical | distribute simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002036 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002037 // | simd | parallel | |
2038 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002039 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002040 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002041 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002042 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002043 // | simd | sections | |
2044 // | simd | section | |
2045 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002046 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002047 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002048 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002049 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002050 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002051 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002052 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002053 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002054 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002055 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002056 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002057 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002058 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002059 // | simd | target parallel | |
2060 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002061 // | simd | target enter | |
2062 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002063 // | simd | target exit | |
2064 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002065 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002066 // | simd | cancellation | |
2067 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002068 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002069 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002070 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002071 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002072 // | simd | distribute | |
2073 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002074 // | simd | distribute | |
2075 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002076 // | simd | distribute simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002077 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002078 // | for simd | parallel | |
2079 // | for simd | for | |
2080 // | for simd | for simd | |
2081 // | for simd | master | |
2082 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002083 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002084 // | for simd | sections | |
2085 // | for simd | section | |
2086 // | for simd | single | |
2087 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002088 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002089 // | for simd |parallel sections| |
2090 // | for simd | task | |
2091 // | for simd | taskyield | |
2092 // | for simd | barrier | |
2093 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002094 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002095 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002096 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002097 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002098 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002099 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002100 // | for simd | target parallel | |
2101 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002102 // | for simd | target enter | |
2103 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002104 // | for simd | target exit | |
2105 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002106 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002107 // | for simd | cancellation | |
2108 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002109 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002110 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002111 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002112 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002113 // | for simd | distribute | |
2114 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002115 // | for simd | distribute | |
2116 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002117 // | for simd | distribute simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002118 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002119 // | parallel for simd| parallel | |
2120 // | parallel for simd| for | |
2121 // | parallel for simd| for simd | |
2122 // | parallel for simd| master | |
2123 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002124 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002125 // | parallel for simd| sections | |
2126 // | parallel for simd| section | |
2127 // | parallel for simd| single | |
2128 // | parallel for simd| parallel for | |
2129 // | parallel for simd|parallel for simd| |
2130 // | parallel for simd|parallel sections| |
2131 // | parallel for simd| task | |
2132 // | parallel for simd| taskyield | |
2133 // | parallel for simd| barrier | |
2134 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002135 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002136 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002137 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002138 // | parallel for simd| atomic | |
2139 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002140 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002141 // | parallel for simd| target parallel | |
2142 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002143 // | parallel for simd| target enter | |
2144 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002145 // | parallel for simd| target exit | |
2146 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002147 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002148 // | parallel for simd| cancellation | |
2149 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002150 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002151 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002152 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002153 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002154 // | parallel for simd| distribute | |
2155 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002156 // | parallel for simd| distribute | |
2157 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002158 // | parallel for simd| distribute simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002159 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002160 // | sections | parallel | * |
2161 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002162 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002163 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002164 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002165 // | sections | simd | * |
2166 // | sections | sections | + |
2167 // | sections | section | * |
2168 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002169 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002170 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002171 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002172 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002173 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002174 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002175 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002176 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002177 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002178 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002179 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002180 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002181 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002182 // | sections | target parallel | * |
2183 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002184 // | sections | target enter | * |
2185 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002186 // | sections | target exit | * |
2187 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002188 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002189 // | sections | cancellation | |
2190 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002191 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002192 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002193 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002194 // | sections | distribute | + |
2195 // | sections | distribute | + |
2196 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002197 // | sections | distribute | + |
2198 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002199 // | sections | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002200 // +------------------+-----------------+------------------------------------+
2201 // | section | parallel | * |
2202 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002203 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002204 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002205 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002206 // | section | simd | * |
2207 // | section | sections | + |
2208 // | section | section | + |
2209 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002210 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002211 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002212 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002213 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002214 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002215 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002216 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002217 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002218 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002219 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002220 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002221 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002222 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002223 // | section | target parallel | * |
2224 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002225 // | section | target enter | * |
2226 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002227 // | section | target exit | * |
2228 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002229 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002230 // | section | cancellation | |
2231 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002232 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002233 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002234 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002235 // | section | distribute | + |
2236 // | section | distribute | + |
2237 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002238 // | section | distribute | + |
2239 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002240 // | section | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002241 // +------------------+-----------------+------------------------------------+
2242 // | single | parallel | * |
2243 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002244 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002245 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002246 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002247 // | single | simd | * |
2248 // | single | sections | + |
2249 // | single | section | + |
2250 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002251 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002252 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002253 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002254 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002255 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002256 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002257 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002258 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002259 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002260 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002261 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002262 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002263 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002264 // | single | target parallel | * |
2265 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002266 // | single | target enter | * |
2267 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002268 // | single | target exit | * |
2269 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002270 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002271 // | single | cancellation | |
2272 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002273 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002274 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002275 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002276 // | single | distribute | + |
2277 // | single | distribute | + |
2278 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002279 // | single | distribute | + |
2280 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002281 // | single | distribute simd | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002282 // +------------------+-----------------+------------------------------------+
2283 // | parallel for | parallel | * |
2284 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002285 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002286 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002287 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002288 // | parallel for | simd | * |
2289 // | parallel for | sections | + |
2290 // | parallel for | section | + |
2291 // | parallel for | single | + |
2292 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002293 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002294 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002295 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002296 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002297 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002298 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002299 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002300 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002301 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002302 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002303 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002304 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002305 // | parallel for | target parallel | * |
2306 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002307 // | parallel for | target enter | * |
2308 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002309 // | parallel for | target exit | * |
2310 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002311 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002312 // | parallel for | cancellation | |
2313 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002314 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002315 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002316 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002317 // | parallel for | distribute | + |
2318 // | parallel for | distribute | + |
2319 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002320 // | parallel for | distribute | + |
2321 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002322 // | parallel for | distribute simd | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002323 // +------------------+-----------------+------------------------------------+
2324 // | parallel sections| parallel | * |
2325 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002326 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002327 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002328 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002329 // | parallel sections| simd | * |
2330 // | parallel sections| sections | + |
2331 // | parallel sections| section | * |
2332 // | parallel sections| single | + |
2333 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002334 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002335 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002336 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002337 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002338 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002339 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002340 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002341 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002342 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002343 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002344 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002345 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002346 // | parallel sections| target parallel | * |
2347 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002348 // | parallel sections| target enter | * |
2349 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002350 // | parallel sections| target exit | * |
2351 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002352 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002353 // | parallel sections| cancellation | |
2354 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002355 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002356 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002357 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002358 // | parallel sections| distribute | + |
2359 // | parallel sections| distribute | + |
2360 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002361 // | parallel sections| distribute | + |
2362 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002363 // | parallel sections| distribute simd | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002364 // +------------------+-----------------+------------------------------------+
2365 // | task | parallel | * |
2366 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002367 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002368 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002369 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002370 // | task | simd | * |
2371 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002372 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002373 // | task | single | + |
2374 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002375 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002376 // | task |parallel sections| * |
2377 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002378 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002379 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002380 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002381 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002382 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002383 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002384 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002385 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002386 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002387 // | task | target parallel | * |
2388 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002389 // | task | target enter | * |
2390 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002391 // | task | target exit | * |
2392 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002393 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002394 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002395 // | | point | ! |
2396 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002397 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002398 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002399 // | task | distribute | + |
2400 // | task | distribute | + |
2401 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002402 // | task | distribute | + |
2403 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002404 // | task | distribute simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002405 // +------------------+-----------------+------------------------------------+
2406 // | ordered | parallel | * |
2407 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002408 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002409 // | ordered | master | * |
2410 // | ordered | critical | * |
2411 // | ordered | simd | * |
2412 // | ordered | sections | + |
2413 // | ordered | section | + |
2414 // | ordered | single | + |
2415 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002416 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002417 // | ordered |parallel sections| * |
2418 // | ordered | task | * |
2419 // | ordered | taskyield | * |
2420 // | ordered | barrier | + |
2421 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002422 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002423 // | ordered | flush | * |
2424 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002425 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002426 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002427 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002428 // | ordered | target parallel | * |
2429 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002430 // | ordered | target enter | * |
2431 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002432 // | ordered | target exit | * |
2433 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002434 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002435 // | ordered | cancellation | |
2436 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002437 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002438 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002439 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002440 // | ordered | distribute | + |
2441 // | ordered | distribute | + |
2442 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002443 // | ordered | distribute | + |
2444 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002445 // | ordered | distribute simd | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002446 // +------------------+-----------------+------------------------------------+
2447 // | atomic | parallel | |
2448 // | atomic | for | |
2449 // | atomic | for simd | |
2450 // | atomic | master | |
2451 // | atomic | critical | |
2452 // | atomic | simd | |
2453 // | atomic | sections | |
2454 // | atomic | section | |
2455 // | atomic | single | |
2456 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002457 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002458 // | atomic |parallel sections| |
2459 // | atomic | task | |
2460 // | atomic | taskyield | |
2461 // | atomic | barrier | |
2462 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002463 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002464 // | atomic | flush | |
2465 // | atomic | ordered | |
2466 // | atomic | atomic | |
2467 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002468 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002469 // | atomic | target parallel | |
2470 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002471 // | atomic | target enter | |
2472 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002473 // | atomic | target exit | |
2474 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002475 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002476 // | atomic | cancellation | |
2477 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002478 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002479 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002480 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002481 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002482 // | atomic | distribute | |
2483 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002484 // | atomic | distribute | |
2485 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002486 // | atomic | distribute simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002487 // +------------------+-----------------+------------------------------------+
2488 // | target | parallel | * |
2489 // | target | for | * |
2490 // | target | for simd | * |
2491 // | target | master | * |
2492 // | target | critical | * |
2493 // | target | simd | * |
2494 // | target | sections | * |
2495 // | target | section | * |
2496 // | target | single | * |
2497 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002498 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002499 // | target |parallel sections| * |
2500 // | target | task | * |
2501 // | target | taskyield | * |
2502 // | target | barrier | * |
2503 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002504 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002505 // | target | flush | * |
2506 // | target | ordered | * |
2507 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002508 // | target | target | |
2509 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002510 // | target | target parallel | |
2511 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002512 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002513 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002514 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002515 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002516 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002517 // | target | cancellation | |
2518 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002519 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002520 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002521 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002522 // | target | distribute | + |
2523 // | target | distribute | + |
2524 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002525 // | target | distribute | + |
2526 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002527 // | target | distribute simd | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002528 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002529 // | target parallel | parallel | * |
2530 // | target parallel | for | * |
2531 // | target parallel | for simd | * |
2532 // | target parallel | master | * |
2533 // | target parallel | critical | * |
2534 // | target parallel | simd | * |
2535 // | target parallel | sections | * |
2536 // | target parallel | section | * |
2537 // | target parallel | single | * |
2538 // | target parallel | parallel for | * |
2539 // | target parallel |parallel for simd| * |
2540 // | target parallel |parallel sections| * |
2541 // | target parallel | task | * |
2542 // | target parallel | taskyield | * |
2543 // | target parallel | barrier | * |
2544 // | target parallel | taskwait | * |
2545 // | target parallel | taskgroup | * |
2546 // | target parallel | flush | * |
2547 // | target parallel | ordered | * |
2548 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002549 // | target parallel | target | |
2550 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002551 // | target parallel | target parallel | |
2552 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002553 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002554 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002555 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002556 // | | data | |
2557 // | target parallel | teams | |
2558 // | target parallel | cancellation | |
2559 // | | point | ! |
2560 // | target parallel | cancel | ! |
2561 // | target parallel | taskloop | * |
2562 // | target parallel | taskloop simd | * |
2563 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002564 // | target parallel | distribute | |
2565 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002566 // | target parallel | distribute | |
2567 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002568 // | target parallel | distribute simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002569 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002570 // | target parallel | parallel | * |
2571 // | for | | |
2572 // | target parallel | for | * |
2573 // | for | | |
2574 // | target parallel | for simd | * |
2575 // | for | | |
2576 // | target parallel | master | * |
2577 // | for | | |
2578 // | target parallel | critical | * |
2579 // | for | | |
2580 // | target parallel | simd | * |
2581 // | for | | |
2582 // | target parallel | sections | * |
2583 // | for | | |
2584 // | target parallel | section | * |
2585 // | for | | |
2586 // | target parallel | single | * |
2587 // | for | | |
2588 // | target parallel | parallel for | * |
2589 // | for | | |
2590 // | target parallel |parallel for simd| * |
2591 // | for | | |
2592 // | target parallel |parallel sections| * |
2593 // | for | | |
2594 // | target parallel | task | * |
2595 // | for | | |
2596 // | target parallel | taskyield | * |
2597 // | for | | |
2598 // | target parallel | barrier | * |
2599 // | for | | |
2600 // | target parallel | taskwait | * |
2601 // | for | | |
2602 // | target parallel | taskgroup | * |
2603 // | for | | |
2604 // | target parallel | flush | * |
2605 // | for | | |
2606 // | target parallel | ordered | * |
2607 // | for | | |
2608 // | target parallel | atomic | * |
2609 // | for | | |
2610 // | target parallel | target | |
2611 // | for | | |
2612 // | target parallel | target parallel | |
2613 // | for | | |
2614 // | target parallel | target parallel | |
2615 // | for | for | |
2616 // | target parallel | target enter | |
2617 // | for | data | |
2618 // | target parallel | target exit | |
2619 // | for | data | |
2620 // | target parallel | teams | |
2621 // | for | | |
2622 // | target parallel | cancellation | |
2623 // | for | point | ! |
2624 // | target parallel | cancel | ! |
2625 // | for | | |
2626 // | target parallel | taskloop | * |
2627 // | for | | |
2628 // | target parallel | taskloop simd | * |
2629 // | for | | |
2630 // | target parallel | distribute | |
2631 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002632 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002633 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002634 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002635 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002636 // | target parallel | distribute simd | |
2637 // | for | | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002638 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002639 // | teams | parallel | * |
2640 // | teams | for | + |
2641 // | teams | for simd | + |
2642 // | teams | master | + |
2643 // | teams | critical | + |
2644 // | teams | simd | + |
2645 // | teams | sections | + |
2646 // | teams | section | + |
2647 // | teams | single | + |
2648 // | teams | parallel for | * |
2649 // | teams |parallel for simd| * |
2650 // | teams |parallel sections| * |
2651 // | teams | task | + |
2652 // | teams | taskyield | + |
2653 // | teams | barrier | + |
2654 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002655 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002656 // | teams | flush | + |
2657 // | teams | ordered | + |
2658 // | teams | atomic | + |
2659 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002660 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002661 // | teams | target parallel | + |
2662 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002663 // | teams | target enter | + |
2664 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002665 // | teams | target exit | + |
2666 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002667 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002668 // | teams | cancellation | |
2669 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002670 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002671 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002672 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002673 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002674 // | teams | distribute | ! |
2675 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002676 // | teams | distribute | ! |
2677 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002678 // | teams | distribute simd | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002679 // +------------------+-----------------+------------------------------------+
2680 // | taskloop | parallel | * |
2681 // | taskloop | for | + |
2682 // | taskloop | for simd | + |
2683 // | taskloop | master | + |
2684 // | taskloop | critical | * |
2685 // | taskloop | simd | * |
2686 // | taskloop | sections | + |
2687 // | taskloop | section | + |
2688 // | taskloop | single | + |
2689 // | taskloop | parallel for | * |
2690 // | taskloop |parallel for simd| * |
2691 // | taskloop |parallel sections| * |
2692 // | taskloop | task | * |
2693 // | taskloop | taskyield | * |
2694 // | taskloop | barrier | + |
2695 // | taskloop | taskwait | * |
2696 // | taskloop | taskgroup | * |
2697 // | taskloop | flush | * |
2698 // | taskloop | ordered | + |
2699 // | taskloop | atomic | * |
2700 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002701 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002702 // | taskloop | target parallel | * |
2703 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002704 // | taskloop | target enter | * |
2705 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002706 // | taskloop | target exit | * |
2707 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002708 // | taskloop | teams | + |
2709 // | taskloop | cancellation | |
2710 // | | point | |
2711 // | taskloop | cancel | |
2712 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002713 // | taskloop | distribute | + |
2714 // | taskloop | distribute | + |
2715 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002716 // | taskloop | distribute | + |
2717 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002718 // | taskloop | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002719 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002720 // | taskloop simd | parallel | |
2721 // | taskloop simd | for | |
2722 // | taskloop simd | for simd | |
2723 // | taskloop simd | master | |
2724 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002725 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002726 // | taskloop simd | sections | |
2727 // | taskloop simd | section | |
2728 // | taskloop simd | single | |
2729 // | taskloop simd | parallel for | |
2730 // | taskloop simd |parallel for simd| |
2731 // | taskloop simd |parallel sections| |
2732 // | taskloop simd | task | |
2733 // | taskloop simd | taskyield | |
2734 // | taskloop simd | barrier | |
2735 // | taskloop simd | taskwait | |
2736 // | taskloop simd | taskgroup | |
2737 // | taskloop simd | flush | |
2738 // | taskloop simd | ordered | + (with simd clause) |
2739 // | taskloop simd | atomic | |
2740 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002741 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002742 // | taskloop simd | target parallel | |
2743 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002744 // | taskloop simd | target enter | |
2745 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002746 // | taskloop simd | target exit | |
2747 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002748 // | taskloop simd | teams | |
2749 // | taskloop simd | cancellation | |
2750 // | | point | |
2751 // | taskloop simd | cancel | |
2752 // | taskloop simd | taskloop | |
2753 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002754 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002755 // | taskloop simd | distribute | |
2756 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002757 // | taskloop simd | distribute | |
2758 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002759 // | taskloop simd | distribute simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002760 // +------------------+-----------------+------------------------------------+
2761 // | distribute | parallel | * |
2762 // | distribute | for | * |
2763 // | distribute | for simd | * |
2764 // | distribute | master | * |
2765 // | distribute | critical | * |
2766 // | distribute | simd | * |
2767 // | distribute | sections | * |
2768 // | distribute | section | * |
2769 // | distribute | single | * |
2770 // | distribute | parallel for | * |
2771 // | distribute |parallel for simd| * |
2772 // | distribute |parallel sections| * |
2773 // | distribute | task | * |
2774 // | distribute | taskyield | * |
2775 // | distribute | barrier | * |
2776 // | distribute | taskwait | * |
2777 // | distribute | taskgroup | * |
2778 // | distribute | flush | * |
2779 // | distribute | ordered | + |
2780 // | distribute | atomic | * |
2781 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002782 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002783 // | distribute | target parallel | |
2784 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002785 // | distribute | target enter | |
2786 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002787 // | distribute | target exit | |
2788 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002789 // | distribute | teams | |
2790 // | distribute | cancellation | + |
2791 // | | point | |
2792 // | distribute | cancel | + |
2793 // | distribute | taskloop | * |
2794 // | distribute | taskloop simd | * |
2795 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002796 // | distribute | distribute | |
2797 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002798 // | distribute | distribute | |
2799 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002800 // | distribute | distribute simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002801 // +------------------+-----------------+------------------------------------+
2802 // | distribute | parallel | * |
2803 // | parallel for | | |
2804 // | distribute | for | * |
2805 // | parallel for | | |
2806 // | distribute | for simd | * |
2807 // | parallel for | | |
2808 // | distribute | master | * |
2809 // | parallel for | | |
2810 // | distribute | critical | * |
2811 // | parallel for | | |
2812 // | distribute | simd | * |
2813 // | parallel for | | |
2814 // | distribute | sections | * |
2815 // | parallel for | | |
2816 // | distribute | section | * |
2817 // | parallel for | | |
2818 // | distribute | single | * |
2819 // | parallel for | | |
2820 // | distribute | parallel for | * |
2821 // | parallel for | | |
2822 // | distribute |parallel for simd| * |
2823 // | parallel for | | |
2824 // | distribute |parallel sections| * |
2825 // | parallel for | | |
2826 // | distribute | task | * |
2827 // | parallel for | | |
2828 // | parallel for | | |
2829 // | distribute | taskyield | * |
2830 // | parallel for | | |
2831 // | distribute | barrier | * |
2832 // | parallel for | | |
2833 // | distribute | taskwait | * |
2834 // | parallel for | | |
2835 // | distribute | taskgroup | * |
2836 // | parallel for | | |
2837 // | distribute | flush | * |
2838 // | parallel for | | |
2839 // | distribute | ordered | + |
2840 // | parallel for | | |
2841 // | distribute | atomic | * |
2842 // | parallel for | | |
2843 // | distribute | target | |
2844 // | parallel for | | |
2845 // | distribute | target parallel | |
2846 // | parallel for | | |
2847 // | distribute | target parallel | |
2848 // | parallel for | for | |
2849 // | distribute | target enter | |
2850 // | parallel for | data | |
2851 // | distribute | target exit | |
2852 // | parallel for | data | |
2853 // | distribute | teams | |
2854 // | parallel for | | |
2855 // | distribute | cancellation | + |
2856 // | parallel for | point | |
2857 // | distribute | cancel | + |
2858 // | parallel for | | |
2859 // | distribute | taskloop | * |
2860 // | parallel for | | |
2861 // | distribute | taskloop simd | * |
2862 // | parallel for | | |
2863 // | distribute | distribute | |
2864 // | parallel for | | |
2865 // | distribute | distribute | |
2866 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002867 // | distribute | distribute | |
2868 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002869 // | distribute | distribute simd | |
2870 // | parallel for | | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002871 // +------------------+-----------------+------------------------------------+
2872 // | distribute | parallel | * |
2873 // | parallel for simd| | |
2874 // | distribute | for | * |
2875 // | parallel for simd| | |
2876 // | distribute | for simd | * |
2877 // | parallel for simd| | |
2878 // | distribute | master | * |
2879 // | parallel for simd| | |
2880 // | distribute | critical | * |
2881 // | parallel for simd| | |
2882 // | distribute | simd | * |
2883 // | parallel for simd| | |
2884 // | distribute | sections | * |
2885 // | parallel for simd| | |
2886 // | distribute | section | * |
2887 // | parallel for simd| | |
2888 // | distribute | single | * |
2889 // | parallel for simd| | |
2890 // | distribute | parallel for | * |
2891 // | parallel for simd| | |
2892 // | distribute |parallel for simd| * |
2893 // | parallel for simd| | |
2894 // | distribute |parallel sections| * |
2895 // | parallel for simd| | |
2896 // | distribute | task | * |
2897 // | parallel for simd| | |
2898 // | distribute | taskyield | * |
2899 // | parallel for simd| | |
2900 // | distribute | barrier | * |
2901 // | parallel for simd| | |
2902 // | distribute | taskwait | * |
2903 // | parallel for simd| | |
2904 // | distribute | taskgroup | * |
2905 // | parallel for simd| | |
2906 // | distribute | flush | * |
2907 // | parallel for simd| | |
2908 // | distribute | ordered | + |
2909 // | parallel for simd| | |
2910 // | distribute | atomic | * |
2911 // | parallel for simd| | |
2912 // | distribute | target | |
2913 // | parallel for simd| | |
2914 // | distribute | target parallel | |
2915 // | parallel for simd| | |
2916 // | distribute | target parallel | |
2917 // | parallel for simd| for | |
2918 // | distribute | target enter | |
2919 // | parallel for simd| data | |
2920 // | distribute | target exit | |
2921 // | parallel for simd| data | |
2922 // | distribute | teams | |
2923 // | parallel for simd| | |
2924 // | distribute | cancellation | + |
2925 // | parallel for simd| point | |
2926 // | distribute | cancel | + |
2927 // | parallel for simd| | |
2928 // | distribute | taskloop | * |
2929 // | parallel for simd| | |
2930 // | distribute | taskloop simd | * |
2931 // | parallel for simd| | |
2932 // | distribute | distribute | |
2933 // | parallel for simd| | |
2934 // | distribute | distribute | * |
2935 // | parallel for simd| parallel for | |
2936 // | distribute | distribute | * |
2937 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002938 // | distribute | distribute simd | * |
2939 // | parallel for simd| | |
2940 // +------------------+-----------------+------------------------------------+
2941 // | distribute simd | parallel | * |
2942 // | distribute simd | for | * |
2943 // | distribute simd | for simd | * |
2944 // | distribute simd | master | * |
2945 // | distribute simd | critical | * |
2946 // | distribute simd | simd | * |
2947 // | distribute simd | sections | * |
2948 // | distribute simd | section | * |
2949 // | distribute simd | single | * |
2950 // | distribute simd | parallel for | * |
2951 // | distribute simd |parallel for simd| * |
2952 // | distribute simd |parallel sections| * |
2953 // | distribute simd | task | * |
2954 // | distribute simd | taskyield | * |
2955 // | distribute simd | barrier | * |
2956 // | distribute simd | taskwait | * |
2957 // | distribute simd | taskgroup | * |
2958 // | distribute simd | flush | * |
2959 // | distribute simd | ordered | + |
2960 // | distribute simd | atomic | * |
2961 // | distribute simd | target | * |
2962 // | distribute simd | target parallel | * |
2963 // | distribute simd | target parallel | * |
2964 // | | for | |
2965 // | distribute simd | target enter | * |
2966 // | | data | |
2967 // | distribute simd | target exit | * |
2968 // | | data | |
2969 // | distribute simd | teams | * |
2970 // | distribute simd | cancellation | + |
2971 // | | point | |
2972 // | distribute simd | cancel | + |
2973 // | distribute simd | taskloop | * |
2974 // | distribute simd | taskloop simd | * |
2975 // | distribute simd | distribute | |
2976 // | distribute simd | distribute | * |
2977 // | | parallel for | |
2978 // | distribute simd | distribute | * |
2979 // | |parallel for simd| |
2980 // | distribute simd | distribute simd | * |
2981 // | | | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002982 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002983 if (Stack->getCurScope()) {
2984 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002985 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002986 bool NestingProhibited = false;
2987 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002988 enum {
2989 NoRecommend,
2990 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002991 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002992 ShouldBeInTargetRegion,
2993 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002994 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002995 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002996 // OpenMP [2.16, Nesting of Regions]
2997 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002998 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002999 // An ordered construct with the simd clause is the only OpenMP
3000 // construct that can appear in the simd region.
3001 // Allowing a SIMD consruct nested in another SIMD construct is an
3002 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3003 // message.
3004 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3005 ? diag::err_omp_prohibited_region_simd
3006 : diag::warn_omp_nesting_simd);
3007 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003008 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003009 if (ParentRegion == OMPD_atomic) {
3010 // OpenMP [2.16, Nesting of Regions]
3011 // OpenMP constructs may not be nested inside an atomic region.
3012 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3013 return true;
3014 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003015 if (CurrentRegion == OMPD_section) {
3016 // OpenMP [2.7.2, sections Construct, Restrictions]
3017 // Orphaned section directives are prohibited. That is, the section
3018 // directives must appear within the sections construct and must not be
3019 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003020 if (ParentRegion != OMPD_sections &&
3021 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003022 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3023 << (ParentRegion != OMPD_unknown)
3024 << getOpenMPDirectiveName(ParentRegion);
3025 return true;
3026 }
3027 return false;
3028 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003029 // Allow some constructs to be orphaned (they could be used in functions,
3030 // called from OpenMP regions with the required preconditions).
3031 if (ParentRegion == OMPD_unknown)
3032 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003033 if (CurrentRegion == OMPD_cancellation_point ||
3034 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003035 // OpenMP [2.16, Nesting of Regions]
3036 // A cancellation point construct for which construct-type-clause is
3037 // taskgroup must be nested inside a task construct. A cancellation
3038 // point construct for which construct-type-clause is not taskgroup must
3039 // be closely nested inside an OpenMP construct that matches the type
3040 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003041 // A cancel construct for which construct-type-clause is taskgroup must be
3042 // nested inside a task construct. A cancel construct for which
3043 // construct-type-clause is not taskgroup must be closely nested inside an
3044 // OpenMP construct that matches the type specified in
3045 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003046 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003047 !((CancelRegion == OMPD_parallel &&
3048 (ParentRegion == OMPD_parallel ||
3049 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003050 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003051 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3052 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003053 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3054 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003055 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3056 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003057 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003058 // OpenMP [2.16, Nesting of Regions]
3059 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003060 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003061 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003062 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003063 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3064 // OpenMP [2.16, Nesting of Regions]
3065 // A critical region may not be nested (closely or otherwise) inside a
3066 // critical region with the same name. Note that this restriction is not
3067 // sufficient to prevent deadlock.
3068 SourceLocation PreviousCriticalLoc;
3069 bool DeadLock =
3070 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3071 OpenMPDirectiveKind K,
3072 const DeclarationNameInfo &DNI,
3073 SourceLocation Loc)
3074 ->bool {
3075 if (K == OMPD_critical &&
3076 DNI.getName() == CurrentName.getName()) {
3077 PreviousCriticalLoc = Loc;
3078 return true;
3079 } else
3080 return false;
3081 },
3082 false /* skip top directive */);
3083 if (DeadLock) {
3084 SemaRef.Diag(StartLoc,
3085 diag::err_omp_prohibited_region_critical_same_name)
3086 << CurrentName.getName();
3087 if (PreviousCriticalLoc.isValid())
3088 SemaRef.Diag(PreviousCriticalLoc,
3089 diag::note_omp_previous_critical_region);
3090 return true;
3091 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003092 } else if (CurrentRegion == OMPD_barrier) {
3093 // OpenMP [2.16, Nesting of Regions]
3094 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003095 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003096 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3097 isOpenMPTaskingDirective(ParentRegion) ||
3098 ParentRegion == OMPD_master ||
3099 ParentRegion == OMPD_critical ||
3100 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003101 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003102 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003103 // OpenMP [2.16, Nesting of Regions]
3104 // A worksharing region may not be closely nested inside a worksharing,
3105 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003106 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3107 isOpenMPTaskingDirective(ParentRegion) ||
3108 ParentRegion == OMPD_master ||
3109 ParentRegion == OMPD_critical ||
3110 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003111 Recommend = ShouldBeInParallelRegion;
3112 } else if (CurrentRegion == OMPD_ordered) {
3113 // OpenMP [2.16, Nesting of Regions]
3114 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003115 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003116 // An ordered region must be closely nested inside a loop region (or
3117 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003118 // OpenMP [2.8.1,simd Construct, Restrictions]
3119 // An ordered construct with the simd clause is the only OpenMP construct
3120 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003121 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003122 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003123 !(isOpenMPSimdDirective(ParentRegion) ||
3124 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003125 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003126 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3127 // OpenMP [2.16, Nesting of Regions]
3128 // If specified, a teams construct must be contained within a target
3129 // construct.
3130 NestingProhibited = ParentRegion != OMPD_target;
3131 Recommend = ShouldBeInTargetRegion;
3132 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3133 }
3134 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3135 // OpenMP [2.16, Nesting of Regions]
3136 // distribute, parallel, parallel sections, parallel workshare, and the
3137 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3138 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003139 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3140 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003141 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003142 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003143 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3144 // OpenMP 4.5 [2.17 Nesting of Regions]
3145 // The region associated with the distribute construct must be strictly
3146 // nested inside a teams region
3147 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3148 Recommend = ShouldBeInTeamsRegion;
3149 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003150 if (!NestingProhibited &&
3151 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3152 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3153 // OpenMP 4.5 [2.17 Nesting of Regions]
3154 // If a target, target update, target data, target enter data, or
3155 // target exit data construct is encountered during execution of a
3156 // target region, the behavior is unspecified.
3157 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003158 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3159 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003160 if (isOpenMPTargetExecutionDirective(K)) {
3161 OffendingRegion = K;
3162 return true;
3163 } else
3164 return false;
3165 },
3166 false /* don't skip top directive */);
3167 CloseNesting = false;
3168 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003169 if (NestingProhibited) {
3170 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003171 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3172 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00003173 return true;
3174 }
3175 }
3176 return false;
3177}
3178
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003179static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3180 ArrayRef<OMPClause *> Clauses,
3181 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3182 bool ErrorFound = false;
3183 unsigned NamedModifiersNumber = 0;
3184 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3185 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003186 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003187 for (const auto *C : Clauses) {
3188 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3189 // At most one if clause without a directive-name-modifier can appear on
3190 // the directive.
3191 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3192 if (FoundNameModifiers[CurNM]) {
3193 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3194 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3195 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3196 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003197 } else if (CurNM != OMPD_unknown) {
3198 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003199 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003200 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003201 FoundNameModifiers[CurNM] = IC;
3202 if (CurNM == OMPD_unknown)
3203 continue;
3204 // Check if the specified name modifier is allowed for the current
3205 // directive.
3206 // At most one if clause with the particular directive-name-modifier can
3207 // appear on the directive.
3208 bool MatchFound = false;
3209 for (auto NM : AllowedNameModifiers) {
3210 if (CurNM == NM) {
3211 MatchFound = true;
3212 break;
3213 }
3214 }
3215 if (!MatchFound) {
3216 S.Diag(IC->getNameModifierLoc(),
3217 diag::err_omp_wrong_if_directive_name_modifier)
3218 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3219 ErrorFound = true;
3220 }
3221 }
3222 }
3223 // If any if clause on the directive includes a directive-name-modifier then
3224 // all if clauses on the directive must include a directive-name-modifier.
3225 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3226 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3227 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3228 diag::err_omp_no_more_if_clause);
3229 } else {
3230 std::string Values;
3231 std::string Sep(", ");
3232 unsigned AllowedCnt = 0;
3233 unsigned TotalAllowedNum =
3234 AllowedNameModifiers.size() - NamedModifiersNumber;
3235 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3236 ++Cnt) {
3237 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3238 if (!FoundNameModifiers[NM]) {
3239 Values += "'";
3240 Values += getOpenMPDirectiveName(NM);
3241 Values += "'";
3242 if (AllowedCnt + 2 == TotalAllowedNum)
3243 Values += " or ";
3244 else if (AllowedCnt + 1 != TotalAllowedNum)
3245 Values += Sep;
3246 ++AllowedCnt;
3247 }
3248 }
3249 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3250 diag::err_omp_unnamed_if_clause)
3251 << (TotalAllowedNum > 1) << Values;
3252 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003253 for (auto Loc : NameModifierLoc) {
3254 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3255 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003256 ErrorFound = true;
3257 }
3258 return ErrorFound;
3259}
3260
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003261StmtResult Sema::ActOnOpenMPExecutableDirective(
3262 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3263 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3264 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003265 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003266 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3267 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003268 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003269
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003270 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003271 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003272 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003273 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003274 if (AStmt) {
3275 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3276
3277 // Check default data sharing attributes for referenced variables.
3278 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3279 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3280 if (DSAChecker.isErrorFound())
3281 return StmtError();
3282 // Generate list of implicitly defined firstprivate variables.
3283 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003284
3285 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3286 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3287 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3288 SourceLocation(), SourceLocation())) {
3289 ClausesWithImplicit.push_back(Implicit);
3290 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3291 DSAChecker.getImplicitFirstprivate().size();
3292 } else
3293 ErrorFound = true;
3294 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003295 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003296
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003297 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003298 switch (Kind) {
3299 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003300 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3301 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003302 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003303 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003304 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003305 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3306 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003307 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003308 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003309 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3310 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003311 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003312 case OMPD_for_simd:
3313 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3314 EndLoc, VarsWithInheritedDSA);
3315 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003316 case OMPD_sections:
3317 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3318 EndLoc);
3319 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003320 case OMPD_section:
3321 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003322 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003323 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3324 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003325 case OMPD_single:
3326 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3327 EndLoc);
3328 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003329 case OMPD_master:
3330 assert(ClausesWithImplicit.empty() &&
3331 "No clauses are allowed for 'omp master' directive");
3332 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3333 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003334 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003335 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3336 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003337 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003338 case OMPD_parallel_for:
3339 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3340 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003341 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003342 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003343 case OMPD_parallel_for_simd:
3344 Res = ActOnOpenMPParallelForSimdDirective(
3345 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003346 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003347 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003348 case OMPD_parallel_sections:
3349 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3350 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003351 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003352 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003353 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003354 Res =
3355 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003356 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003357 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003358 case OMPD_taskyield:
3359 assert(ClausesWithImplicit.empty() &&
3360 "No clauses are allowed for 'omp taskyield' directive");
3361 assert(AStmt == nullptr &&
3362 "No associated statement allowed for 'omp taskyield' directive");
3363 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3364 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003365 case OMPD_barrier:
3366 assert(ClausesWithImplicit.empty() &&
3367 "No clauses are allowed for 'omp barrier' directive");
3368 assert(AStmt == nullptr &&
3369 "No associated statement allowed for 'omp barrier' directive");
3370 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3371 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003372 case OMPD_taskwait:
3373 assert(ClausesWithImplicit.empty() &&
3374 "No clauses are allowed for 'omp taskwait' directive");
3375 assert(AStmt == nullptr &&
3376 "No associated statement allowed for 'omp taskwait' directive");
3377 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3378 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003379 case OMPD_taskgroup:
3380 assert(ClausesWithImplicit.empty() &&
3381 "No clauses are allowed for 'omp taskgroup' directive");
3382 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3383 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003384 case OMPD_flush:
3385 assert(AStmt == nullptr &&
3386 "No associated statement allowed for 'omp flush' directive");
3387 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3388 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003389 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003390 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3391 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003392 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003393 case OMPD_atomic:
3394 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3395 EndLoc);
3396 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003397 case OMPD_teams:
3398 Res =
3399 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3400 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003401 case OMPD_target:
3402 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3403 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003404 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003405 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003406 case OMPD_target_parallel:
3407 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3408 StartLoc, EndLoc);
3409 AllowedNameModifiers.push_back(OMPD_target);
3410 AllowedNameModifiers.push_back(OMPD_parallel);
3411 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003412 case OMPD_target_parallel_for:
3413 Res = ActOnOpenMPTargetParallelForDirective(
3414 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3415 AllowedNameModifiers.push_back(OMPD_target);
3416 AllowedNameModifiers.push_back(OMPD_parallel);
3417 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003418 case OMPD_cancellation_point:
3419 assert(ClausesWithImplicit.empty() &&
3420 "No clauses are allowed for 'omp cancellation point' directive");
3421 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3422 "cancellation point' directive");
3423 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3424 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003425 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003426 assert(AStmt == nullptr &&
3427 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003428 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3429 CancelRegion);
3430 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003431 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003432 case OMPD_target_data:
3433 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3434 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003435 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003436 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003437 case OMPD_target_enter_data:
3438 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3439 EndLoc);
3440 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3441 break;
Samuel Antao72590762016-01-19 20:04:50 +00003442 case OMPD_target_exit_data:
3443 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3444 EndLoc);
3445 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3446 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003447 case OMPD_taskloop:
3448 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3449 EndLoc, VarsWithInheritedDSA);
3450 AllowedNameModifiers.push_back(OMPD_taskloop);
3451 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003452 case OMPD_taskloop_simd:
3453 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3454 EndLoc, VarsWithInheritedDSA);
3455 AllowedNameModifiers.push_back(OMPD_taskloop);
3456 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003457 case OMPD_distribute:
3458 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3459 EndLoc, VarsWithInheritedDSA);
3460 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003461 case OMPD_target_update:
3462 assert(!AStmt && "Statement is not allowed for target update");
3463 Res =
3464 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3465 AllowedNameModifiers.push_back(OMPD_target_update);
3466 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003467 case OMPD_distribute_parallel_for:
3468 Res = ActOnOpenMPDistributeParallelForDirective(
3469 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3470 AllowedNameModifiers.push_back(OMPD_parallel);
3471 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003472 case OMPD_distribute_parallel_for_simd:
3473 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3474 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3475 AllowedNameModifiers.push_back(OMPD_parallel);
3476 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003477 case OMPD_distribute_simd:
3478 Res = ActOnOpenMPDistributeSimdDirective(
3479 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3480 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003481 case OMPD_declare_target:
3482 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003483 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003484 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003485 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003486 llvm_unreachable("OpenMP Directive is not allowed");
3487 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003488 llvm_unreachable("Unknown OpenMP directive");
3489 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003490
Alexey Bataev4acb8592014-07-07 13:01:15 +00003491 for (auto P : VarsWithInheritedDSA) {
3492 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3493 << P.first << P.second->getSourceRange();
3494 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003495 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3496
3497 if (!AllowedNameModifiers.empty())
3498 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3499 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003500
Alexey Bataeved09d242014-05-28 05:53:51 +00003501 if (ErrorFound)
3502 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003503 return Res;
3504}
3505
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003506Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3507 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003508 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003509 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3510 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003511 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003512 assert(Linears.size() == LinModifiers.size());
3513 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003514 if (!DG || DG.get().isNull())
3515 return DeclGroupPtrTy();
3516
3517 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003518 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003519 return DG;
3520 }
3521 auto *ADecl = DG.get().getSingleDecl();
3522 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3523 ADecl = FTD->getTemplatedDecl();
3524
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003525 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3526 if (!FD) {
3527 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003528 return DeclGroupPtrTy();
3529 }
3530
Alexey Bataev2af33e32016-04-07 12:45:37 +00003531 // OpenMP [2.8.2, declare simd construct, Description]
3532 // The parameter of the simdlen clause must be a constant positive integer
3533 // expression.
3534 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003535 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003536 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003537 // OpenMP [2.8.2, declare simd construct, Description]
3538 // The special this pointer can be used as if was one of the arguments to the
3539 // function in any of the linear, aligned, or uniform clauses.
3540 // The uniform clause declares one or more arguments to have an invariant
3541 // value for all concurrent invocations of the function in the execution of a
3542 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003543 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3544 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003545 for (auto *E : Uniforms) {
3546 E = E->IgnoreParenImpCasts();
3547 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3548 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3549 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3550 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003551 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3552 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003553 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003554 }
3555 if (isa<CXXThisExpr>(E)) {
3556 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003557 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003558 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003559 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3560 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003561 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003562 // OpenMP [2.8.2, declare simd construct, Description]
3563 // The aligned clause declares that the object to which each list item points
3564 // is aligned to the number of bytes expressed in the optional parameter of
3565 // the aligned clause.
3566 // The special this pointer can be used as if was one of the arguments to the
3567 // function in any of the linear, aligned, or uniform clauses.
3568 // The type of list items appearing in the aligned clause must be array,
3569 // pointer, reference to array, or reference to pointer.
3570 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3571 Expr *AlignedThis = nullptr;
3572 for (auto *E : Aligneds) {
3573 E = E->IgnoreParenImpCasts();
3574 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3575 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3576 auto *CanonPVD = PVD->getCanonicalDecl();
3577 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3578 FD->getParamDecl(PVD->getFunctionScopeIndex())
3579 ->getCanonicalDecl() == CanonPVD) {
3580 // OpenMP [2.8.1, simd construct, Restrictions]
3581 // A list-item cannot appear in more than one aligned clause.
3582 if (AlignedArgs.count(CanonPVD) > 0) {
3583 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3584 << 1 << E->getSourceRange();
3585 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3586 diag::note_omp_explicit_dsa)
3587 << getOpenMPClauseName(OMPC_aligned);
3588 continue;
3589 }
3590 AlignedArgs[CanonPVD] = E;
3591 QualType QTy = PVD->getType()
3592 .getNonReferenceType()
3593 .getUnqualifiedType()
3594 .getCanonicalType();
3595 const Type *Ty = QTy.getTypePtrOrNull();
3596 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3597 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3598 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3599 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3600 }
3601 continue;
3602 }
3603 }
3604 if (isa<CXXThisExpr>(E)) {
3605 if (AlignedThis) {
3606 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3607 << 2 << E->getSourceRange();
3608 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3609 << getOpenMPClauseName(OMPC_aligned);
3610 }
3611 AlignedThis = E;
3612 continue;
3613 }
3614 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3615 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3616 }
3617 // The optional parameter of the aligned clause, alignment, must be a constant
3618 // positive integer expression. If no optional parameter is specified,
3619 // implementation-defined default alignments for SIMD instructions on the
3620 // target platforms are assumed.
3621 SmallVector<Expr *, 4> NewAligns;
3622 for (auto *E : Alignments) {
3623 ExprResult Align;
3624 if (E)
3625 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3626 NewAligns.push_back(Align.get());
3627 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003628 // OpenMP [2.8.2, declare simd construct, Description]
3629 // The linear clause declares one or more list items to be private to a SIMD
3630 // lane and to have a linear relationship with respect to the iteration space
3631 // of a loop.
3632 // The special this pointer can be used as if was one of the arguments to the
3633 // function in any of the linear, aligned, or uniform clauses.
3634 // When a linear-step expression is specified in a linear clause it must be
3635 // either a constant integer expression or an integer-typed parameter that is
3636 // specified in a uniform clause on the directive.
3637 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3638 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3639 auto MI = LinModifiers.begin();
3640 for (auto *E : Linears) {
3641 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3642 ++MI;
3643 E = E->IgnoreParenImpCasts();
3644 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3645 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3646 auto *CanonPVD = PVD->getCanonicalDecl();
3647 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3648 FD->getParamDecl(PVD->getFunctionScopeIndex())
3649 ->getCanonicalDecl() == CanonPVD) {
3650 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3651 // A list-item cannot appear in more than one linear clause.
3652 if (LinearArgs.count(CanonPVD) > 0) {
3653 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3654 << getOpenMPClauseName(OMPC_linear)
3655 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3656 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3657 diag::note_omp_explicit_dsa)
3658 << getOpenMPClauseName(OMPC_linear);
3659 continue;
3660 }
3661 // Each argument can appear in at most one uniform or linear clause.
3662 if (UniformedArgs.count(CanonPVD) > 0) {
3663 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3664 << getOpenMPClauseName(OMPC_linear)
3665 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3666 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3667 diag::note_omp_explicit_dsa)
3668 << getOpenMPClauseName(OMPC_uniform);
3669 continue;
3670 }
3671 LinearArgs[CanonPVD] = E;
3672 if (E->isValueDependent() || E->isTypeDependent() ||
3673 E->isInstantiationDependent() ||
3674 E->containsUnexpandedParameterPack())
3675 continue;
3676 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3677 PVD->getOriginalType());
3678 continue;
3679 }
3680 }
3681 if (isa<CXXThisExpr>(E)) {
3682 if (UniformedLinearThis) {
3683 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3684 << getOpenMPClauseName(OMPC_linear)
3685 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3686 << E->getSourceRange();
3687 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3688 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3689 : OMPC_linear);
3690 continue;
3691 }
3692 UniformedLinearThis = E;
3693 if (E->isValueDependent() || E->isTypeDependent() ||
3694 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3695 continue;
3696 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3697 E->getType());
3698 continue;
3699 }
3700 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3701 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3702 }
3703 Expr *Step = nullptr;
3704 Expr *NewStep = nullptr;
3705 SmallVector<Expr *, 4> NewSteps;
3706 for (auto *E : Steps) {
3707 // Skip the same step expression, it was checked already.
3708 if (Step == E || !E) {
3709 NewSteps.push_back(E ? NewStep : nullptr);
3710 continue;
3711 }
3712 Step = E;
3713 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3714 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3715 auto *CanonPVD = PVD->getCanonicalDecl();
3716 if (UniformedArgs.count(CanonPVD) == 0) {
3717 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3718 << Step->getSourceRange();
3719 } else if (E->isValueDependent() || E->isTypeDependent() ||
3720 E->isInstantiationDependent() ||
3721 E->containsUnexpandedParameterPack() ||
3722 CanonPVD->getType()->hasIntegerRepresentation())
3723 NewSteps.push_back(Step);
3724 else {
3725 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3726 << Step->getSourceRange();
3727 }
3728 continue;
3729 }
3730 NewStep = Step;
3731 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3732 !Step->isInstantiationDependent() &&
3733 !Step->containsUnexpandedParameterPack()) {
3734 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3735 .get();
3736 if (NewStep)
3737 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3738 }
3739 NewSteps.push_back(NewStep);
3740 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003741 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3742 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003743 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003744 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3745 const_cast<Expr **>(Linears.data()), Linears.size(),
3746 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3747 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003748 ADecl->addAttr(NewAttr);
3749 return ConvertDeclToDeclGroup(ADecl);
3750}
3751
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003752StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3753 Stmt *AStmt,
3754 SourceLocation StartLoc,
3755 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003756 if (!AStmt)
3757 return StmtError();
3758
Alexey Bataev9959db52014-05-06 10:08:46 +00003759 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3760 // 1.2.2 OpenMP Language Terminology
3761 // Structured block - An executable statement with a single entry at the
3762 // top and a single exit at the bottom.
3763 // The point of exit cannot be a branch out of the structured block.
3764 // longjmp() and throw() must not violate the entry/exit criteria.
3765 CS->getCapturedDecl()->setNothrow();
3766
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003767 getCurFunction()->setHasBranchProtectedScope();
3768
Alexey Bataev25e5b442015-09-15 12:52:43 +00003769 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3770 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003771}
3772
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003773namespace {
3774/// \brief Helper class for checking canonical form of the OpenMP loops and
3775/// extracting iteration space of each loop in the loop nest, that will be used
3776/// for IR generation.
3777class OpenMPIterationSpaceChecker {
3778 /// \brief Reference to Sema.
3779 Sema &SemaRef;
3780 /// \brief A location for diagnostics (when there is no some better location).
3781 SourceLocation DefaultLoc;
3782 /// \brief A location for diagnostics (when increment is not compatible).
3783 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003784 /// \brief A source location for referring to loop init later.
3785 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003786 /// \brief A source location for referring to condition later.
3787 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003788 /// \brief A source location for referring to increment later.
3789 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003790 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003791 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003792 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003793 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003794 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003795 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003796 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003797 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003798 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003799 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003800 /// \brief This flag is true when condition is one of:
3801 /// Var < UB
3802 /// Var <= UB
3803 /// UB > Var
3804 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003805 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003806 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003807 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003808 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003809 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003810
3811public:
3812 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003813 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003814 /// \brief Check init-expr for canonical loop form and save loop counter
3815 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003816 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003817 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3818 /// for less/greater and for strict/non-strict comparison.
3819 bool CheckCond(Expr *S);
3820 /// \brief Check incr-expr for canonical loop form and return true if it
3821 /// does not conform, otherwise save loop step (#Step).
3822 bool CheckInc(Expr *S);
3823 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003824 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003825 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003826 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003827 /// \brief Source range of the loop init.
3828 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3829 /// \brief Source range of the loop condition.
3830 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3831 /// \brief Source range of the loop increment.
3832 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3833 /// \brief True if the step should be subtracted.
3834 bool ShouldSubtractStep() const { return SubtractStep; }
3835 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003836 Expr *
3837 BuildNumIterations(Scope *S, const bool LimitedType,
3838 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003839 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003840 Expr *BuildPreCond(Scope *S, Expr *Cond,
3841 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003842 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003843 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3844 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003845 /// \brief Build reference expression to the private counter be used for
3846 /// codegen.
3847 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003848 /// \brief Build initization of the counter be used for codegen.
3849 Expr *BuildCounterInit() const;
3850 /// \brief Build step of the counter be used for codegen.
3851 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003852 /// \brief Return true if any expression is dependent.
3853 bool Dependent() const;
3854
3855private:
3856 /// \brief Check the right-hand side of an assignment in the increment
3857 /// expression.
3858 bool CheckIncRHS(Expr *RHS);
3859 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003860 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003861 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003862 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003863 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003864 /// \brief Helper to set loop increment.
3865 bool SetStep(Expr *NewStep, bool Subtract);
3866};
3867
3868bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003869 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003870 assert(!LB && !UB && !Step);
3871 return false;
3872 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003873 return LCDecl->getType()->isDependentType() ||
3874 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3875 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003876}
3877
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003878static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003879 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3880 E = ExprTemp->getSubExpr();
3881
3882 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3883 E = MTE->GetTemporaryExpr();
3884
3885 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3886 E = Binder->getSubExpr();
3887
3888 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3889 E = ICE->getSubExprAsWritten();
3890 return E->IgnoreParens();
3891}
3892
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003893bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3894 Expr *NewLCRefExpr,
3895 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003896 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003897 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003898 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003899 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003900 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003901 LCDecl = getCanonicalDecl(NewLCDecl);
3902 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003903 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3904 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003905 if ((Ctor->isCopyOrMoveConstructor() ||
3906 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3907 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003908 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003909 LB = NewLB;
3910 return false;
3911}
3912
3913bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003914 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003915 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003916 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3917 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 if (!NewUB)
3919 return true;
3920 UB = NewUB;
3921 TestIsLessOp = LessOp;
3922 TestIsStrictOp = StrictOp;
3923 ConditionSrcRange = SR;
3924 ConditionLoc = SL;
3925 return false;
3926}
3927
3928bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3929 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003930 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003931 if (!NewStep)
3932 return true;
3933 if (!NewStep->isValueDependent()) {
3934 // Check that the step is integer expression.
3935 SourceLocation StepLoc = NewStep->getLocStart();
3936 ExprResult Val =
3937 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3938 if (Val.isInvalid())
3939 return true;
3940 NewStep = Val.get();
3941
3942 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3943 // If test-expr is of form var relational-op b and relational-op is < or
3944 // <= then incr-expr must cause var to increase on each iteration of the
3945 // loop. If test-expr is of form var relational-op b and relational-op is
3946 // > or >= then incr-expr must cause var to decrease on each iteration of
3947 // the loop.
3948 // If test-expr is of form b relational-op var and relational-op is < or
3949 // <= then incr-expr must cause var to decrease on each iteration of the
3950 // loop. If test-expr is of form b relational-op var and relational-op is
3951 // > or >= then incr-expr must cause var to increase on each iteration of
3952 // the loop.
3953 llvm::APSInt Result;
3954 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3955 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3956 bool IsConstNeg =
3957 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003958 bool IsConstPos =
3959 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003960 bool IsConstZero = IsConstant && !Result.getBoolValue();
3961 if (UB && (IsConstZero ||
3962 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003963 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003964 SemaRef.Diag(NewStep->getExprLoc(),
3965 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003966 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003967 SemaRef.Diag(ConditionLoc,
3968 diag::note_omp_loop_cond_requres_compatible_incr)
3969 << TestIsLessOp << ConditionSrcRange;
3970 return true;
3971 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003972 if (TestIsLessOp == Subtract) {
3973 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3974 NewStep).get();
3975 Subtract = !Subtract;
3976 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003977 }
3978
3979 Step = NewStep;
3980 SubtractStep = Subtract;
3981 return false;
3982}
3983
Alexey Bataev9c821032015-04-30 04:23:23 +00003984bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003985 // Check init-expr for canonical loop form and save loop counter
3986 // variable - #Var and its initialization value - #LB.
3987 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3988 // var = lb
3989 // integer-type var = lb
3990 // random-access-iterator-type var = lb
3991 // pointer-type var = lb
3992 //
3993 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003994 if (EmitDiags) {
3995 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3996 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003997 return true;
3998 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003999 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4000 if (!ExprTemp->cleanupsHaveSideEffects())
4001 S = ExprTemp->getSubExpr();
4002
Alexander Musmana5f070a2014-10-01 06:03:56 +00004003 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004004 if (Expr *E = dyn_cast<Expr>(S))
4005 S = E->IgnoreParens();
4006 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004007 if (BO->getOpcode() == BO_Assign) {
4008 auto *LHS = BO->getLHS()->IgnoreParens();
4009 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4010 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4011 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4012 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4013 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4014 }
4015 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4016 if (ME->isArrow() &&
4017 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4018 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4019 }
4020 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004021 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4022 if (DS->isSingleDecl()) {
4023 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004024 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004025 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004026 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004027 SemaRef.Diag(S->getLocStart(),
4028 diag::ext_omp_loop_not_canonical_init)
4029 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004030 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004031 }
4032 }
4033 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004034 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4035 if (CE->getOperator() == OO_Equal) {
4036 auto *LHS = CE->getArg(0);
4037 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4038 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4039 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4040 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4041 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4042 }
4043 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4044 if (ME->isArrow() &&
4045 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4046 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4047 }
4048 }
4049 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004050
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004051 if (Dependent() || SemaRef.CurContext->isDependentContext())
4052 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004053 if (EmitDiags) {
4054 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4055 << S->getSourceRange();
4056 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004057 return true;
4058}
4059
Alexey Bataev23b69422014-06-18 07:08:49 +00004060/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004061/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004062static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004063 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004064 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004065 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004066 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4067 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004068 if ((Ctor->isCopyOrMoveConstructor() ||
4069 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4070 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004071 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004072 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4073 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4074 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4075 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4076 return getCanonicalDecl(ME->getMemberDecl());
4077 return getCanonicalDecl(VD);
4078 }
4079 }
4080 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4081 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4082 return getCanonicalDecl(ME->getMemberDecl());
4083 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004084}
4085
4086bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4087 // Check test-expr for canonical form, save upper-bound UB, flags for
4088 // less/greater and for strict/non-strict comparison.
4089 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4090 // var relational-op b
4091 // b relational-op var
4092 //
4093 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004094 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004095 return true;
4096 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004097 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004098 SourceLocation CondLoc = S->getLocStart();
4099 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4100 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004101 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004102 return SetUB(BO->getRHS(),
4103 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4104 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4105 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004106 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004107 return SetUB(BO->getLHS(),
4108 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4109 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4110 BO->getSourceRange(), BO->getOperatorLoc());
4111 }
4112 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4113 if (CE->getNumArgs() == 2) {
4114 auto Op = CE->getOperator();
4115 switch (Op) {
4116 case OO_Greater:
4117 case OO_GreaterEqual:
4118 case OO_Less:
4119 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004120 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004121 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4122 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4123 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004124 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004125 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4126 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4127 CE->getOperatorLoc());
4128 break;
4129 default:
4130 break;
4131 }
4132 }
4133 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004134 if (Dependent() || SemaRef.CurContext->isDependentContext())
4135 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004136 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004137 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004138 return true;
4139}
4140
4141bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4142 // RHS of canonical loop form increment can be:
4143 // var + incr
4144 // incr + var
4145 // var - incr
4146 //
4147 RHS = RHS->IgnoreParenImpCasts();
4148 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4149 if (BO->isAdditiveOp()) {
4150 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004151 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004152 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004153 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004154 return SetStep(BO->getLHS(), false);
4155 }
4156 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4157 bool IsAdd = CE->getOperator() == OO_Plus;
4158 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004159 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004160 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004161 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004162 return SetStep(CE->getArg(0), false);
4163 }
4164 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004165 if (Dependent() || SemaRef.CurContext->isDependentContext())
4166 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004167 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004168 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004169 return true;
4170}
4171
4172bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4173 // Check incr-expr for canonical loop form and return true if it
4174 // does not conform.
4175 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4176 // ++var
4177 // var++
4178 // --var
4179 // var--
4180 // var += incr
4181 // var -= incr
4182 // var = var + incr
4183 // var = incr + var
4184 // var = var - incr
4185 //
4186 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004187 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004188 return true;
4189 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004190 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4191 if (!ExprTemp->cleanupsHaveSideEffects())
4192 S = ExprTemp->getSubExpr();
4193
Alexander Musmana5f070a2014-10-01 06:03:56 +00004194 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 S = S->IgnoreParens();
4196 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004197 if (UO->isIncrementDecrementOp() &&
4198 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004199 return SetStep(
4200 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4201 (UO->isDecrementOp() ? -1 : 1)).get(),
4202 false);
4203 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4204 switch (BO->getOpcode()) {
4205 case BO_AddAssign:
4206 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004207 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004208 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4209 break;
4210 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004211 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004212 return CheckIncRHS(BO->getRHS());
4213 break;
4214 default:
4215 break;
4216 }
4217 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4218 switch (CE->getOperator()) {
4219 case OO_PlusPlus:
4220 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004221 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004222 return SetStep(
4223 SemaRef.ActOnIntegerConstant(
4224 CE->getLocStart(),
4225 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4226 false);
4227 break;
4228 case OO_PlusEqual:
4229 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004230 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004231 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4232 break;
4233 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004234 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004235 return CheckIncRHS(CE->getArg(1));
4236 break;
4237 default:
4238 break;
4239 }
4240 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004241 if (Dependent() || SemaRef.CurContext->isDependentContext())
4242 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004243 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004244 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004245 return true;
4246}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004247
Alexey Bataev5a3af132016-03-29 08:58:54 +00004248static ExprResult
4249tryBuildCapture(Sema &SemaRef, Expr *Capture,
4250 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004251 if (SemaRef.CurContext->isDependentContext())
4252 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004253 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4254 return SemaRef.PerformImplicitConversion(
4255 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4256 /*AllowExplicit=*/true);
4257 auto I = Captures.find(Capture);
4258 if (I != Captures.end())
4259 return buildCapture(SemaRef, Capture, I->second);
4260 DeclRefExpr *Ref = nullptr;
4261 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4262 Captures[Capture] = Ref;
4263 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004264}
4265
Alexander Musmana5f070a2014-10-01 06:03:56 +00004266/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004267Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4268 Scope *S, const bool LimitedType,
4269 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004270 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004271 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004272 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004273 SemaRef.getLangOpts().CPlusPlus) {
4274 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004275 auto *UBExpr = TestIsLessOp ? UB : LB;
4276 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004277 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4278 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004279 if (!Upper || !Lower)
4280 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004281
4282 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4283
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004284 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004285 // BuildBinOp already emitted error, this one is to point user to upper
4286 // and lower bound, and to tell what is passed to 'operator-'.
4287 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4288 << Upper->getSourceRange() << Lower->getSourceRange();
4289 return nullptr;
4290 }
4291 }
4292
4293 if (!Diff.isUsable())
4294 return nullptr;
4295
4296 // Upper - Lower [- 1]
4297 if (TestIsStrictOp)
4298 Diff = SemaRef.BuildBinOp(
4299 S, DefaultLoc, BO_Sub, Diff.get(),
4300 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4301 if (!Diff.isUsable())
4302 return nullptr;
4303
4304 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004305 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4306 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004307 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004308 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004309 if (!Diff.isUsable())
4310 return nullptr;
4311
4312 // Parentheses (for dumping/debugging purposes only).
4313 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4314 if (!Diff.isUsable())
4315 return nullptr;
4316
4317 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004318 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004319 if (!Diff.isUsable())
4320 return nullptr;
4321
Alexander Musman174b3ca2014-10-06 11:16:29 +00004322 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004323 QualType Type = Diff.get()->getType();
4324 auto &C = SemaRef.Context;
4325 bool UseVarType = VarType->hasIntegerRepresentation() &&
4326 C.getTypeSize(Type) > C.getTypeSize(VarType);
4327 if (!Type->isIntegerType() || UseVarType) {
4328 unsigned NewSize =
4329 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4330 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4331 : Type->hasSignedIntegerRepresentation();
4332 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004333 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4334 Diff = SemaRef.PerformImplicitConversion(
4335 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4336 if (!Diff.isUsable())
4337 return nullptr;
4338 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004339 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004340 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004341 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4342 if (NewSize != C.getTypeSize(Type)) {
4343 if (NewSize < C.getTypeSize(Type)) {
4344 assert(NewSize == 64 && "incorrect loop var size");
4345 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4346 << InitSrcRange << ConditionSrcRange;
4347 }
4348 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004349 NewSize, Type->hasSignedIntegerRepresentation() ||
4350 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004351 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4352 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4353 Sema::AA_Converting, true);
4354 if (!Diff.isUsable())
4355 return nullptr;
4356 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004357 }
4358 }
4359
Alexander Musmana5f070a2014-10-01 06:03:56 +00004360 return Diff.get();
4361}
4362
Alexey Bataev5a3af132016-03-29 08:58:54 +00004363Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4364 Scope *S, Expr *Cond,
4365 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004366 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4367 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4368 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004369
Alexey Bataev5a3af132016-03-29 08:58:54 +00004370 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4371 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4372 if (!NewLB.isUsable() || !NewUB.isUsable())
4373 return nullptr;
4374
Alexey Bataev62dbb972015-04-22 11:59:37 +00004375 auto CondExpr = SemaRef.BuildBinOp(
4376 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4377 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004378 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004379 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004380 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4381 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004382 CondExpr = SemaRef.PerformImplicitConversion(
4383 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4384 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004385 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004386 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4387 // Otherwise use original loop conditon and evaluate it in runtime.
4388 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4389}
4390
Alexander Musmana5f070a2014-10-01 06:03:56 +00004391/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004392DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004393 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004394 auto *VD = dyn_cast<VarDecl>(LCDecl);
4395 if (!VD) {
4396 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4397 auto *Ref = buildDeclRefExpr(
4398 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004399 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4400 // If the loop control decl is explicitly marked as private, do not mark it
4401 // as captured again.
4402 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4403 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004404 return Ref;
4405 }
4406 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004407 DefaultLoc);
4408}
4409
4410Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004411 if (LCDecl && !LCDecl->isInvalidDecl()) {
4412 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004413 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004414 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4415 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004416 if (PrivateVar->isInvalidDecl())
4417 return nullptr;
4418 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4419 }
4420 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004421}
4422
4423/// \brief Build initization of the counter be used for codegen.
4424Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4425
4426/// \brief Build step of the counter be used for codegen.
4427Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4428
4429/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004430struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004431 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004432 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004433 /// \brief This expression calculates the number of iterations in the loop.
4434 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004435 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004436 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004437 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004438 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004439 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004440 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004441 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004442 /// \brief This is step for the #CounterVar used to generate its update:
4443 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004444 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004445 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004446 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004447 /// \brief Source range of the loop init.
4448 SourceRange InitSrcRange;
4449 /// \brief Source range of the loop condition.
4450 SourceRange CondSrcRange;
4451 /// \brief Source range of the loop increment.
4452 SourceRange IncSrcRange;
4453};
4454
Alexey Bataev23b69422014-06-18 07:08:49 +00004455} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004456
Alexey Bataev9c821032015-04-30 04:23:23 +00004457void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4458 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4459 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004460 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4461 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004462 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4463 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004464 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4465 if (auto *D = ISC.GetLoopDecl()) {
4466 auto *VD = dyn_cast<VarDecl>(D);
4467 if (!VD) {
4468 if (auto *Private = IsOpenMPCapturedDecl(D))
4469 VD = Private;
4470 else {
4471 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4472 /*WithInit=*/false);
4473 VD = cast<VarDecl>(Ref->getDecl());
4474 }
4475 }
4476 DSAStack->addLoopControlVariable(D, VD);
4477 }
4478 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004479 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004480 }
4481}
4482
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004483/// \brief Called on a for stmt to check and extract its iteration space
4484/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004485static bool CheckOpenMPIterationSpace(
4486 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4487 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004488 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004489 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004490 LoopIterationSpace &ResultIterSpace,
4491 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004492 // OpenMP [2.6, Canonical Loop Form]
4493 // for (init-expr; test-expr; incr-expr) structured-block
4494 auto For = dyn_cast_or_null<ForStmt>(S);
4495 if (!For) {
4496 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004497 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4498 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4499 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4500 if (NestedLoopCount > 1) {
4501 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4502 SemaRef.Diag(DSA.getConstructLoc(),
4503 diag::note_omp_collapse_ordered_expr)
4504 << 2 << CollapseLoopCountExpr->getSourceRange()
4505 << OrderedLoopCountExpr->getSourceRange();
4506 else if (CollapseLoopCountExpr)
4507 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4508 diag::note_omp_collapse_ordered_expr)
4509 << 0 << CollapseLoopCountExpr->getSourceRange();
4510 else
4511 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4512 diag::note_omp_collapse_ordered_expr)
4513 << 1 << OrderedLoopCountExpr->getSourceRange();
4514 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004515 return true;
4516 }
4517 assert(For->getBody());
4518
4519 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4520
4521 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004522 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004523 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004524 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004525
4526 bool HasErrors = false;
4527
4528 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004529 if (auto *LCDecl = ISC.GetLoopDecl()) {
4530 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004531
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004532 // OpenMP [2.6, Canonical Loop Form]
4533 // Var is one of the following:
4534 // A variable of signed or unsigned integer type.
4535 // For C++, a variable of a random access iterator type.
4536 // For C, a variable of a pointer type.
4537 auto VarType = LCDecl->getType().getNonReferenceType();
4538 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4539 !VarType->isPointerType() &&
4540 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4541 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4542 << SemaRef.getLangOpts().CPlusPlus;
4543 HasErrors = true;
4544 }
4545
4546 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4547 // a Construct
4548 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4549 // parallel for construct is (are) private.
4550 // The loop iteration variable in the associated for-loop of a simd
4551 // construct with just one associated for-loop is linear with a
4552 // constant-linear-step that is the increment of the associated for-loop.
4553 // Exclude loop var from the list of variables with implicitly defined data
4554 // sharing attributes.
4555 VarsWithImplicitDSA.erase(LCDecl);
4556
4557 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4558 // in a Construct, C/C++].
4559 // The loop iteration variable in the associated for-loop of a simd
4560 // construct with just one associated for-loop may be listed in a linear
4561 // clause with a constant-linear-step that is the increment of the
4562 // associated for-loop.
4563 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4564 // parallel for construct may be listed in a private or lastprivate clause.
4565 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4566 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4567 // declared in the loop and it is predetermined as a private.
4568 auto PredeterminedCKind =
4569 isOpenMPSimdDirective(DKind)
4570 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4571 : OMPC_private;
4572 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4573 DVar.CKind != PredeterminedCKind) ||
4574 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4575 isOpenMPDistributeDirective(DKind)) &&
4576 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4577 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4578 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4579 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4580 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4581 << getOpenMPClauseName(PredeterminedCKind);
4582 if (DVar.RefExpr == nullptr)
4583 DVar.CKind = PredeterminedCKind;
4584 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4585 HasErrors = true;
4586 } else if (LoopDeclRefExpr != nullptr) {
4587 // Make the loop iteration variable private (for worksharing constructs),
4588 // linear (for simd directives with the only one associated loop) or
4589 // lastprivate (for simd directives with several collapsed or ordered
4590 // loops).
4591 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004592 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4593 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004594 /*FromParent=*/false);
4595 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4596 }
4597
4598 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4599
4600 // Check test-expr.
4601 HasErrors |= ISC.CheckCond(For->getCond());
4602
4603 // Check incr-expr.
4604 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004605 }
4606
Alexander Musmana5f070a2014-10-01 06:03:56 +00004607 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004608 return HasErrors;
4609
Alexander Musmana5f070a2014-10-01 06:03:56 +00004610 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004611 ResultIterSpace.PreCond =
4612 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004613 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004614 DSA.getCurScope(),
4615 (isOpenMPWorksharingDirective(DKind) ||
4616 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4617 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004618 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004619 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004620 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4621 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4622 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4623 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4624 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4625 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4626
Alexey Bataev62dbb972015-04-22 11:59:37 +00004627 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4628 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004629 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004630 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004631 ResultIterSpace.CounterInit == nullptr ||
4632 ResultIterSpace.CounterStep == nullptr);
4633
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004634 return HasErrors;
4635}
4636
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004637/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004638static ExprResult
4639BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4640 ExprResult Start,
4641 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004642 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004643 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4644 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004645 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004646 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004647 VarRef.get()->getType())) {
4648 NewStart = SemaRef.PerformImplicitConversion(
4649 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4650 /*AllowExplicit=*/true);
4651 if (!NewStart.isUsable())
4652 return ExprError();
4653 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004654
4655 auto Init =
4656 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4657 return Init;
4658}
4659
Alexander Musmana5f070a2014-10-01 06:03:56 +00004660/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004661static ExprResult
4662BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4663 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4664 ExprResult Step, bool Subtract,
4665 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004666 // Add parentheses (for debugging purposes only).
4667 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4668 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4669 !Step.isUsable())
4670 return ExprError();
4671
Alexey Bataev5a3af132016-03-29 08:58:54 +00004672 ExprResult NewStep = Step;
4673 if (Captures)
4674 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004675 if (NewStep.isInvalid())
4676 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004677 ExprResult Update =
4678 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004679 if (!Update.isUsable())
4680 return ExprError();
4681
Alexey Bataevc0214e02016-02-16 12:13:49 +00004682 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4683 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004684 ExprResult NewStart = Start;
4685 if (Captures)
4686 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004687 if (NewStart.isInvalid())
4688 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004689
Alexey Bataevc0214e02016-02-16 12:13:49 +00004690 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4691 ExprResult SavedUpdate = Update;
4692 ExprResult UpdateVal;
4693 if (VarRef.get()->getType()->isOverloadableType() ||
4694 NewStart.get()->getType()->isOverloadableType() ||
4695 Update.get()->getType()->isOverloadableType()) {
4696 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4697 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4698 Update =
4699 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4700 if (Update.isUsable()) {
4701 UpdateVal =
4702 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4703 VarRef.get(), SavedUpdate.get());
4704 if (UpdateVal.isUsable()) {
4705 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4706 UpdateVal.get());
4707 }
4708 }
4709 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4710 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004711
Alexey Bataevc0214e02016-02-16 12:13:49 +00004712 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4713 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4714 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4715 NewStart.get(), SavedUpdate.get());
4716 if (!Update.isUsable())
4717 return ExprError();
4718
Alexey Bataev11481f52016-02-17 10:29:05 +00004719 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4720 VarRef.get()->getType())) {
4721 Update = SemaRef.PerformImplicitConversion(
4722 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4723 if (!Update.isUsable())
4724 return ExprError();
4725 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004726
4727 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4728 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004729 return Update;
4730}
4731
4732/// \brief Convert integer expression \a E to make it have at least \a Bits
4733/// bits.
4734static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4735 Sema &SemaRef) {
4736 if (E == nullptr)
4737 return ExprError();
4738 auto &C = SemaRef.Context;
4739 QualType OldType = E->getType();
4740 unsigned HasBits = C.getTypeSize(OldType);
4741 if (HasBits >= Bits)
4742 return ExprResult(E);
4743 // OK to convert to signed, because new type has more bits than old.
4744 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4745 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4746 true);
4747}
4748
4749/// \brief Check if the given expression \a E is a constant integer that fits
4750/// into \a Bits bits.
4751static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4752 if (E == nullptr)
4753 return false;
4754 llvm::APSInt Result;
4755 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4756 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4757 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004758}
4759
Alexey Bataev5a3af132016-03-29 08:58:54 +00004760/// Build preinits statement for the given declarations.
4761static Stmt *buildPreInits(ASTContext &Context,
4762 SmallVectorImpl<Decl *> &PreInits) {
4763 if (!PreInits.empty()) {
4764 return new (Context) DeclStmt(
4765 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4766 SourceLocation(), SourceLocation());
4767 }
4768 return nullptr;
4769}
4770
4771/// Build preinits statement for the given declarations.
4772static Stmt *buildPreInits(ASTContext &Context,
4773 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4774 if (!Captures.empty()) {
4775 SmallVector<Decl *, 16> PreInits;
4776 for (auto &Pair : Captures)
4777 PreInits.push_back(Pair.second->getDecl());
4778 return buildPreInits(Context, PreInits);
4779 }
4780 return nullptr;
4781}
4782
4783/// Build postupdate expression for the given list of postupdates expressions.
4784static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4785 Expr *PostUpdate = nullptr;
4786 if (!PostUpdates.empty()) {
4787 for (auto *E : PostUpdates) {
4788 Expr *ConvE = S.BuildCStyleCastExpr(
4789 E->getExprLoc(),
4790 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4791 E->getExprLoc(), E)
4792 .get();
4793 PostUpdate = PostUpdate
4794 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4795 PostUpdate, ConvE)
4796 .get()
4797 : ConvE;
4798 }
4799 }
4800 return PostUpdate;
4801}
4802
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004803/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004804/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4805/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004806static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004807CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4808 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4809 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004810 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004811 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004812 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004813 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004814 // Found 'collapse' clause - calculate collapse number.
4815 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004816 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004817 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004818 }
4819 if (OrderedLoopCountExpr) {
4820 // Found 'ordered' clause - calculate collapse number.
4821 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004822 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4823 if (Result.getLimitedValue() < NestedLoopCount) {
4824 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4825 diag::err_omp_wrong_ordered_loop_count)
4826 << OrderedLoopCountExpr->getSourceRange();
4827 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4828 diag::note_collapse_loop_count)
4829 << CollapseLoopCountExpr->getSourceRange();
4830 }
4831 NestedLoopCount = Result.getLimitedValue();
4832 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004833 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004834 // This is helper routine for loop directives (e.g., 'for', 'simd',
4835 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004836 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004837 SmallVector<LoopIterationSpace, 4> IterSpaces;
4838 IterSpaces.resize(NestedLoopCount);
4839 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004840 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004841 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004842 NestedLoopCount, CollapseLoopCountExpr,
4843 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004844 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004845 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004846 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004847 // OpenMP [2.8.1, simd construct, Restrictions]
4848 // All loops associated with the construct must be perfectly nested; that
4849 // is, there must be no intervening code nor any OpenMP directive between
4850 // any two loops.
4851 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004852 }
4853
Alexander Musmana5f070a2014-10-01 06:03:56 +00004854 Built.clear(/* size */ NestedLoopCount);
4855
4856 if (SemaRef.CurContext->isDependentContext())
4857 return NestedLoopCount;
4858
4859 // An example of what is generated for the following code:
4860 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004861 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004862 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004863 // for (k = 0; k < NK; ++k)
4864 // for (j = J0; j < NJ; j+=2) {
4865 // <loop body>
4866 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004867 //
4868 // We generate the code below.
4869 // Note: the loop body may be outlined in CodeGen.
4870 // Note: some counters may be C++ classes, operator- is used to find number of
4871 // iterations and operator+= to calculate counter value.
4872 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4873 // or i64 is currently supported).
4874 //
4875 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4876 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4877 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4878 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4879 // // similar updates for vars in clauses (e.g. 'linear')
4880 // <loop body (using local i and j)>
4881 // }
4882 // i = NI; // assign final values of counters
4883 // j = NJ;
4884 //
4885
4886 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4887 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004888 // Precondition tests if there is at least one iteration (all conditions are
4889 // true).
4890 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004891 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004892 ExprResult LastIteration32 = WidenIterationCount(
4893 32 /* Bits */, SemaRef.PerformImplicitConversion(
4894 N0->IgnoreImpCasts(), N0->getType(),
4895 Sema::AA_Converting, /*AllowExplicit=*/true)
4896 .get(),
4897 SemaRef);
4898 ExprResult LastIteration64 = WidenIterationCount(
4899 64 /* Bits */, SemaRef.PerformImplicitConversion(
4900 N0->IgnoreImpCasts(), N0->getType(),
4901 Sema::AA_Converting, /*AllowExplicit=*/true)
4902 .get(),
4903 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004904
4905 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4906 return NestedLoopCount;
4907
4908 auto &C = SemaRef.Context;
4909 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4910
4911 Scope *CurScope = DSA.getCurScope();
4912 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004913 if (PreCond.isUsable()) {
4914 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4915 PreCond.get(), IterSpaces[Cnt].PreCond);
4916 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004917 auto N = IterSpaces[Cnt].NumIterations;
4918 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4919 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004920 LastIteration32 = SemaRef.BuildBinOp(
4921 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4922 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4923 Sema::AA_Converting,
4924 /*AllowExplicit=*/true)
4925 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004926 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004927 LastIteration64 = SemaRef.BuildBinOp(
4928 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4929 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4930 Sema::AA_Converting,
4931 /*AllowExplicit=*/true)
4932 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004933 }
4934
4935 // Choose either the 32-bit or 64-bit version.
4936 ExprResult LastIteration = LastIteration64;
4937 if (LastIteration32.isUsable() &&
4938 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4939 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4940 FitsInto(
4941 32 /* Bits */,
4942 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4943 LastIteration64.get(), SemaRef)))
4944 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004945 QualType VType = LastIteration.get()->getType();
4946 QualType RealVType = VType;
4947 QualType StrideVType = VType;
4948 if (isOpenMPTaskLoopDirective(DKind)) {
4949 VType =
4950 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4951 StrideVType =
4952 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4953 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004954
4955 if (!LastIteration.isUsable())
4956 return 0;
4957
4958 // Save the number of iterations.
4959 ExprResult NumIterations = LastIteration;
4960 {
4961 LastIteration = SemaRef.BuildBinOp(
4962 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4963 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4964 if (!LastIteration.isUsable())
4965 return 0;
4966 }
4967
4968 // Calculate the last iteration number beforehand instead of doing this on
4969 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4970 llvm::APSInt Result;
4971 bool IsConstant =
4972 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4973 ExprResult CalcLastIteration;
4974 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004975 ExprResult SaveRef =
4976 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004977 LastIteration = SaveRef;
4978
4979 // Prepare SaveRef + 1.
4980 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004981 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004982 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4983 if (!NumIterations.isUsable())
4984 return 0;
4985 }
4986
4987 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4988
Alexander Musmanc6388682014-12-15 07:07:06 +00004989 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00004990 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004991 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4992 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004993 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004994 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4995 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004996 SemaRef.AddInitializerToDecl(
4997 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4998 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4999
5000 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005001 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5002 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005003 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5004 /*DirectInit*/ false,
5005 /*TypeMayContainAuto*/ false);
5006
5007 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5008 // This will be used to implement clause 'lastprivate'.
5009 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005010 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5011 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005012 SemaRef.AddInitializerToDecl(
5013 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5014 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5015
5016 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005017 VarDecl *STDecl =
5018 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5019 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005020 SemaRef.AddInitializerToDecl(
5021 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5022 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5023
5024 // Build expression: UB = min(UB, LastIteration)
5025 // It is nesessary for CodeGen of directives with static scheduling.
5026 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5027 UB.get(), LastIteration.get());
5028 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5029 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5030 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5031 CondOp.get());
5032 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005033
5034 // If we have a combined directive that combines 'distribute', 'for' or
5035 // 'simd' we need to be able to access the bounds of the schedule of the
5036 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5037 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5038 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5039 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5040
5041 // We expect to have at least 2 more parameters than the 'parallel'
5042 // directive does - the lower and upper bounds of the previous schedule.
5043 assert(CD->getNumParams() >= 4 &&
5044 "Unexpected number of parameters in loop combined directive");
5045
5046 // Set the proper type for the bounds given what we learned from the
5047 // enclosed loops.
5048 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5049 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5050
5051 // Previous lower and upper bounds are obtained from the region
5052 // parameters.
5053 PrevLB =
5054 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5055 PrevUB =
5056 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5057 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005058 }
5059
5060 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005061 ExprResult IV;
5062 ExprResult Init;
5063 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005064 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5065 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005066 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005067 isOpenMPTaskLoopDirective(DKind) ||
5068 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005069 ? LB.get()
5070 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5071 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5072 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005073 }
5074
Alexander Musmanc6388682014-12-15 07:07:06 +00005075 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005076 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005077 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005078 (isOpenMPWorksharingDirective(DKind) ||
5079 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005080 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5081 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5082 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005083
5084 // Loop increment (IV = IV + 1)
5085 SourceLocation IncLoc;
5086 ExprResult Inc =
5087 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5088 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5089 if (!Inc.isUsable())
5090 return 0;
5091 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005092 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5093 if (!Inc.isUsable())
5094 return 0;
5095
5096 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5097 // Used for directives with static scheduling.
5098 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005099 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5100 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005101 // LB + ST
5102 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5103 if (!NextLB.isUsable())
5104 return 0;
5105 // LB = LB + ST
5106 NextLB =
5107 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5108 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5109 if (!NextLB.isUsable())
5110 return 0;
5111 // UB + ST
5112 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5113 if (!NextUB.isUsable())
5114 return 0;
5115 // UB = UB + ST
5116 NextUB =
5117 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5118 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5119 if (!NextUB.isUsable())
5120 return 0;
5121 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005122
5123 // Build updates and final values of the loop counters.
5124 bool HasErrors = false;
5125 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005126 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005127 Built.Updates.resize(NestedLoopCount);
5128 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005129 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005130 {
5131 ExprResult Div;
5132 // Go from inner nested loop to outer.
5133 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5134 LoopIterationSpace &IS = IterSpaces[Cnt];
5135 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5136 // Build: Iter = (IV / Div) % IS.NumIters
5137 // where Div is product of previous iterations' IS.NumIters.
5138 ExprResult Iter;
5139 if (Div.isUsable()) {
5140 Iter =
5141 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5142 } else {
5143 Iter = IV;
5144 assert((Cnt == (int)NestedLoopCount - 1) &&
5145 "unusable div expected on first iteration only");
5146 }
5147
5148 if (Cnt != 0 && Iter.isUsable())
5149 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5150 IS.NumIterations);
5151 if (!Iter.isUsable()) {
5152 HasErrors = true;
5153 break;
5154 }
5155
Alexey Bataev39f915b82015-05-08 10:41:21 +00005156 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005157 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5158 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5159 IS.CounterVar->getExprLoc(),
5160 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005161 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005162 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005163 if (!Init.isUsable()) {
5164 HasErrors = true;
5165 break;
5166 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005167 ExprResult Update = BuildCounterUpdate(
5168 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5169 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005170 if (!Update.isUsable()) {
5171 HasErrors = true;
5172 break;
5173 }
5174
5175 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5176 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005177 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005178 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005179 if (!Final.isUsable()) {
5180 HasErrors = true;
5181 break;
5182 }
5183
5184 // Build Div for the next iteration: Div <- Div * IS.NumIters
5185 if (Cnt != 0) {
5186 if (Div.isUnset())
5187 Div = IS.NumIterations;
5188 else
5189 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5190 IS.NumIterations);
5191
5192 // Add parentheses (for debugging purposes only).
5193 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005194 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005195 if (!Div.isUsable()) {
5196 HasErrors = true;
5197 break;
5198 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005199 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005200 }
5201 if (!Update.isUsable() || !Final.isUsable()) {
5202 HasErrors = true;
5203 break;
5204 }
5205 // Save results
5206 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005207 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005208 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005209 Built.Updates[Cnt] = Update.get();
5210 Built.Finals[Cnt] = Final.get();
5211 }
5212 }
5213
5214 if (HasErrors)
5215 return 0;
5216
5217 // Save results
5218 Built.IterationVarRef = IV.get();
5219 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005220 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005221 Built.CalcLastIteration =
5222 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005223 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005224 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005225 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005226 Built.Init = Init.get();
5227 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005228 Built.LB = LB.get();
5229 Built.UB = UB.get();
5230 Built.IL = IL.get();
5231 Built.ST = ST.get();
5232 Built.EUB = EUB.get();
5233 Built.NLB = NextLB.get();
5234 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005235 Built.PrevLB = PrevLB.get();
5236 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005237
Alexey Bataev8b427062016-05-25 12:36:08 +00005238 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5239 // Fill data for doacross depend clauses.
5240 for (auto Pair : DSA.getDoacrossDependClauses()) {
5241 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5242 Pair.first->setCounterValue(CounterVal);
5243 else {
5244 if (NestedLoopCount != Pair.second.size() ||
5245 NestedLoopCount != LoopMultipliers.size() + 1) {
5246 // Erroneous case - clause has some problems.
5247 Pair.first->setCounterValue(CounterVal);
5248 continue;
5249 }
5250 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5251 auto I = Pair.second.rbegin();
5252 auto IS = IterSpaces.rbegin();
5253 auto ILM = LoopMultipliers.rbegin();
5254 Expr *UpCounterVal = CounterVal;
5255 Expr *Multiplier = nullptr;
5256 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5257 if (I->first) {
5258 assert(IS->CounterStep);
5259 Expr *NormalizedOffset =
5260 SemaRef
5261 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5262 I->first, IS->CounterStep)
5263 .get();
5264 if (Multiplier) {
5265 NormalizedOffset =
5266 SemaRef
5267 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5268 NormalizedOffset, Multiplier)
5269 .get();
5270 }
5271 assert(I->second == OO_Plus || I->second == OO_Minus);
5272 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5273 UpCounterVal =
5274 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5275 UpCounterVal, NormalizedOffset).get();
5276 }
5277 Multiplier = *ILM;
5278 ++I;
5279 ++IS;
5280 ++ILM;
5281 }
5282 Pair.first->setCounterValue(UpCounterVal);
5283 }
5284 }
5285
Alexey Bataevabfc0692014-06-25 06:52:00 +00005286 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005287}
5288
Alexey Bataev10e775f2015-07-30 11:36:16 +00005289static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005290 auto CollapseClauses =
5291 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5292 if (CollapseClauses.begin() != CollapseClauses.end())
5293 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005294 return nullptr;
5295}
5296
Alexey Bataev10e775f2015-07-30 11:36:16 +00005297static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005298 auto OrderedClauses =
5299 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5300 if (OrderedClauses.begin() != OrderedClauses.end())
5301 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005302 return nullptr;
5303}
5304
Alexey Bataev66b15b52015-08-21 11:14:16 +00005305static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
5306 const Expr *Safelen) {
5307 llvm::APSInt SimdlenRes, SafelenRes;
5308 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
5309 Simdlen->isInstantiationDependent() ||
5310 Simdlen->containsUnexpandedParameterPack())
5311 return false;
5312 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
5313 Safelen->isInstantiationDependent() ||
5314 Safelen->containsUnexpandedParameterPack())
5315 return false;
5316 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
5317 Safelen->EvaluateAsInt(SafelenRes, S.Context);
5318 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5319 // If both simdlen and safelen clauses are specified, the value of the simdlen
5320 // parameter must be less than or equal to the value of the safelen parameter.
5321 if (SimdlenRes > SafelenRes) {
5322 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
5323 << Simdlen->getSourceRange() << Safelen->getSourceRange();
5324 return true;
5325 }
5326 return false;
5327}
5328
Alexey Bataev4acb8592014-07-07 13:01:15 +00005329StmtResult Sema::ActOnOpenMPSimdDirective(
5330 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5331 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005332 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005333 if (!AStmt)
5334 return StmtError();
5335
5336 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005337 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005338 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5339 // define the nested loops number.
5340 unsigned NestedLoopCount = CheckOpenMPLoop(
5341 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5342 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005343 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005344 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005345
Alexander Musmana5f070a2014-10-01 06:03:56 +00005346 assert((CurContext->isDependentContext() || B.builtAll()) &&
5347 "omp simd loop exprs were not built");
5348
Alexander Musman3276a272015-03-21 10:12:56 +00005349 if (!CurContext->isDependentContext()) {
5350 // Finalize the clauses that need pre-built expressions for CodeGen.
5351 for (auto C : Clauses) {
5352 if (auto LC = dyn_cast<OMPLinearClause>(C))
5353 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005354 B.NumIterations, *this, CurScope,
5355 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005356 return StmtError();
5357 }
5358 }
5359
Alexey Bataev66b15b52015-08-21 11:14:16 +00005360 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5361 // If both simdlen and safelen clauses are specified, the value of the simdlen
5362 // parameter must be less than or equal to the value of the safelen parameter.
5363 OMPSafelenClause *Safelen = nullptr;
5364 OMPSimdlenClause *Simdlen = nullptr;
5365 for (auto *Clause : Clauses) {
5366 if (Clause->getClauseKind() == OMPC_safelen)
5367 Safelen = cast<OMPSafelenClause>(Clause);
5368 else if (Clause->getClauseKind() == OMPC_simdlen)
5369 Simdlen = cast<OMPSimdlenClause>(Clause);
5370 if (Safelen && Simdlen)
5371 break;
5372 }
5373 if (Simdlen && Safelen &&
5374 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5375 Safelen->getSafelen()))
5376 return StmtError();
5377
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005378 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005379 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5380 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005381}
5382
Alexey Bataev4acb8592014-07-07 13:01:15 +00005383StmtResult Sema::ActOnOpenMPForDirective(
5384 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5385 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005386 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005387 if (!AStmt)
5388 return StmtError();
5389
5390 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005391 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005392 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5393 // define the nested loops number.
5394 unsigned NestedLoopCount = CheckOpenMPLoop(
5395 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5396 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005397 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005398 return StmtError();
5399
Alexander Musmana5f070a2014-10-01 06:03:56 +00005400 assert((CurContext->isDependentContext() || B.builtAll()) &&
5401 "omp for loop exprs were not built");
5402
Alexey Bataev54acd402015-08-04 11:18:19 +00005403 if (!CurContext->isDependentContext()) {
5404 // Finalize the clauses that need pre-built expressions for CodeGen.
5405 for (auto C : Clauses) {
5406 if (auto LC = dyn_cast<OMPLinearClause>(C))
5407 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005408 B.NumIterations, *this, CurScope,
5409 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005410 return StmtError();
5411 }
5412 }
5413
Alexey Bataevf29276e2014-06-18 04:14:57 +00005414 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005415 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005416 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005417}
5418
Alexander Musmanf82886e2014-09-18 05:12:34 +00005419StmtResult Sema::ActOnOpenMPForSimdDirective(
5420 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5421 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005422 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005423 if (!AStmt)
5424 return StmtError();
5425
5426 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005427 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005428 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5429 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005430 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005431 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5432 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5433 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005434 if (NestedLoopCount == 0)
5435 return StmtError();
5436
Alexander Musmanc6388682014-12-15 07:07:06 +00005437 assert((CurContext->isDependentContext() || B.builtAll()) &&
5438 "omp for simd loop exprs were not built");
5439
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005440 if (!CurContext->isDependentContext()) {
5441 // Finalize the clauses that need pre-built expressions for CodeGen.
5442 for (auto C : Clauses) {
5443 if (auto LC = dyn_cast<OMPLinearClause>(C))
5444 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005445 B.NumIterations, *this, CurScope,
5446 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005447 return StmtError();
5448 }
5449 }
5450
Alexey Bataev66b15b52015-08-21 11:14:16 +00005451 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5452 // If both simdlen and safelen clauses are specified, the value of the simdlen
5453 // parameter must be less than or equal to the value of the safelen parameter.
5454 OMPSafelenClause *Safelen = nullptr;
5455 OMPSimdlenClause *Simdlen = nullptr;
5456 for (auto *Clause : Clauses) {
5457 if (Clause->getClauseKind() == OMPC_safelen)
5458 Safelen = cast<OMPSafelenClause>(Clause);
5459 else if (Clause->getClauseKind() == OMPC_simdlen)
5460 Simdlen = cast<OMPSimdlenClause>(Clause);
5461 if (Safelen && Simdlen)
5462 break;
5463 }
5464 if (Simdlen && Safelen &&
5465 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5466 Safelen->getSafelen()))
5467 return StmtError();
5468
Alexander Musmanf82886e2014-09-18 05:12:34 +00005469 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005470 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5471 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005472}
5473
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005474StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5475 Stmt *AStmt,
5476 SourceLocation StartLoc,
5477 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005478 if (!AStmt)
5479 return StmtError();
5480
5481 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005482 auto BaseStmt = AStmt;
5483 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5484 BaseStmt = CS->getCapturedStmt();
5485 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5486 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005487 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005488 return StmtError();
5489 // All associated statements must be '#pragma omp section' except for
5490 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005491 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005492 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5493 if (SectionStmt)
5494 Diag(SectionStmt->getLocStart(),
5495 diag::err_omp_sections_substmt_not_section);
5496 return StmtError();
5497 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005498 cast<OMPSectionDirective>(SectionStmt)
5499 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005500 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005501 } else {
5502 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5503 return StmtError();
5504 }
5505
5506 getCurFunction()->setHasBranchProtectedScope();
5507
Alexey Bataev25e5b442015-09-15 12:52:43 +00005508 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5509 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005510}
5511
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005512StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5513 SourceLocation StartLoc,
5514 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005515 if (!AStmt)
5516 return StmtError();
5517
5518 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005519
5520 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005521 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005522
Alexey Bataev25e5b442015-09-15 12:52:43 +00005523 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5524 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005525}
5526
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005527StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5528 Stmt *AStmt,
5529 SourceLocation StartLoc,
5530 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005531 if (!AStmt)
5532 return StmtError();
5533
5534 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005535
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005536 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005537
Alexey Bataev3255bf32015-01-19 05:20:46 +00005538 // OpenMP [2.7.3, single Construct, Restrictions]
5539 // The copyprivate clause must not be used with the nowait clause.
5540 OMPClause *Nowait = nullptr;
5541 OMPClause *Copyprivate = nullptr;
5542 for (auto *Clause : Clauses) {
5543 if (Clause->getClauseKind() == OMPC_nowait)
5544 Nowait = Clause;
5545 else if (Clause->getClauseKind() == OMPC_copyprivate)
5546 Copyprivate = Clause;
5547 if (Copyprivate && Nowait) {
5548 Diag(Copyprivate->getLocStart(),
5549 diag::err_omp_single_copyprivate_with_nowait);
5550 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5551 return StmtError();
5552 }
5553 }
5554
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005555 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5556}
5557
Alexander Musman80c22892014-07-17 08:54:58 +00005558StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5559 SourceLocation StartLoc,
5560 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005561 if (!AStmt)
5562 return StmtError();
5563
5564 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005565
5566 getCurFunction()->setHasBranchProtectedScope();
5567
5568 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5569}
5570
Alexey Bataev28c75412015-12-15 08:19:24 +00005571StmtResult Sema::ActOnOpenMPCriticalDirective(
5572 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5573 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005574 if (!AStmt)
5575 return StmtError();
5576
5577 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005578
Alexey Bataev28c75412015-12-15 08:19:24 +00005579 bool ErrorFound = false;
5580 llvm::APSInt Hint;
5581 SourceLocation HintLoc;
5582 bool DependentHint = false;
5583 for (auto *C : Clauses) {
5584 if (C->getClauseKind() == OMPC_hint) {
5585 if (!DirName.getName()) {
5586 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5587 ErrorFound = true;
5588 }
5589 Expr *E = cast<OMPHintClause>(C)->getHint();
5590 if (E->isTypeDependent() || E->isValueDependent() ||
5591 E->isInstantiationDependent())
5592 DependentHint = true;
5593 else {
5594 Hint = E->EvaluateKnownConstInt(Context);
5595 HintLoc = C->getLocStart();
5596 }
5597 }
5598 }
5599 if (ErrorFound)
5600 return StmtError();
5601 auto Pair = DSAStack->getCriticalWithHint(DirName);
5602 if (Pair.first && DirName.getName() && !DependentHint) {
5603 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5604 Diag(StartLoc, diag::err_omp_critical_with_hint);
5605 if (HintLoc.isValid()) {
5606 Diag(HintLoc, diag::note_omp_critical_hint_here)
5607 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5608 } else
5609 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5610 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5611 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5612 << 1
5613 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5614 /*Radix=*/10, /*Signed=*/false);
5615 } else
5616 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5617 }
5618 }
5619
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005620 getCurFunction()->setHasBranchProtectedScope();
5621
Alexey Bataev28c75412015-12-15 08:19:24 +00005622 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5623 Clauses, AStmt);
5624 if (!Pair.first && DirName.getName() && !DependentHint)
5625 DSAStack->addCriticalWithHint(Dir, Hint);
5626 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005627}
5628
Alexey Bataev4acb8592014-07-07 13:01:15 +00005629StmtResult Sema::ActOnOpenMPParallelForDirective(
5630 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5631 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005632 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005633 if (!AStmt)
5634 return StmtError();
5635
Alexey Bataev4acb8592014-07-07 13:01:15 +00005636 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5637 // 1.2.2 OpenMP Language Terminology
5638 // Structured block - An executable statement with a single entry at the
5639 // top and a single exit at the bottom.
5640 // The point of exit cannot be a branch out of the structured block.
5641 // longjmp() and throw() must not violate the entry/exit criteria.
5642 CS->getCapturedDecl()->setNothrow();
5643
Alexander Musmanc6388682014-12-15 07:07:06 +00005644 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005645 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5646 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005647 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005648 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5649 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5650 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005651 if (NestedLoopCount == 0)
5652 return StmtError();
5653
Alexander Musmana5f070a2014-10-01 06:03:56 +00005654 assert((CurContext->isDependentContext() || B.builtAll()) &&
5655 "omp parallel for loop exprs were not built");
5656
Alexey Bataev54acd402015-08-04 11:18:19 +00005657 if (!CurContext->isDependentContext()) {
5658 // Finalize the clauses that need pre-built expressions for CodeGen.
5659 for (auto C : Clauses) {
5660 if (auto LC = dyn_cast<OMPLinearClause>(C))
5661 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005662 B.NumIterations, *this, CurScope,
5663 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005664 return StmtError();
5665 }
5666 }
5667
Alexey Bataev4acb8592014-07-07 13:01:15 +00005668 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005669 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005670 NestedLoopCount, Clauses, AStmt, B,
5671 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005672}
5673
Alexander Musmane4e893b2014-09-23 09:33:00 +00005674StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5675 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5676 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005677 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005678 if (!AStmt)
5679 return StmtError();
5680
Alexander Musmane4e893b2014-09-23 09:33:00 +00005681 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5682 // 1.2.2 OpenMP Language Terminology
5683 // Structured block - An executable statement with a single entry at the
5684 // top and a single exit at the bottom.
5685 // The point of exit cannot be a branch out of the structured block.
5686 // longjmp() and throw() must not violate the entry/exit criteria.
5687 CS->getCapturedDecl()->setNothrow();
5688
Alexander Musmanc6388682014-12-15 07:07:06 +00005689 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005690 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5691 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005692 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005693 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5694 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5695 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005696 if (NestedLoopCount == 0)
5697 return StmtError();
5698
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005699 if (!CurContext->isDependentContext()) {
5700 // Finalize the clauses that need pre-built expressions for CodeGen.
5701 for (auto C : Clauses) {
5702 if (auto LC = dyn_cast<OMPLinearClause>(C))
5703 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005704 B.NumIterations, *this, CurScope,
5705 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005706 return StmtError();
5707 }
5708 }
5709
Alexey Bataev66b15b52015-08-21 11:14:16 +00005710 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5711 // If both simdlen and safelen clauses are specified, the value of the simdlen
5712 // parameter must be less than or equal to the value of the safelen parameter.
5713 OMPSafelenClause *Safelen = nullptr;
5714 OMPSimdlenClause *Simdlen = nullptr;
5715 for (auto *Clause : Clauses) {
5716 if (Clause->getClauseKind() == OMPC_safelen)
5717 Safelen = cast<OMPSafelenClause>(Clause);
5718 else if (Clause->getClauseKind() == OMPC_simdlen)
5719 Simdlen = cast<OMPSimdlenClause>(Clause);
5720 if (Safelen && Simdlen)
5721 break;
5722 }
5723 if (Simdlen && Safelen &&
5724 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5725 Safelen->getSafelen()))
5726 return StmtError();
5727
Alexander Musmane4e893b2014-09-23 09:33:00 +00005728 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005729 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005730 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005731}
5732
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005733StmtResult
5734Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5735 Stmt *AStmt, SourceLocation StartLoc,
5736 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005737 if (!AStmt)
5738 return StmtError();
5739
5740 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005741 auto BaseStmt = AStmt;
5742 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5743 BaseStmt = CS->getCapturedStmt();
5744 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5745 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005746 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005747 return StmtError();
5748 // All associated statements must be '#pragma omp section' except for
5749 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005750 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005751 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5752 if (SectionStmt)
5753 Diag(SectionStmt->getLocStart(),
5754 diag::err_omp_parallel_sections_substmt_not_section);
5755 return StmtError();
5756 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005757 cast<OMPSectionDirective>(SectionStmt)
5758 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005759 }
5760 } else {
5761 Diag(AStmt->getLocStart(),
5762 diag::err_omp_parallel_sections_not_compound_stmt);
5763 return StmtError();
5764 }
5765
5766 getCurFunction()->setHasBranchProtectedScope();
5767
Alexey Bataev25e5b442015-09-15 12:52:43 +00005768 return OMPParallelSectionsDirective::Create(
5769 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005770}
5771
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005772StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5773 Stmt *AStmt, SourceLocation StartLoc,
5774 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005775 if (!AStmt)
5776 return StmtError();
5777
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005778 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5779 // 1.2.2 OpenMP Language Terminology
5780 // Structured block - An executable statement with a single entry at the
5781 // top and a single exit at the bottom.
5782 // The point of exit cannot be a branch out of the structured block.
5783 // longjmp() and throw() must not violate the entry/exit criteria.
5784 CS->getCapturedDecl()->setNothrow();
5785
5786 getCurFunction()->setHasBranchProtectedScope();
5787
Alexey Bataev25e5b442015-09-15 12:52:43 +00005788 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5789 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005790}
5791
Alexey Bataev68446b72014-07-18 07:47:19 +00005792StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5793 SourceLocation EndLoc) {
5794 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5795}
5796
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005797StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5798 SourceLocation EndLoc) {
5799 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5800}
5801
Alexey Bataev2df347a2014-07-18 10:17:07 +00005802StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5803 SourceLocation EndLoc) {
5804 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5805}
5806
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005807StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5808 SourceLocation StartLoc,
5809 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005810 if (!AStmt)
5811 return StmtError();
5812
5813 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005814
5815 getCurFunction()->setHasBranchProtectedScope();
5816
5817 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5818}
5819
Alexey Bataev6125da92014-07-21 11:26:11 +00005820StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5821 SourceLocation StartLoc,
5822 SourceLocation EndLoc) {
5823 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5824 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5825}
5826
Alexey Bataev346265e2015-09-25 10:37:12 +00005827StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5828 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005829 SourceLocation StartLoc,
5830 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005831 OMPClause *DependFound = nullptr;
5832 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005833 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005834 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005835 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005836 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005837 for (auto *C : Clauses) {
5838 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5839 DependFound = C;
5840 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5841 if (DependSourceClause) {
5842 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5843 << getOpenMPDirectiveName(OMPD_ordered)
5844 << getOpenMPClauseName(OMPC_depend) << 2;
5845 ErrorFound = true;
5846 } else
5847 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005848 if (DependSinkClause) {
5849 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5850 << 0;
5851 ErrorFound = true;
5852 }
5853 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5854 if (DependSourceClause) {
5855 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5856 << 1;
5857 ErrorFound = true;
5858 }
5859 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005860 }
5861 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005862 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005863 else if (C->getClauseKind() == OMPC_simd)
5864 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005865 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005866 if (!ErrorFound && !SC &&
5867 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005868 // OpenMP [2.8.1,simd Construct, Restrictions]
5869 // An ordered construct with the simd clause is the only OpenMP construct
5870 // that can appear in the simd region.
5871 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005872 ErrorFound = true;
5873 } else if (DependFound && (TC || SC)) {
5874 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5875 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5876 ErrorFound = true;
5877 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5878 Diag(DependFound->getLocStart(),
5879 diag::err_omp_ordered_directive_without_param);
5880 ErrorFound = true;
5881 } else if (TC || Clauses.empty()) {
5882 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5883 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5884 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5885 << (TC != nullptr);
5886 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5887 ErrorFound = true;
5888 }
5889 }
5890 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005891 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005892
5893 if (AStmt) {
5894 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5895
5896 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005897 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005898
5899 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005900}
5901
Alexey Bataev1d160b12015-03-13 12:27:31 +00005902namespace {
5903/// \brief Helper class for checking expression in 'omp atomic [update]'
5904/// construct.
5905class OpenMPAtomicUpdateChecker {
5906 /// \brief Error results for atomic update expressions.
5907 enum ExprAnalysisErrorCode {
5908 /// \brief A statement is not an expression statement.
5909 NotAnExpression,
5910 /// \brief Expression is not builtin binary or unary operation.
5911 NotABinaryOrUnaryExpression,
5912 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5913 NotAnUnaryIncDecExpression,
5914 /// \brief An expression is not of scalar type.
5915 NotAScalarType,
5916 /// \brief A binary operation is not an assignment operation.
5917 NotAnAssignmentOp,
5918 /// \brief RHS part of the binary operation is not a binary expression.
5919 NotABinaryExpression,
5920 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5921 /// expression.
5922 NotABinaryOperator,
5923 /// \brief RHS binary operation does not have reference to the updated LHS
5924 /// part.
5925 NotAnUpdateExpression,
5926 /// \brief No errors is found.
5927 NoError
5928 };
5929 /// \brief Reference to Sema.
5930 Sema &SemaRef;
5931 /// \brief A location for note diagnostics (when error is found).
5932 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005933 /// \brief 'x' lvalue part of the source atomic expression.
5934 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005935 /// \brief 'expr' rvalue part of the source atomic expression.
5936 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005937 /// \brief Helper expression of the form
5938 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5939 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5940 Expr *UpdateExpr;
5941 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5942 /// important for non-associative operations.
5943 bool IsXLHSInRHSPart;
5944 BinaryOperatorKind Op;
5945 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005946 /// \brief true if the source expression is a postfix unary operation, false
5947 /// if it is a prefix unary operation.
5948 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005949
5950public:
5951 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005952 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005953 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005954 /// \brief Check specified statement that it is suitable for 'atomic update'
5955 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005956 /// expression. If DiagId and NoteId == 0, then only check is performed
5957 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005958 /// \param DiagId Diagnostic which should be emitted if error is found.
5959 /// \param NoteId Diagnostic note for the main error message.
5960 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005961 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005962 /// \brief Return the 'x' lvalue part of the source atomic expression.
5963 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005964 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5965 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005966 /// \brief Return the update expression used in calculation of the updated
5967 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5968 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5969 Expr *getUpdateExpr() const { return UpdateExpr; }
5970 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5971 /// false otherwise.
5972 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5973
Alexey Bataevb78ca832015-04-01 03:33:17 +00005974 /// \brief true if the source expression is a postfix unary operation, false
5975 /// if it is a prefix unary operation.
5976 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5977
Alexey Bataev1d160b12015-03-13 12:27:31 +00005978private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005979 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5980 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005981};
5982} // namespace
5983
5984bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5985 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5986 ExprAnalysisErrorCode ErrorFound = NoError;
5987 SourceLocation ErrorLoc, NoteLoc;
5988 SourceRange ErrorRange, NoteRange;
5989 // Allowed constructs are:
5990 // x = x binop expr;
5991 // x = expr binop x;
5992 if (AtomicBinOp->getOpcode() == BO_Assign) {
5993 X = AtomicBinOp->getLHS();
5994 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5995 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5996 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5997 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5998 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005999 Op = AtomicInnerBinOp->getOpcode();
6000 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006001 auto *LHS = AtomicInnerBinOp->getLHS();
6002 auto *RHS = AtomicInnerBinOp->getRHS();
6003 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6004 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6005 /*Canonical=*/true);
6006 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6007 /*Canonical=*/true);
6008 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6009 /*Canonical=*/true);
6010 if (XId == LHSId) {
6011 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006012 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006013 } else if (XId == RHSId) {
6014 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006015 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006016 } else {
6017 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6018 ErrorRange = AtomicInnerBinOp->getSourceRange();
6019 NoteLoc = X->getExprLoc();
6020 NoteRange = X->getSourceRange();
6021 ErrorFound = NotAnUpdateExpression;
6022 }
6023 } else {
6024 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6025 ErrorRange = AtomicInnerBinOp->getSourceRange();
6026 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6027 NoteRange = SourceRange(NoteLoc, NoteLoc);
6028 ErrorFound = NotABinaryOperator;
6029 }
6030 } else {
6031 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6032 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6033 ErrorFound = NotABinaryExpression;
6034 }
6035 } else {
6036 ErrorLoc = AtomicBinOp->getExprLoc();
6037 ErrorRange = AtomicBinOp->getSourceRange();
6038 NoteLoc = AtomicBinOp->getOperatorLoc();
6039 NoteRange = SourceRange(NoteLoc, NoteLoc);
6040 ErrorFound = NotAnAssignmentOp;
6041 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006042 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006043 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6044 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6045 return true;
6046 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006047 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006048 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006049}
6050
6051bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6052 unsigned NoteId) {
6053 ExprAnalysisErrorCode ErrorFound = NoError;
6054 SourceLocation ErrorLoc, NoteLoc;
6055 SourceRange ErrorRange, NoteRange;
6056 // Allowed constructs are:
6057 // x++;
6058 // x--;
6059 // ++x;
6060 // --x;
6061 // x binop= expr;
6062 // x = x binop expr;
6063 // x = expr binop x;
6064 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6065 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6066 if (AtomicBody->getType()->isScalarType() ||
6067 AtomicBody->isInstantiationDependent()) {
6068 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6069 AtomicBody->IgnoreParenImpCasts())) {
6070 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006071 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006072 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006073 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006074 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006075 X = AtomicCompAssignOp->getLHS();
6076 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006077 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6078 AtomicBody->IgnoreParenImpCasts())) {
6079 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006080 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6081 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006082 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006083 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6084 // Check for Unary Operation
6085 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006086 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006087 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6088 OpLoc = AtomicUnaryOp->getOperatorLoc();
6089 X = AtomicUnaryOp->getSubExpr();
6090 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6091 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006092 } else {
6093 ErrorFound = NotAnUnaryIncDecExpression;
6094 ErrorLoc = AtomicUnaryOp->getExprLoc();
6095 ErrorRange = AtomicUnaryOp->getSourceRange();
6096 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6097 NoteRange = SourceRange(NoteLoc, NoteLoc);
6098 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006099 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006100 ErrorFound = NotABinaryOrUnaryExpression;
6101 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6102 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6103 }
6104 } else {
6105 ErrorFound = NotAScalarType;
6106 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6107 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6108 }
6109 } else {
6110 ErrorFound = NotAnExpression;
6111 NoteLoc = ErrorLoc = S->getLocStart();
6112 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6113 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006114 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006115 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6116 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6117 return true;
6118 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006119 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006120 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006121 // Build an update expression of form 'OpaqueValueExpr(x) binop
6122 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6123 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6124 auto *OVEX = new (SemaRef.getASTContext())
6125 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6126 auto *OVEExpr = new (SemaRef.getASTContext())
6127 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6128 auto Update =
6129 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6130 IsXLHSInRHSPart ? OVEExpr : OVEX);
6131 if (Update.isInvalid())
6132 return true;
6133 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6134 Sema::AA_Casting);
6135 if (Update.isInvalid())
6136 return true;
6137 UpdateExpr = Update.get();
6138 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006139 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006140}
6141
Alexey Bataev0162e452014-07-22 10:10:35 +00006142StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6143 Stmt *AStmt,
6144 SourceLocation StartLoc,
6145 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006146 if (!AStmt)
6147 return StmtError();
6148
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006149 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006150 // 1.2.2 OpenMP Language Terminology
6151 // Structured block - An executable statement with a single entry at the
6152 // top and a single exit at the bottom.
6153 // The point of exit cannot be a branch out of the structured block.
6154 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006155 OpenMPClauseKind AtomicKind = OMPC_unknown;
6156 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006157 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006158 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006159 C->getClauseKind() == OMPC_update ||
6160 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006161 if (AtomicKind != OMPC_unknown) {
6162 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6163 << SourceRange(C->getLocStart(), C->getLocEnd());
6164 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6165 << getOpenMPClauseName(AtomicKind);
6166 } else {
6167 AtomicKind = C->getClauseKind();
6168 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006169 }
6170 }
6171 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006172
Alexey Bataev459dec02014-07-24 06:46:57 +00006173 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006174 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6175 Body = EWC->getSubExpr();
6176
Alexey Bataev62cec442014-11-18 10:14:22 +00006177 Expr *X = nullptr;
6178 Expr *V = nullptr;
6179 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006180 Expr *UE = nullptr;
6181 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006182 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006183 // OpenMP [2.12.6, atomic Construct]
6184 // In the next expressions:
6185 // * x and v (as applicable) are both l-value expressions with scalar type.
6186 // * During the execution of an atomic region, multiple syntactic
6187 // occurrences of x must designate the same storage location.
6188 // * Neither of v and expr (as applicable) may access the storage location
6189 // designated by x.
6190 // * Neither of x and expr (as applicable) may access the storage location
6191 // designated by v.
6192 // * expr is an expression with scalar type.
6193 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6194 // * binop, binop=, ++, and -- are not overloaded operators.
6195 // * The expression x binop expr must be numerically equivalent to x binop
6196 // (expr). This requirement is satisfied if the operators in expr have
6197 // precedence greater than binop, or by using parentheses around expr or
6198 // subexpressions of expr.
6199 // * The expression expr binop x must be numerically equivalent to (expr)
6200 // binop x. This requirement is satisfied if the operators in expr have
6201 // precedence equal to or greater than binop, or by using parentheses around
6202 // expr or subexpressions of expr.
6203 // * For forms that allow multiple occurrences of x, the number of times
6204 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006205 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006206 enum {
6207 NotAnExpression,
6208 NotAnAssignmentOp,
6209 NotAScalarType,
6210 NotAnLValue,
6211 NoError
6212 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006213 SourceLocation ErrorLoc, NoteLoc;
6214 SourceRange ErrorRange, NoteRange;
6215 // If clause is read:
6216 // v = x;
6217 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6218 auto AtomicBinOp =
6219 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6220 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6221 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6222 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6223 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6224 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6225 if (!X->isLValue() || !V->isLValue()) {
6226 auto NotLValueExpr = X->isLValue() ? V : X;
6227 ErrorFound = NotAnLValue;
6228 ErrorLoc = AtomicBinOp->getExprLoc();
6229 ErrorRange = AtomicBinOp->getSourceRange();
6230 NoteLoc = NotLValueExpr->getExprLoc();
6231 NoteRange = NotLValueExpr->getSourceRange();
6232 }
6233 } else if (!X->isInstantiationDependent() ||
6234 !V->isInstantiationDependent()) {
6235 auto NotScalarExpr =
6236 (X->isInstantiationDependent() || X->getType()->isScalarType())
6237 ? V
6238 : X;
6239 ErrorFound = NotAScalarType;
6240 ErrorLoc = AtomicBinOp->getExprLoc();
6241 ErrorRange = AtomicBinOp->getSourceRange();
6242 NoteLoc = NotScalarExpr->getExprLoc();
6243 NoteRange = NotScalarExpr->getSourceRange();
6244 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006245 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006246 ErrorFound = NotAnAssignmentOp;
6247 ErrorLoc = AtomicBody->getExprLoc();
6248 ErrorRange = AtomicBody->getSourceRange();
6249 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6250 : AtomicBody->getExprLoc();
6251 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6252 : AtomicBody->getSourceRange();
6253 }
6254 } else {
6255 ErrorFound = NotAnExpression;
6256 NoteLoc = ErrorLoc = Body->getLocStart();
6257 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006258 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006259 if (ErrorFound != NoError) {
6260 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6261 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006262 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6263 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006264 return StmtError();
6265 } else if (CurContext->isDependentContext())
6266 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006267 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006268 enum {
6269 NotAnExpression,
6270 NotAnAssignmentOp,
6271 NotAScalarType,
6272 NotAnLValue,
6273 NoError
6274 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006275 SourceLocation ErrorLoc, NoteLoc;
6276 SourceRange ErrorRange, NoteRange;
6277 // If clause is write:
6278 // x = expr;
6279 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6280 auto AtomicBinOp =
6281 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6282 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006283 X = AtomicBinOp->getLHS();
6284 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006285 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6286 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6287 if (!X->isLValue()) {
6288 ErrorFound = NotAnLValue;
6289 ErrorLoc = AtomicBinOp->getExprLoc();
6290 ErrorRange = AtomicBinOp->getSourceRange();
6291 NoteLoc = X->getExprLoc();
6292 NoteRange = X->getSourceRange();
6293 }
6294 } else if (!X->isInstantiationDependent() ||
6295 !E->isInstantiationDependent()) {
6296 auto NotScalarExpr =
6297 (X->isInstantiationDependent() || X->getType()->isScalarType())
6298 ? E
6299 : X;
6300 ErrorFound = NotAScalarType;
6301 ErrorLoc = AtomicBinOp->getExprLoc();
6302 ErrorRange = AtomicBinOp->getSourceRange();
6303 NoteLoc = NotScalarExpr->getExprLoc();
6304 NoteRange = NotScalarExpr->getSourceRange();
6305 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006306 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006307 ErrorFound = NotAnAssignmentOp;
6308 ErrorLoc = AtomicBody->getExprLoc();
6309 ErrorRange = AtomicBody->getSourceRange();
6310 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6311 : AtomicBody->getExprLoc();
6312 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6313 : AtomicBody->getSourceRange();
6314 }
6315 } else {
6316 ErrorFound = NotAnExpression;
6317 NoteLoc = ErrorLoc = Body->getLocStart();
6318 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006319 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006320 if (ErrorFound != NoError) {
6321 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6322 << ErrorRange;
6323 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6324 << NoteRange;
6325 return StmtError();
6326 } else if (CurContext->isDependentContext())
6327 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006328 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006329 // If clause is update:
6330 // x++;
6331 // x--;
6332 // ++x;
6333 // --x;
6334 // x binop= expr;
6335 // x = x binop expr;
6336 // x = expr binop x;
6337 OpenMPAtomicUpdateChecker Checker(*this);
6338 if (Checker.checkStatement(
6339 Body, (AtomicKind == OMPC_update)
6340 ? diag::err_omp_atomic_update_not_expression_statement
6341 : diag::err_omp_atomic_not_expression_statement,
6342 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006343 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006344 if (!CurContext->isDependentContext()) {
6345 E = Checker.getExpr();
6346 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006347 UE = Checker.getUpdateExpr();
6348 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006349 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006350 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006351 enum {
6352 NotAnAssignmentOp,
6353 NotACompoundStatement,
6354 NotTwoSubstatements,
6355 NotASpecificExpression,
6356 NoError
6357 } ErrorFound = NoError;
6358 SourceLocation ErrorLoc, NoteLoc;
6359 SourceRange ErrorRange, NoteRange;
6360 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6361 // If clause is a capture:
6362 // v = x++;
6363 // v = x--;
6364 // v = ++x;
6365 // v = --x;
6366 // v = x binop= expr;
6367 // v = x = x binop expr;
6368 // v = x = expr binop x;
6369 auto *AtomicBinOp =
6370 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6371 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6372 V = AtomicBinOp->getLHS();
6373 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6374 OpenMPAtomicUpdateChecker Checker(*this);
6375 if (Checker.checkStatement(
6376 Body, diag::err_omp_atomic_capture_not_expression_statement,
6377 diag::note_omp_atomic_update))
6378 return StmtError();
6379 E = Checker.getExpr();
6380 X = Checker.getX();
6381 UE = Checker.getUpdateExpr();
6382 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6383 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006384 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006385 ErrorLoc = AtomicBody->getExprLoc();
6386 ErrorRange = AtomicBody->getSourceRange();
6387 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6388 : AtomicBody->getExprLoc();
6389 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6390 : AtomicBody->getSourceRange();
6391 ErrorFound = NotAnAssignmentOp;
6392 }
6393 if (ErrorFound != NoError) {
6394 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6395 << ErrorRange;
6396 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6397 return StmtError();
6398 } else if (CurContext->isDependentContext()) {
6399 UE = V = E = X = nullptr;
6400 }
6401 } else {
6402 // If clause is a capture:
6403 // { v = x; x = expr; }
6404 // { v = x; x++; }
6405 // { v = x; x--; }
6406 // { v = x; ++x; }
6407 // { v = x; --x; }
6408 // { v = x; x binop= expr; }
6409 // { v = x; x = x binop expr; }
6410 // { v = x; x = expr binop x; }
6411 // { x++; v = x; }
6412 // { x--; v = x; }
6413 // { ++x; v = x; }
6414 // { --x; v = x; }
6415 // { x binop= expr; v = x; }
6416 // { x = x binop expr; v = x; }
6417 // { x = expr binop x; v = x; }
6418 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6419 // Check that this is { expr1; expr2; }
6420 if (CS->size() == 2) {
6421 auto *First = CS->body_front();
6422 auto *Second = CS->body_back();
6423 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6424 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6425 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6426 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6427 // Need to find what subexpression is 'v' and what is 'x'.
6428 OpenMPAtomicUpdateChecker Checker(*this);
6429 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6430 BinaryOperator *BinOp = nullptr;
6431 if (IsUpdateExprFound) {
6432 BinOp = dyn_cast<BinaryOperator>(First);
6433 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6434 }
6435 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6436 // { v = x; x++; }
6437 // { v = x; x--; }
6438 // { v = x; ++x; }
6439 // { v = x; --x; }
6440 // { v = x; x binop= expr; }
6441 // { v = x; x = x binop expr; }
6442 // { v = x; x = expr binop x; }
6443 // Check that the first expression has form v = x.
6444 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6445 llvm::FoldingSetNodeID XId, PossibleXId;
6446 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6447 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6448 IsUpdateExprFound = XId == PossibleXId;
6449 if (IsUpdateExprFound) {
6450 V = BinOp->getLHS();
6451 X = Checker.getX();
6452 E = Checker.getExpr();
6453 UE = Checker.getUpdateExpr();
6454 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006455 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006456 }
6457 }
6458 if (!IsUpdateExprFound) {
6459 IsUpdateExprFound = !Checker.checkStatement(First);
6460 BinOp = nullptr;
6461 if (IsUpdateExprFound) {
6462 BinOp = dyn_cast<BinaryOperator>(Second);
6463 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6464 }
6465 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6466 // { x++; v = x; }
6467 // { x--; v = x; }
6468 // { ++x; v = x; }
6469 // { --x; v = x; }
6470 // { x binop= expr; v = x; }
6471 // { x = x binop expr; v = x; }
6472 // { x = expr binop x; v = x; }
6473 // Check that the second expression has form v = x.
6474 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6475 llvm::FoldingSetNodeID XId, PossibleXId;
6476 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6477 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6478 IsUpdateExprFound = XId == PossibleXId;
6479 if (IsUpdateExprFound) {
6480 V = BinOp->getLHS();
6481 X = Checker.getX();
6482 E = Checker.getExpr();
6483 UE = Checker.getUpdateExpr();
6484 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006485 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006486 }
6487 }
6488 }
6489 if (!IsUpdateExprFound) {
6490 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006491 auto *FirstExpr = dyn_cast<Expr>(First);
6492 auto *SecondExpr = dyn_cast<Expr>(Second);
6493 if (!FirstExpr || !SecondExpr ||
6494 !(FirstExpr->isInstantiationDependent() ||
6495 SecondExpr->isInstantiationDependent())) {
6496 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6497 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006498 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006499 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6500 : First->getLocStart();
6501 NoteRange = ErrorRange = FirstBinOp
6502 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006503 : SourceRange(ErrorLoc, ErrorLoc);
6504 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006505 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6506 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6507 ErrorFound = NotAnAssignmentOp;
6508 NoteLoc = ErrorLoc = SecondBinOp
6509 ? SecondBinOp->getOperatorLoc()
6510 : Second->getLocStart();
6511 NoteRange = ErrorRange =
6512 SecondBinOp ? SecondBinOp->getSourceRange()
6513 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006514 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006515 auto *PossibleXRHSInFirst =
6516 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6517 auto *PossibleXLHSInSecond =
6518 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6519 llvm::FoldingSetNodeID X1Id, X2Id;
6520 PossibleXRHSInFirst->Profile(X1Id, Context,
6521 /*Canonical=*/true);
6522 PossibleXLHSInSecond->Profile(X2Id, Context,
6523 /*Canonical=*/true);
6524 IsUpdateExprFound = X1Id == X2Id;
6525 if (IsUpdateExprFound) {
6526 V = FirstBinOp->getLHS();
6527 X = SecondBinOp->getLHS();
6528 E = SecondBinOp->getRHS();
6529 UE = nullptr;
6530 IsXLHSInRHSPart = false;
6531 IsPostfixUpdate = true;
6532 } else {
6533 ErrorFound = NotASpecificExpression;
6534 ErrorLoc = FirstBinOp->getExprLoc();
6535 ErrorRange = FirstBinOp->getSourceRange();
6536 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6537 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6538 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006539 }
6540 }
6541 }
6542 }
6543 } else {
6544 NoteLoc = ErrorLoc = Body->getLocStart();
6545 NoteRange = ErrorRange =
6546 SourceRange(Body->getLocStart(), Body->getLocStart());
6547 ErrorFound = NotTwoSubstatements;
6548 }
6549 } else {
6550 NoteLoc = ErrorLoc = Body->getLocStart();
6551 NoteRange = ErrorRange =
6552 SourceRange(Body->getLocStart(), Body->getLocStart());
6553 ErrorFound = NotACompoundStatement;
6554 }
6555 if (ErrorFound != NoError) {
6556 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6557 << ErrorRange;
6558 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6559 return StmtError();
6560 } else if (CurContext->isDependentContext()) {
6561 UE = V = E = X = nullptr;
6562 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006563 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006564 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006565
6566 getCurFunction()->setHasBranchProtectedScope();
6567
Alexey Bataev62cec442014-11-18 10:14:22 +00006568 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006569 X, V, E, UE, IsXLHSInRHSPart,
6570 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006571}
6572
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006573StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6574 Stmt *AStmt,
6575 SourceLocation StartLoc,
6576 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006577 if (!AStmt)
6578 return StmtError();
6579
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006580 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6581 // 1.2.2 OpenMP Language Terminology
6582 // Structured block - An executable statement with a single entry at the
6583 // top and a single exit at the bottom.
6584 // The point of exit cannot be a branch out of the structured block.
6585 // longjmp() and throw() must not violate the entry/exit criteria.
6586 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006587
Alexey Bataev13314bf2014-10-09 04:18:56 +00006588 // OpenMP [2.16, Nesting of Regions]
6589 // If specified, a teams construct must be contained within a target
6590 // construct. That target construct must contain no statements or directives
6591 // outside of the teams construct.
6592 if (DSAStack->hasInnerTeamsRegion()) {
6593 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6594 bool OMPTeamsFound = true;
6595 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6596 auto I = CS->body_begin();
6597 while (I != CS->body_end()) {
6598 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6599 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6600 OMPTeamsFound = false;
6601 break;
6602 }
6603 ++I;
6604 }
6605 assert(I != CS->body_end() && "Not found statement");
6606 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006607 } else {
6608 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6609 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006610 }
6611 if (!OMPTeamsFound) {
6612 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6613 Diag(DSAStack->getInnerTeamsRegionLoc(),
6614 diag::note_omp_nested_teams_construct_here);
6615 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6616 << isa<OMPExecutableDirective>(S);
6617 return StmtError();
6618 }
6619 }
6620
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006621 getCurFunction()->setHasBranchProtectedScope();
6622
6623 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6624}
6625
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006626StmtResult
6627Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6628 Stmt *AStmt, SourceLocation StartLoc,
6629 SourceLocation EndLoc) {
6630 if (!AStmt)
6631 return StmtError();
6632
6633 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6634 // 1.2.2 OpenMP Language Terminology
6635 // Structured block - An executable statement with a single entry at the
6636 // top and a single exit at the bottom.
6637 // The point of exit cannot be a branch out of the structured block.
6638 // longjmp() and throw() must not violate the entry/exit criteria.
6639 CS->getCapturedDecl()->setNothrow();
6640
6641 getCurFunction()->setHasBranchProtectedScope();
6642
6643 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6644 AStmt);
6645}
6646
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006647StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6648 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6649 SourceLocation EndLoc,
6650 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6651 if (!AStmt)
6652 return StmtError();
6653
6654 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6655 // 1.2.2 OpenMP Language Terminology
6656 // Structured block - An executable statement with a single entry at the
6657 // top and a single exit at the bottom.
6658 // The point of exit cannot be a branch out of the structured block.
6659 // longjmp() and throw() must not violate the entry/exit criteria.
6660 CS->getCapturedDecl()->setNothrow();
6661
6662 OMPLoopDirective::HelperExprs B;
6663 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6664 // define the nested loops number.
6665 unsigned NestedLoopCount =
6666 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6667 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6668 VarsWithImplicitDSA, B);
6669 if (NestedLoopCount == 0)
6670 return StmtError();
6671
6672 assert((CurContext->isDependentContext() || B.builtAll()) &&
6673 "omp target parallel for loop exprs were not built");
6674
6675 if (!CurContext->isDependentContext()) {
6676 // Finalize the clauses that need pre-built expressions for CodeGen.
6677 for (auto C : Clauses) {
6678 if (auto LC = dyn_cast<OMPLinearClause>(C))
6679 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006680 B.NumIterations, *this, CurScope,
6681 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006682 return StmtError();
6683 }
6684 }
6685
6686 getCurFunction()->setHasBranchProtectedScope();
6687 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6688 NestedLoopCount, Clauses, AStmt,
6689 B, DSAStack->isCancelRegion());
6690}
6691
Samuel Antaodf67fc42016-01-19 19:15:56 +00006692/// \brief Check for existence of a map clause in the list of clauses.
6693static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6694 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6695 I != E; ++I) {
6696 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6697 return true;
6698 }
6699 }
6700
6701 return false;
6702}
6703
Michael Wong65f367f2015-07-21 13:44:28 +00006704StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6705 Stmt *AStmt,
6706 SourceLocation StartLoc,
6707 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006708 if (!AStmt)
6709 return StmtError();
6710
6711 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6712
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006713 // OpenMP [2.10.1, Restrictions, p. 97]
6714 // At least one map clause must appear on the directive.
6715 if (!HasMapClause(Clauses)) {
6716 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6717 getOpenMPDirectiveName(OMPD_target_data);
6718 return StmtError();
6719 }
6720
Michael Wong65f367f2015-07-21 13:44:28 +00006721 getCurFunction()->setHasBranchProtectedScope();
6722
6723 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6724 AStmt);
6725}
6726
Samuel Antaodf67fc42016-01-19 19:15:56 +00006727StmtResult
6728Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6729 SourceLocation StartLoc,
6730 SourceLocation EndLoc) {
6731 // OpenMP [2.10.2, Restrictions, p. 99]
6732 // At least one map clause must appear on the directive.
6733 if (!HasMapClause(Clauses)) {
6734 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6735 << getOpenMPDirectiveName(OMPD_target_enter_data);
6736 return StmtError();
6737 }
6738
6739 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6740 Clauses);
6741}
6742
Samuel Antao72590762016-01-19 20:04:50 +00006743StmtResult
6744Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6745 SourceLocation StartLoc,
6746 SourceLocation EndLoc) {
6747 // OpenMP [2.10.3, Restrictions, p. 102]
6748 // At least one map clause must appear on the directive.
6749 if (!HasMapClause(Clauses)) {
6750 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6751 << getOpenMPDirectiveName(OMPD_target_exit_data);
6752 return StmtError();
6753 }
6754
6755 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6756}
6757
Samuel Antao686c70c2016-05-26 17:30:50 +00006758StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6759 SourceLocation StartLoc,
6760 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006761 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00006762 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00006763 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00006764 seenMotionClause = true;
6765 }
Samuel Antao686c70c2016-05-26 17:30:50 +00006766 if (!seenMotionClause) {
6767 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6768 return StmtError();
6769 }
6770 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6771}
6772
Alexey Bataev13314bf2014-10-09 04:18:56 +00006773StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6774 Stmt *AStmt, SourceLocation StartLoc,
6775 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006776 if (!AStmt)
6777 return StmtError();
6778
Alexey Bataev13314bf2014-10-09 04:18:56 +00006779 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6780 // 1.2.2 OpenMP Language Terminology
6781 // Structured block - An executable statement with a single entry at the
6782 // top and a single exit at the bottom.
6783 // The point of exit cannot be a branch out of the structured block.
6784 // longjmp() and throw() must not violate the entry/exit criteria.
6785 CS->getCapturedDecl()->setNothrow();
6786
6787 getCurFunction()->setHasBranchProtectedScope();
6788
6789 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6790}
6791
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006792StmtResult
6793Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6794 SourceLocation EndLoc,
6795 OpenMPDirectiveKind CancelRegion) {
6796 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6797 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6798 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6799 << getOpenMPDirectiveName(CancelRegion);
6800 return StmtError();
6801 }
6802 if (DSAStack->isParentNowaitRegion()) {
6803 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6804 return StmtError();
6805 }
6806 if (DSAStack->isParentOrderedRegion()) {
6807 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6808 return StmtError();
6809 }
6810 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6811 CancelRegion);
6812}
6813
Alexey Bataev87933c72015-09-18 08:07:34 +00006814StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6815 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006816 SourceLocation EndLoc,
6817 OpenMPDirectiveKind CancelRegion) {
6818 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6819 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6820 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6821 << getOpenMPDirectiveName(CancelRegion);
6822 return StmtError();
6823 }
6824 if (DSAStack->isParentNowaitRegion()) {
6825 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6826 return StmtError();
6827 }
6828 if (DSAStack->isParentOrderedRegion()) {
6829 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6830 return StmtError();
6831 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006832 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006833 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6834 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006835}
6836
Alexey Bataev382967a2015-12-08 12:06:20 +00006837static bool checkGrainsizeNumTasksClauses(Sema &S,
6838 ArrayRef<OMPClause *> Clauses) {
6839 OMPClause *PrevClause = nullptr;
6840 bool ErrorFound = false;
6841 for (auto *C : Clauses) {
6842 if (C->getClauseKind() == OMPC_grainsize ||
6843 C->getClauseKind() == OMPC_num_tasks) {
6844 if (!PrevClause)
6845 PrevClause = C;
6846 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6847 S.Diag(C->getLocStart(),
6848 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6849 << getOpenMPClauseName(C->getClauseKind())
6850 << getOpenMPClauseName(PrevClause->getClauseKind());
6851 S.Diag(PrevClause->getLocStart(),
6852 diag::note_omp_previous_grainsize_num_tasks)
6853 << getOpenMPClauseName(PrevClause->getClauseKind());
6854 ErrorFound = true;
6855 }
6856 }
6857 }
6858 return ErrorFound;
6859}
6860
Alexey Bataev49f6e782015-12-01 04:18:41 +00006861StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6862 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6863 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006864 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006865 if (!AStmt)
6866 return StmtError();
6867
6868 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6869 OMPLoopDirective::HelperExprs B;
6870 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6871 // define the nested loops number.
6872 unsigned NestedLoopCount =
6873 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006874 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006875 VarsWithImplicitDSA, B);
6876 if (NestedLoopCount == 0)
6877 return StmtError();
6878
6879 assert((CurContext->isDependentContext() || B.builtAll()) &&
6880 "omp for loop exprs were not built");
6881
Alexey Bataev382967a2015-12-08 12:06:20 +00006882 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6883 // The grainsize clause and num_tasks clause are mutually exclusive and may
6884 // not appear on the same taskloop directive.
6885 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6886 return StmtError();
6887
Alexey Bataev49f6e782015-12-01 04:18:41 +00006888 getCurFunction()->setHasBranchProtectedScope();
6889 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6890 NestedLoopCount, Clauses, AStmt, B);
6891}
6892
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006893StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6894 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6895 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006896 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006897 if (!AStmt)
6898 return StmtError();
6899
6900 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6901 OMPLoopDirective::HelperExprs B;
6902 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6903 // define the nested loops number.
6904 unsigned NestedLoopCount =
6905 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6906 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6907 VarsWithImplicitDSA, B);
6908 if (NestedLoopCount == 0)
6909 return StmtError();
6910
6911 assert((CurContext->isDependentContext() || B.builtAll()) &&
6912 "omp for loop exprs were not built");
6913
Alexey Bataev5a3af132016-03-29 08:58:54 +00006914 if (!CurContext->isDependentContext()) {
6915 // Finalize the clauses that need pre-built expressions for CodeGen.
6916 for (auto C : Clauses) {
6917 if (auto LC = dyn_cast<OMPLinearClause>(C))
6918 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006919 B.NumIterations, *this, CurScope,
6920 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006921 return StmtError();
6922 }
6923 }
6924
Alexey Bataev382967a2015-12-08 12:06:20 +00006925 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6926 // The grainsize clause and num_tasks clause are mutually exclusive and may
6927 // not appear on the same taskloop directive.
6928 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6929 return StmtError();
6930
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006931 getCurFunction()->setHasBranchProtectedScope();
6932 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6933 NestedLoopCount, Clauses, AStmt, B);
6934}
6935
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006936StmtResult Sema::ActOnOpenMPDistributeDirective(
6937 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6938 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006939 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006940 if (!AStmt)
6941 return StmtError();
6942
6943 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6944 OMPLoopDirective::HelperExprs B;
6945 // In presence of clause 'collapse' with number of loops, it will
6946 // define the nested loops number.
6947 unsigned NestedLoopCount =
6948 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6949 nullptr /*ordered not a clause on distribute*/, AStmt,
6950 *this, *DSAStack, VarsWithImplicitDSA, B);
6951 if (NestedLoopCount == 0)
6952 return StmtError();
6953
6954 assert((CurContext->isDependentContext() || B.builtAll()) &&
6955 "omp for loop exprs were not built");
6956
6957 getCurFunction()->setHasBranchProtectedScope();
6958 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6959 NestedLoopCount, Clauses, AStmt, B);
6960}
6961
Carlo Bertolli9925f152016-06-27 14:55:37 +00006962StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
6963 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6964 SourceLocation EndLoc,
6965 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6966 if (!AStmt)
6967 return StmtError();
6968
6969 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6970 // 1.2.2 OpenMP Language Terminology
6971 // Structured block - An executable statement with a single entry at the
6972 // top and a single exit at the bottom.
6973 // The point of exit cannot be a branch out of the structured block.
6974 // longjmp() and throw() must not violate the entry/exit criteria.
6975 CS->getCapturedDecl()->setNothrow();
6976
6977 OMPLoopDirective::HelperExprs B;
6978 // In presence of clause 'collapse' with number of loops, it will
6979 // define the nested loops number.
6980 unsigned NestedLoopCount = CheckOpenMPLoop(
6981 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
6982 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6983 VarsWithImplicitDSA, B);
6984 if (NestedLoopCount == 0)
6985 return StmtError();
6986
6987 assert((CurContext->isDependentContext() || B.builtAll()) &&
6988 "omp for loop exprs were not built");
6989
6990 getCurFunction()->setHasBranchProtectedScope();
6991 return OMPDistributeParallelForDirective::Create(
6992 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6993}
6994
Kelvin Li4a39add2016-07-05 05:00:15 +00006995StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
6996 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6997 SourceLocation EndLoc,
6998 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6999 if (!AStmt)
7000 return StmtError();
7001
7002 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7003 // 1.2.2 OpenMP Language Terminology
7004 // Structured block - An executable statement with a single entry at the
7005 // top and a single exit at the bottom.
7006 // The point of exit cannot be a branch out of the structured block.
7007 // longjmp() and throw() must not violate the entry/exit criteria.
7008 CS->getCapturedDecl()->setNothrow();
7009
7010 OMPLoopDirective::HelperExprs B;
7011 // In presence of clause 'collapse' with number of loops, it will
7012 // define the nested loops number.
7013 unsigned NestedLoopCount = CheckOpenMPLoop(
7014 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7015 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7016 VarsWithImplicitDSA, B);
7017 if (NestedLoopCount == 0)
7018 return StmtError();
7019
7020 assert((CurContext->isDependentContext() || B.builtAll()) &&
7021 "omp for loop exprs were not built");
7022
7023 getCurFunction()->setHasBranchProtectedScope();
7024 return OMPDistributeParallelForSimdDirective::Create(
7025 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7026}
7027
Kelvin Li787f3fc2016-07-06 04:45:38 +00007028StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7029 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7030 SourceLocation EndLoc,
7031 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7032 if (!AStmt)
7033 return StmtError();
7034
7035 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7036 // 1.2.2 OpenMP Language Terminology
7037 // Structured block - An executable statement with a single entry at the
7038 // top and a single exit at the bottom.
7039 // The point of exit cannot be a branch out of the structured block.
7040 // longjmp() and throw() must not violate the entry/exit criteria.
7041 CS->getCapturedDecl()->setNothrow();
7042
7043 OMPLoopDirective::HelperExprs B;
7044 // In presence of clause 'collapse' with number of loops, it will
7045 // define the nested loops number.
7046 unsigned NestedLoopCount =
7047 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7048 nullptr /*ordered not a clause on distribute*/, AStmt,
7049 *this, *DSAStack, VarsWithImplicitDSA, B);
7050 if (NestedLoopCount == 0)
7051 return StmtError();
7052
7053 assert((CurContext->isDependentContext() || B.builtAll()) &&
7054 "omp for loop exprs were not built");
7055
7056 getCurFunction()->setHasBranchProtectedScope();
7057 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7058 NestedLoopCount, Clauses, AStmt, B);
7059}
7060
Alexey Bataeved09d242014-05-28 05:53:51 +00007061OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007062 SourceLocation StartLoc,
7063 SourceLocation LParenLoc,
7064 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007065 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007066 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007067 case OMPC_final:
7068 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7069 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007070 case OMPC_num_threads:
7071 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7072 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007073 case OMPC_safelen:
7074 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7075 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007076 case OMPC_simdlen:
7077 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7078 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007079 case OMPC_collapse:
7080 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7081 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007082 case OMPC_ordered:
7083 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7084 break;
Michael Wonge710d542015-08-07 16:16:36 +00007085 case OMPC_device:
7086 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7087 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007088 case OMPC_num_teams:
7089 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7090 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007091 case OMPC_thread_limit:
7092 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7093 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007094 case OMPC_priority:
7095 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7096 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007097 case OMPC_grainsize:
7098 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7099 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007100 case OMPC_num_tasks:
7101 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7102 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007103 case OMPC_hint:
7104 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7105 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007106 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007107 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007108 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007109 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007110 case OMPC_private:
7111 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007112 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007113 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007114 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007115 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007116 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007117 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007118 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007119 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007120 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007121 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007122 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007123 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007124 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007125 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007126 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007127 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007128 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007129 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007130 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007131 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007132 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007133 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007134 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007135 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007136 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007137 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007138 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007139 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007140 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007141 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007142 llvm_unreachable("Clause is not allowed.");
7143 }
7144 return Res;
7145}
7146
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007147OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7148 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007149 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007150 SourceLocation NameModifierLoc,
7151 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007152 SourceLocation EndLoc) {
7153 Expr *ValExpr = Condition;
7154 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7155 !Condition->isInstantiationDependent() &&
7156 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007157 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007158 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007159 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007160
Richard Smith03a4aa32016-06-23 19:02:52 +00007161 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007162 }
7163
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007164 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7165 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007166}
7167
Alexey Bataev3778b602014-07-17 07:32:53 +00007168OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7169 SourceLocation StartLoc,
7170 SourceLocation LParenLoc,
7171 SourceLocation EndLoc) {
7172 Expr *ValExpr = Condition;
7173 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7174 !Condition->isInstantiationDependent() &&
7175 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007176 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007177 if (Val.isInvalid())
7178 return nullptr;
7179
Richard Smith03a4aa32016-06-23 19:02:52 +00007180 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007181 }
7182
7183 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7184}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007185ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7186 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007187 if (!Op)
7188 return ExprError();
7189
7190 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7191 public:
7192 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007193 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007194 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7195 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007196 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7197 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007198 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7199 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007200 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7201 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007202 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7203 QualType T,
7204 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007205 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7206 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007207 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7208 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007209 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007210 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007211 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007212 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7213 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007214 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7215 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007216 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7217 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007218 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007219 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007220 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007221 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7222 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007223 llvm_unreachable("conversion functions are permitted");
7224 }
7225 } ConvertDiagnoser;
7226 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7227}
7228
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007229static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007230 OpenMPClauseKind CKind,
7231 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007232 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7233 !ValExpr->isInstantiationDependent()) {
7234 SourceLocation Loc = ValExpr->getExprLoc();
7235 ExprResult Value =
7236 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7237 if (Value.isInvalid())
7238 return false;
7239
7240 ValExpr = Value.get();
7241 // The expression must evaluate to a non-negative integer value.
7242 llvm::APSInt Result;
7243 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007244 Result.isSigned() &&
7245 !((!StrictlyPositive && Result.isNonNegative()) ||
7246 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007247 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007248 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7249 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007250 return false;
7251 }
7252 }
7253 return true;
7254}
7255
Alexey Bataev568a8332014-03-06 06:15:19 +00007256OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7257 SourceLocation StartLoc,
7258 SourceLocation LParenLoc,
7259 SourceLocation EndLoc) {
7260 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007261
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007262 // OpenMP [2.5, Restrictions]
7263 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007264 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7265 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007266 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007267
Alexey Bataeved09d242014-05-28 05:53:51 +00007268 return new (Context)
7269 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007270}
7271
Alexey Bataev62c87d22014-03-21 04:51:18 +00007272ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007273 OpenMPClauseKind CKind,
7274 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007275 if (!E)
7276 return ExprError();
7277 if (E->isValueDependent() || E->isTypeDependent() ||
7278 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007279 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007280 llvm::APSInt Result;
7281 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7282 if (ICE.isInvalid())
7283 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007284 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7285 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007286 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007287 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7288 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007289 return ExprError();
7290 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007291 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7292 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7293 << E->getSourceRange();
7294 return ExprError();
7295 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007296 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7297 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007298 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007299 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007300 return ICE;
7301}
7302
7303OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7304 SourceLocation LParenLoc,
7305 SourceLocation EndLoc) {
7306 // OpenMP [2.8.1, simd construct, Description]
7307 // The parameter of the safelen clause must be a constant
7308 // positive integer expression.
7309 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7310 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007311 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007312 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007313 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007314}
7315
Alexey Bataev66b15b52015-08-21 11:14:16 +00007316OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7317 SourceLocation LParenLoc,
7318 SourceLocation EndLoc) {
7319 // OpenMP [2.8.1, simd construct, Description]
7320 // The parameter of the simdlen clause must be a constant
7321 // positive integer expression.
7322 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7323 if (Simdlen.isInvalid())
7324 return nullptr;
7325 return new (Context)
7326 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7327}
7328
Alexander Musman64d33f12014-06-04 07:53:32 +00007329OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7330 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007331 SourceLocation LParenLoc,
7332 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007333 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007334 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007335 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007336 // The parameter of the collapse clause must be a constant
7337 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007338 ExprResult NumForLoopsResult =
7339 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7340 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007341 return nullptr;
7342 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007343 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007344}
7345
Alexey Bataev10e775f2015-07-30 11:36:16 +00007346OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7347 SourceLocation EndLoc,
7348 SourceLocation LParenLoc,
7349 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007350 // OpenMP [2.7.1, loop construct, Description]
7351 // OpenMP [2.8.1, simd construct, Description]
7352 // OpenMP [2.9.6, distribute construct, Description]
7353 // The parameter of the ordered clause must be a constant
7354 // positive integer expression if any.
7355 if (NumForLoops && LParenLoc.isValid()) {
7356 ExprResult NumForLoopsResult =
7357 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7358 if (NumForLoopsResult.isInvalid())
7359 return nullptr;
7360 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007361 } else
7362 NumForLoops = nullptr;
7363 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007364 return new (Context)
7365 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7366}
7367
Alexey Bataeved09d242014-05-28 05:53:51 +00007368OMPClause *Sema::ActOnOpenMPSimpleClause(
7369 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7370 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007371 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007372 switch (Kind) {
7373 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007374 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007375 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7376 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007377 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007378 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007379 Res = ActOnOpenMPProcBindClause(
7380 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7381 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007382 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007383 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007384 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007385 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007386 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007387 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007388 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007389 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007390 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007391 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007392 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007393 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007394 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007395 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007396 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007397 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007398 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007399 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007400 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007401 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007402 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007403 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007404 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007405 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007406 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007407 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007408 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007409 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007410 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007411 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007412 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007413 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007414 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007415 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007416 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007417 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007418 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007419 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007420 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007421 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007422 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007423 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007424 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007425 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007426 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007427 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007428 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007429 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007430 llvm_unreachable("Clause is not allowed.");
7431 }
7432 return Res;
7433}
7434
Alexey Bataev6402bca2015-12-28 07:25:51 +00007435static std::string
7436getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7437 ArrayRef<unsigned> Exclude = llvm::None) {
7438 std::string Values;
7439 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7440 unsigned Skipped = Exclude.size();
7441 auto S = Exclude.begin(), E = Exclude.end();
7442 for (unsigned i = First; i < Last; ++i) {
7443 if (std::find(S, E, i) != E) {
7444 --Skipped;
7445 continue;
7446 }
7447 Values += "'";
7448 Values += getOpenMPSimpleClauseTypeName(K, i);
7449 Values += "'";
7450 if (i == Bound - Skipped)
7451 Values += " or ";
7452 else if (i != Bound + 1 - Skipped)
7453 Values += ", ";
7454 }
7455 return Values;
7456}
7457
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007458OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7459 SourceLocation KindKwLoc,
7460 SourceLocation StartLoc,
7461 SourceLocation LParenLoc,
7462 SourceLocation EndLoc) {
7463 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007464 static_assert(OMPC_DEFAULT_unknown > 0,
7465 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007466 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007467 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7468 /*Last=*/OMPC_DEFAULT_unknown)
7469 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007470 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007471 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007472 switch (Kind) {
7473 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007474 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007475 break;
7476 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007477 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007478 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007479 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007480 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007481 break;
7482 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007483 return new (Context)
7484 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007485}
7486
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007487OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7488 SourceLocation KindKwLoc,
7489 SourceLocation StartLoc,
7490 SourceLocation LParenLoc,
7491 SourceLocation EndLoc) {
7492 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007493 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007494 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7495 /*Last=*/OMPC_PROC_BIND_unknown)
7496 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007497 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007498 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007499 return new (Context)
7500 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007501}
7502
Alexey Bataev56dafe82014-06-20 07:16:17 +00007503OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007504 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007505 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007506 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007507 SourceLocation EndLoc) {
7508 OMPClause *Res = nullptr;
7509 switch (Kind) {
7510 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007511 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7512 assert(Argument.size() == NumberOfElements &&
7513 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007514 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007515 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7516 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7517 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7518 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7519 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007520 break;
7521 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007522 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7523 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7524 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7525 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007526 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007527 case OMPC_dist_schedule:
7528 Res = ActOnOpenMPDistScheduleClause(
7529 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7530 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7531 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007532 case OMPC_defaultmap:
7533 enum { Modifier, DefaultmapKind };
7534 Res = ActOnOpenMPDefaultmapClause(
7535 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7536 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7537 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7538 ArgumentLoc[DefaultmapKind], EndLoc);
7539 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007540 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007541 case OMPC_num_threads:
7542 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007543 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007544 case OMPC_collapse:
7545 case OMPC_default:
7546 case OMPC_proc_bind:
7547 case OMPC_private:
7548 case OMPC_firstprivate:
7549 case OMPC_lastprivate:
7550 case OMPC_shared:
7551 case OMPC_reduction:
7552 case OMPC_linear:
7553 case OMPC_aligned:
7554 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007555 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007556 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007557 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007558 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007559 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007560 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007561 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007562 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007563 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007564 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007565 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007566 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007567 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007568 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007569 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007570 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007571 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007572 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007573 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007574 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007575 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007576 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007577 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007578 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007579 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007580 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007581 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007582 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007583 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007584 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007585 llvm_unreachable("Clause is not allowed.");
7586 }
7587 return Res;
7588}
7589
Alexey Bataev6402bca2015-12-28 07:25:51 +00007590static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7591 OpenMPScheduleClauseModifier M2,
7592 SourceLocation M1Loc, SourceLocation M2Loc) {
7593 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7594 SmallVector<unsigned, 2> Excluded;
7595 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7596 Excluded.push_back(M2);
7597 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7598 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7599 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7600 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7601 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7602 << getListOfPossibleValues(OMPC_schedule,
7603 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7604 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7605 Excluded)
7606 << getOpenMPClauseName(OMPC_schedule);
7607 return true;
7608 }
7609 return false;
7610}
7611
Alexey Bataev56dafe82014-06-20 07:16:17 +00007612OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007613 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007614 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007615 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7616 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7617 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7618 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7619 return nullptr;
7620 // OpenMP, 2.7.1, Loop Construct, Restrictions
7621 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7622 // but not both.
7623 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7624 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7625 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7626 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7627 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7628 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7629 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7630 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7631 return nullptr;
7632 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007633 if (Kind == OMPC_SCHEDULE_unknown) {
7634 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007635 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7636 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7637 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7638 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7639 Exclude);
7640 } else {
7641 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7642 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007643 }
7644 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7645 << Values << getOpenMPClauseName(OMPC_schedule);
7646 return nullptr;
7647 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007648 // OpenMP, 2.7.1, Loop Construct, Restrictions
7649 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7650 // schedule(guided).
7651 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7652 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7653 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7654 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7655 diag::err_omp_schedule_nonmonotonic_static);
7656 return nullptr;
7657 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007658 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007659 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007660 if (ChunkSize) {
7661 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7662 !ChunkSize->isInstantiationDependent() &&
7663 !ChunkSize->containsUnexpandedParameterPack()) {
7664 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7665 ExprResult Val =
7666 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7667 if (Val.isInvalid())
7668 return nullptr;
7669
7670 ValExpr = Val.get();
7671
7672 // OpenMP [2.7.1, Restrictions]
7673 // chunk_size must be a loop invariant integer expression with a positive
7674 // value.
7675 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007676 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7677 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7678 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007679 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007680 return nullptr;
7681 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007682 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7683 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007684 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7685 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7686 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007687 }
7688 }
7689 }
7690
Alexey Bataev6402bca2015-12-28 07:25:51 +00007691 return new (Context)
7692 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007693 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007694}
7695
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007696OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7697 SourceLocation StartLoc,
7698 SourceLocation EndLoc) {
7699 OMPClause *Res = nullptr;
7700 switch (Kind) {
7701 case OMPC_ordered:
7702 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7703 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007704 case OMPC_nowait:
7705 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7706 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007707 case OMPC_untied:
7708 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7709 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007710 case OMPC_mergeable:
7711 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7712 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007713 case OMPC_read:
7714 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7715 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007716 case OMPC_write:
7717 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7718 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007719 case OMPC_update:
7720 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7721 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007722 case OMPC_capture:
7723 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7724 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007725 case OMPC_seq_cst:
7726 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7727 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007728 case OMPC_threads:
7729 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7730 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007731 case OMPC_simd:
7732 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7733 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007734 case OMPC_nogroup:
7735 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7736 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007737 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007738 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007739 case OMPC_num_threads:
7740 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007741 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007742 case OMPC_collapse:
7743 case OMPC_schedule:
7744 case OMPC_private:
7745 case OMPC_firstprivate:
7746 case OMPC_lastprivate:
7747 case OMPC_shared:
7748 case OMPC_reduction:
7749 case OMPC_linear:
7750 case OMPC_aligned:
7751 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007752 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007753 case OMPC_default:
7754 case OMPC_proc_bind:
7755 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007756 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007757 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007758 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007759 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007760 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007761 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007762 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007763 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007764 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007765 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007766 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007767 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007768 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007769 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007770 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007771 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007772 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007773 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007774 llvm_unreachable("Clause is not allowed.");
7775 }
7776 return Res;
7777}
7778
Alexey Bataev236070f2014-06-20 11:19:47 +00007779OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7780 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007781 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007782 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7783}
7784
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007785OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7786 SourceLocation EndLoc) {
7787 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7788}
7789
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007790OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7791 SourceLocation EndLoc) {
7792 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7793}
7794
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007795OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7796 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007797 return new (Context) OMPReadClause(StartLoc, EndLoc);
7798}
7799
Alexey Bataevdea47612014-07-23 07:46:59 +00007800OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7801 SourceLocation EndLoc) {
7802 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7803}
7804
Alexey Bataev67a4f222014-07-23 10:25:33 +00007805OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7806 SourceLocation EndLoc) {
7807 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7808}
7809
Alexey Bataev459dec02014-07-24 06:46:57 +00007810OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7811 SourceLocation EndLoc) {
7812 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7813}
7814
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007815OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7816 SourceLocation EndLoc) {
7817 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7818}
7819
Alexey Bataev346265e2015-09-25 10:37:12 +00007820OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7821 SourceLocation EndLoc) {
7822 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7823}
7824
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007825OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7826 SourceLocation EndLoc) {
7827 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7828}
7829
Alexey Bataevb825de12015-12-07 10:51:44 +00007830OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7831 SourceLocation EndLoc) {
7832 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7833}
7834
Alexey Bataevc5e02582014-06-16 07:08:35 +00007835OMPClause *Sema::ActOnOpenMPVarListClause(
7836 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7837 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7838 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007839 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007840 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7841 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7842 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007843 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007844 switch (Kind) {
7845 case OMPC_private:
7846 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7847 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007848 case OMPC_firstprivate:
7849 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7850 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007851 case OMPC_lastprivate:
7852 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7853 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007854 case OMPC_shared:
7855 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7856 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007857 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007858 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7859 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007860 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007861 case OMPC_linear:
7862 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007863 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007864 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007865 case OMPC_aligned:
7866 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7867 ColonLoc, EndLoc);
7868 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007869 case OMPC_copyin:
7870 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7871 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007872 case OMPC_copyprivate:
7873 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7874 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007875 case OMPC_flush:
7876 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7877 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007878 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007879 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7880 StartLoc, LParenLoc, EndLoc);
7881 break;
7882 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007883 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7884 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7885 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007886 break;
Samuel Antao661c0902016-05-26 17:39:58 +00007887 case OMPC_to:
7888 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
7889 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00007890 case OMPC_from:
7891 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
7892 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00007893 case OMPC_use_device_ptr:
7894 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
7895 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00007896 case OMPC_is_device_ptr:
7897 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
7898 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007899 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007900 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007901 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007902 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007903 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007904 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007905 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007906 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007907 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007908 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007909 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007910 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007911 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007912 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007913 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007914 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007915 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007916 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007917 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007918 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007919 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007920 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007921 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007922 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007923 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007924 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007925 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007926 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007927 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007928 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007929 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007930 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007931 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007932 llvm_unreachable("Clause is not allowed.");
7933 }
7934 return Res;
7935}
7936
Alexey Bataev90c228f2016-02-08 09:29:13 +00007937ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007938 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007939 ExprResult Res = BuildDeclRefExpr(
7940 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7941 if (!Res.isUsable())
7942 return ExprError();
7943 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7944 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7945 if (!Res.isUsable())
7946 return ExprError();
7947 }
7948 if (VK != VK_LValue && Res.get()->isGLValue()) {
7949 Res = DefaultLvalueConversion(Res.get());
7950 if (!Res.isUsable())
7951 return ExprError();
7952 }
7953 return Res;
7954}
7955
Alexey Bataev60da77e2016-02-29 05:54:20 +00007956static std::pair<ValueDecl *, bool>
7957getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7958 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007959 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7960 RefExpr->containsUnexpandedParameterPack())
7961 return std::make_pair(nullptr, true);
7962
Alexey Bataevd985eda2016-02-10 11:29:16 +00007963 // OpenMP [3.1, C/C++]
7964 // A list item is a variable name.
7965 // OpenMP [2.9.3.3, Restrictions, p.1]
7966 // A variable that is part of another variable (as an array or
7967 // structure element) cannot appear in a private clause.
7968 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007969 enum {
7970 NoArrayExpr = -1,
7971 ArraySubscript = 0,
7972 OMPArraySection = 1
7973 } IsArrayExpr = NoArrayExpr;
7974 if (AllowArraySection) {
7975 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7976 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7977 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7978 Base = TempASE->getBase()->IgnoreParenImpCasts();
7979 RefExpr = Base;
7980 IsArrayExpr = ArraySubscript;
7981 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7982 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7983 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7984 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7985 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7986 Base = TempASE->getBase()->IgnoreParenImpCasts();
7987 RefExpr = Base;
7988 IsArrayExpr = OMPArraySection;
7989 }
7990 }
7991 ELoc = RefExpr->getExprLoc();
7992 ERange = RefExpr->getSourceRange();
7993 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007994 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7995 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7996 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7997 (S.getCurrentThisType().isNull() || !ME ||
7998 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7999 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008000 if (IsArrayExpr != NoArrayExpr)
8001 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8002 << ERange;
8003 else {
8004 S.Diag(ELoc,
8005 AllowArraySection
8006 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8007 : diag::err_omp_expected_var_name_member_expr)
8008 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8009 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008010 return std::make_pair(nullptr, false);
8011 }
8012 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8013}
8014
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008015OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8016 SourceLocation StartLoc,
8017 SourceLocation LParenLoc,
8018 SourceLocation EndLoc) {
8019 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008020 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008021 for (auto &RefExpr : VarList) {
8022 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008023 SourceLocation ELoc;
8024 SourceRange ERange;
8025 Expr *SimpleRefExpr = RefExpr;
8026 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008027 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008028 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008029 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008030 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008031 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008032 ValueDecl *D = Res.first;
8033 if (!D)
8034 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008035
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008036 QualType Type = D->getType();
8037 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008038
8039 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8040 // A variable that appears in a private clause must not have an incomplete
8041 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008042 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008043 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008044 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008045
Alexey Bataev758e55e2013-09-06 18:03:48 +00008046 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8047 // in a Construct]
8048 // Variables with the predetermined data-sharing attributes may not be
8049 // listed in data-sharing attributes clauses, except for the cases
8050 // listed below. For these exceptions only, listing a predetermined
8051 // variable in a data-sharing attribute clause is allowed and overrides
8052 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008053 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008054 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008055 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8056 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008057 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008058 continue;
8059 }
8060
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008061 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008062 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008063 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008064 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8065 << getOpenMPClauseName(OMPC_private) << Type
8066 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8067 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008068 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008069 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008070 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008071 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008072 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008073 continue;
8074 }
8075
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008076 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8077 // A list item cannot appear in both a map clause and a data-sharing
8078 // attribute clause on the same construct
8079 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008080 if (DSAStack->checkMappableExprComponentListsForDecl(
8081 VD, /* CurrentRegionOnly = */ true,
8082 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8083 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008084 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8085 << getOpenMPClauseName(OMPC_private)
8086 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8087 ReportOriginalDSA(*this, DSAStack, D, DVar);
8088 continue;
8089 }
8090 }
8091
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008092 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8093 // A variable of class type (or array thereof) that appears in a private
8094 // clause requires an accessible, unambiguous default constructor for the
8095 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008096 // Generate helper private variable and initialize it with the default
8097 // value. The address of the original variable is replaced by the address of
8098 // the new private variable in CodeGen. This new variable is not added to
8099 // IdResolver, so the code in the OpenMP region uses original variable for
8100 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008101 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008102 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8103 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008104 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008105 if (VDPrivate->isInvalidDecl())
8106 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008107 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008108 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008109
Alexey Bataev90c228f2016-02-08 09:29:13 +00008110 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008111 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008112 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008113 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008114 Vars.push_back((VD || CurContext->isDependentContext())
8115 ? RefExpr->IgnoreParens()
8116 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008117 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008118 }
8119
Alexey Bataeved09d242014-05-28 05:53:51 +00008120 if (Vars.empty())
8121 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008122
Alexey Bataev03b340a2014-10-21 03:16:40 +00008123 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8124 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008125}
8126
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008127namespace {
8128class DiagsUninitializedSeveretyRAII {
8129private:
8130 DiagnosticsEngine &Diags;
8131 SourceLocation SavedLoc;
8132 bool IsIgnored;
8133
8134public:
8135 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8136 bool IsIgnored)
8137 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8138 if (!IsIgnored) {
8139 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8140 /*Map*/ diag::Severity::Ignored, Loc);
8141 }
8142 }
8143 ~DiagsUninitializedSeveretyRAII() {
8144 if (!IsIgnored)
8145 Diags.popMappings(SavedLoc);
8146 }
8147};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008148}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008149
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008150OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8151 SourceLocation StartLoc,
8152 SourceLocation LParenLoc,
8153 SourceLocation EndLoc) {
8154 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008155 SmallVector<Expr *, 8> PrivateCopies;
8156 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008157 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008158 bool IsImplicitClause =
8159 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8160 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8161
Alexey Bataeved09d242014-05-28 05:53:51 +00008162 for (auto &RefExpr : VarList) {
8163 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008164 SourceLocation ELoc;
8165 SourceRange ERange;
8166 Expr *SimpleRefExpr = RefExpr;
8167 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008168 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008169 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008170 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008171 PrivateCopies.push_back(nullptr);
8172 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008173 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008174 ValueDecl *D = Res.first;
8175 if (!D)
8176 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008177
Alexey Bataev60da77e2016-02-29 05:54:20 +00008178 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008179 QualType Type = D->getType();
8180 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008181
8182 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8183 // A variable that appears in a private clause must not have an incomplete
8184 // type or a reference type.
8185 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008186 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008187 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008188 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008189
8190 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8191 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008192 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008193 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008194 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008195
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008196 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008197 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008198 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008199 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008200 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008201 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008202 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8203 // A list item that specifies a given variable may not appear in more
8204 // than one clause on the same directive, except that a variable may be
8205 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008206 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008207 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008208 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008209 << getOpenMPClauseName(DVar.CKind)
8210 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008211 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008212 continue;
8213 }
8214
8215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8216 // in a Construct]
8217 // Variables with the predetermined data-sharing attributes may not be
8218 // listed in data-sharing attributes clauses, except for the cases
8219 // listed below. For these exceptions only, listing a predetermined
8220 // variable in a data-sharing attribute clause is allowed and overrides
8221 // the variable's predetermined data-sharing attributes.
8222 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8223 // in a Construct, C/C++, p.2]
8224 // Variables with const-qualified type having no mutable member may be
8225 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008226 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008227 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8228 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008229 << getOpenMPClauseName(DVar.CKind)
8230 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008231 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008232 continue;
8233 }
8234
Alexey Bataevf29276e2014-06-18 04:14:57 +00008235 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008236 // OpenMP [2.9.3.4, Restrictions, p.2]
8237 // A list item that is private within a parallel region must not appear
8238 // in a firstprivate clause on a worksharing construct if any of the
8239 // worksharing regions arising from the worksharing construct ever bind
8240 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008241 if (isOpenMPWorksharingDirective(CurrDir) &&
8242 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008243 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008244 if (DVar.CKind != OMPC_shared &&
8245 (isOpenMPParallelDirective(DVar.DKind) ||
8246 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008247 Diag(ELoc, diag::err_omp_required_access)
8248 << getOpenMPClauseName(OMPC_firstprivate)
8249 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008250 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008251 continue;
8252 }
8253 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008254 // OpenMP [2.9.3.4, Restrictions, p.3]
8255 // A list item that appears in a reduction clause of a parallel construct
8256 // must not appear in a firstprivate clause on a worksharing or task
8257 // construct if any of the worksharing or task regions arising from the
8258 // worksharing or task construct ever bind to any of the parallel regions
8259 // arising from the parallel construct.
8260 // OpenMP [2.9.3.4, Restrictions, p.4]
8261 // A list item that appears in a reduction clause in worksharing
8262 // construct must not appear in a firstprivate clause in a task construct
8263 // encountered during execution of any of the worksharing regions arising
8264 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008265 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008266 DVar = DSAStack->hasInnermostDSA(
8267 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8268 [](OpenMPDirectiveKind K) -> bool {
8269 return isOpenMPParallelDirective(K) ||
8270 isOpenMPWorksharingDirective(K);
8271 },
8272 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008273 if (DVar.CKind == OMPC_reduction &&
8274 (isOpenMPParallelDirective(DVar.DKind) ||
8275 isOpenMPWorksharingDirective(DVar.DKind))) {
8276 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8277 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008278 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008279 continue;
8280 }
8281 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008282
8283 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8284 // A list item that is private within a teams region must not appear in a
8285 // firstprivate clause on a distribute construct if any of the distribute
8286 // regions arising from the distribute construct ever bind to any of the
8287 // teams regions arising from the teams construct.
8288 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8289 // A list item that appears in a reduction clause of a teams construct
8290 // must not appear in a firstprivate clause on a distribute construct if
8291 // any of the distribute regions arising from the distribute construct
8292 // ever bind to any of the teams regions arising from the teams construct.
8293 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8294 // A list item may appear in a firstprivate or lastprivate clause but not
8295 // both.
8296 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008297 DVar = DSAStack->hasInnermostDSA(
8298 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8299 [](OpenMPDirectiveKind K) -> bool {
8300 return isOpenMPTeamsDirective(K);
8301 },
8302 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008303 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8304 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008305 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008306 continue;
8307 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008308 DVar = DSAStack->hasInnermostDSA(
8309 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8310 [](OpenMPDirectiveKind K) -> bool {
8311 return isOpenMPTeamsDirective(K);
8312 },
8313 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008314 if (DVar.CKind == OMPC_reduction &&
8315 isOpenMPTeamsDirective(DVar.DKind)) {
8316 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008317 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008318 continue;
8319 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008320 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008321 if (DVar.CKind == OMPC_lastprivate) {
8322 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008323 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008324 continue;
8325 }
8326 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008327 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8328 // A list item cannot appear in both a map clause and a data-sharing
8329 // attribute clause on the same construct
8330 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008331 if (DSAStack->checkMappableExprComponentListsForDecl(
8332 VD, /* CurrentRegionOnly = */ true,
8333 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8334 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008335 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8336 << getOpenMPClauseName(OMPC_firstprivate)
8337 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8338 ReportOriginalDSA(*this, DSAStack, D, DVar);
8339 continue;
8340 }
8341 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008342 }
8343
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008344 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008345 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008346 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008347 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8348 << getOpenMPClauseName(OMPC_firstprivate) << Type
8349 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8350 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008351 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008352 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008353 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008354 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008355 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008356 continue;
8357 }
8358
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008359 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008360 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8361 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008362 // Generate helper private variable and initialize it with the value of the
8363 // original variable. The address of the original variable is replaced by
8364 // the address of the new private variable in the CodeGen. This new variable
8365 // is not added to IdResolver, so the code in the OpenMP region uses
8366 // original variable for proper diagnostics and variable capturing.
8367 Expr *VDInitRefExpr = nullptr;
8368 // For arrays generate initializer for single element and replace it by the
8369 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008370 if (Type->isArrayType()) {
8371 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008372 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008373 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008374 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008375 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008376 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008377 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008378 InitializedEntity Entity =
8379 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008380 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8381
8382 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8383 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8384 if (Result.isInvalid())
8385 VDPrivate->setInvalidDecl();
8386 else
8387 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008388 // Remove temp variable declaration.
8389 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008390 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008391 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8392 ".firstprivate.temp");
8393 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8394 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008395 AddInitializerToDecl(VDPrivate,
8396 DefaultLvalueConversion(VDInitRefExpr).get(),
8397 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008398 }
8399 if (VDPrivate->isInvalidDecl()) {
8400 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008401 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008402 diag::note_omp_task_predetermined_firstprivate_here);
8403 }
8404 continue;
8405 }
8406 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008407 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008408 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8409 RefExpr->getExprLoc());
8410 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008411 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008412 if (TopDVar.CKind == OMPC_lastprivate)
8413 Ref = TopDVar.PrivateCopy;
8414 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008415 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008416 if (!IsOpenMPCapturedDecl(D))
8417 ExprCaptures.push_back(Ref->getDecl());
8418 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008419 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008420 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008421 Vars.push_back((VD || CurContext->isDependentContext())
8422 ? RefExpr->IgnoreParens()
8423 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008424 PrivateCopies.push_back(VDPrivateRefExpr);
8425 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008426 }
8427
Alexey Bataeved09d242014-05-28 05:53:51 +00008428 if (Vars.empty())
8429 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008430
8431 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008432 Vars, PrivateCopies, Inits,
8433 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008434}
8435
Alexander Musman1bb328c2014-06-04 13:06:39 +00008436OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8437 SourceLocation StartLoc,
8438 SourceLocation LParenLoc,
8439 SourceLocation EndLoc) {
8440 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008441 SmallVector<Expr *, 8> SrcExprs;
8442 SmallVector<Expr *, 8> DstExprs;
8443 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008444 SmallVector<Decl *, 4> ExprCaptures;
8445 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008446 for (auto &RefExpr : VarList) {
8447 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008448 SourceLocation ELoc;
8449 SourceRange ERange;
8450 Expr *SimpleRefExpr = RefExpr;
8451 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008452 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008453 // It will be analyzed later.
8454 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008455 SrcExprs.push_back(nullptr);
8456 DstExprs.push_back(nullptr);
8457 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008458 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008459 ValueDecl *D = Res.first;
8460 if (!D)
8461 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008462
Alexey Bataev74caaf22016-02-20 04:09:36 +00008463 QualType Type = D->getType();
8464 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008465
8466 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8467 // A variable that appears in a lastprivate clause must not have an
8468 // incomplete type or a reference type.
8469 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008470 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008471 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008472 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008473
8474 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8475 // in a Construct]
8476 // Variables with the predetermined data-sharing attributes may not be
8477 // listed in data-sharing attributes clauses, except for the cases
8478 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008479 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008480 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8481 DVar.CKind != OMPC_firstprivate &&
8482 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8483 Diag(ELoc, diag::err_omp_wrong_dsa)
8484 << getOpenMPClauseName(DVar.CKind)
8485 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008486 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008487 continue;
8488 }
8489
Alexey Bataevf29276e2014-06-18 04:14:57 +00008490 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8491 // OpenMP [2.14.3.5, Restrictions, p.2]
8492 // A list item that is private within a parallel region, or that appears in
8493 // the reduction clause of a parallel construct, must not appear in a
8494 // lastprivate clause on a worksharing construct if any of the corresponding
8495 // worksharing regions ever binds to any of the corresponding parallel
8496 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008497 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008498 if (isOpenMPWorksharingDirective(CurrDir) &&
8499 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008500 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008501 if (DVar.CKind != OMPC_shared) {
8502 Diag(ELoc, diag::err_omp_required_access)
8503 << getOpenMPClauseName(OMPC_lastprivate)
8504 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008505 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008506 continue;
8507 }
8508 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008509
8510 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8511 // A list item may appear in a firstprivate or lastprivate clause but not
8512 // both.
8513 if (CurrDir == OMPD_distribute) {
8514 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8515 if (DVar.CKind == OMPC_firstprivate) {
8516 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8517 ReportOriginalDSA(*this, DSAStack, D, DVar);
8518 continue;
8519 }
8520 }
8521
Alexander Musman1bb328c2014-06-04 13:06:39 +00008522 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008523 // A variable of class type (or array thereof) that appears in a
8524 // lastprivate clause requires an accessible, unambiguous default
8525 // constructor for the class type, unless the list item is also specified
8526 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008527 // A variable of class type (or array thereof) that appears in a
8528 // lastprivate clause requires an accessible, unambiguous copy assignment
8529 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008530 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008531 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008532 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008533 D->hasAttrs() ? &D->getAttrs() : nullptr);
8534 auto *PseudoSrcExpr =
8535 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008536 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008537 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008538 D->hasAttrs() ? &D->getAttrs() : nullptr);
8539 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008540 // For arrays generate assignment operation for single element and replace
8541 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008542 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008543 PseudoDstExpr, PseudoSrcExpr);
8544 if (AssignmentOp.isInvalid())
8545 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008546 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008547 /*DiscardedValue=*/true);
8548 if (AssignmentOp.isInvalid())
8549 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008550
Alexey Bataev74caaf22016-02-20 04:09:36 +00008551 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008552 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008553 if (TopDVar.CKind == OMPC_firstprivate)
8554 Ref = TopDVar.PrivateCopy;
8555 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008556 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008557 if (!IsOpenMPCapturedDecl(D))
8558 ExprCaptures.push_back(Ref->getDecl());
8559 }
8560 if (TopDVar.CKind == OMPC_firstprivate ||
8561 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008562 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008563 ExprResult RefRes = DefaultLvalueConversion(Ref);
8564 if (!RefRes.isUsable())
8565 continue;
8566 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008567 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8568 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008569 if (!PostUpdateRes.isUsable())
8570 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008571 ExprPostUpdates.push_back(
8572 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008573 }
8574 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008575 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008576 Vars.push_back((VD || CurContext->isDependentContext())
8577 ? RefExpr->IgnoreParens()
8578 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008579 SrcExprs.push_back(PseudoSrcExpr);
8580 DstExprs.push_back(PseudoDstExpr);
8581 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008582 }
8583
8584 if (Vars.empty())
8585 return nullptr;
8586
8587 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008588 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008589 buildPreInits(Context, ExprCaptures),
8590 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008591}
8592
Alexey Bataev758e55e2013-09-06 18:03:48 +00008593OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8594 SourceLocation StartLoc,
8595 SourceLocation LParenLoc,
8596 SourceLocation EndLoc) {
8597 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008598 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008599 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008600 SourceLocation ELoc;
8601 SourceRange ERange;
8602 Expr *SimpleRefExpr = RefExpr;
8603 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008604 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008605 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008606 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008607 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008608 ValueDecl *D = Res.first;
8609 if (!D)
8610 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008611
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008612 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008613 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8614 // in a Construct]
8615 // Variables with the predetermined data-sharing attributes may not be
8616 // listed in data-sharing attributes clauses, except for the cases
8617 // listed below. For these exceptions only, listing a predetermined
8618 // variable in a data-sharing attribute clause is allowed and overrides
8619 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008620 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008621 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8622 DVar.RefExpr) {
8623 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8624 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008625 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008626 continue;
8627 }
8628
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008629 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008630 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008631 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008632 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008633 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8634 ? RefExpr->IgnoreParens()
8635 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008636 }
8637
Alexey Bataeved09d242014-05-28 05:53:51 +00008638 if (Vars.empty())
8639 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008640
8641 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8642}
8643
Alexey Bataevc5e02582014-06-16 07:08:35 +00008644namespace {
8645class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8646 DSAStackTy *Stack;
8647
8648public:
8649 bool VisitDeclRefExpr(DeclRefExpr *E) {
8650 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008651 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008652 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8653 return false;
8654 if (DVar.CKind != OMPC_unknown)
8655 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008656 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8657 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8658 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008659 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008660 return true;
8661 return false;
8662 }
8663 return false;
8664 }
8665 bool VisitStmt(Stmt *S) {
8666 for (auto Child : S->children()) {
8667 if (Child && Visit(Child))
8668 return true;
8669 }
8670 return false;
8671 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008672 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008673};
Alexey Bataev23b69422014-06-18 07:08:49 +00008674} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008675
Alexey Bataev60da77e2016-02-29 05:54:20 +00008676namespace {
8677// Transform MemberExpression for specified FieldDecl of current class to
8678// DeclRefExpr to specified OMPCapturedExprDecl.
8679class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8680 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8681 ValueDecl *Field;
8682 DeclRefExpr *CapturedExpr;
8683
8684public:
8685 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8686 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8687
8688 ExprResult TransformMemberExpr(MemberExpr *E) {
8689 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8690 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008691 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008692 return CapturedExpr;
8693 }
8694 return BaseTransform::TransformMemberExpr(E);
8695 }
8696 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8697};
8698} // namespace
8699
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008700template <typename T>
8701static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8702 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8703 for (auto &Set : Lookups) {
8704 for (auto *D : Set) {
8705 if (auto Res = Gen(cast<ValueDecl>(D)))
8706 return Res;
8707 }
8708 }
8709 return T();
8710}
8711
8712static ExprResult
8713buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8714 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8715 const DeclarationNameInfo &ReductionId, QualType Ty,
8716 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8717 if (ReductionIdScopeSpec.isInvalid())
8718 return ExprError();
8719 SmallVector<UnresolvedSet<8>, 4> Lookups;
8720 if (S) {
8721 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8722 Lookup.suppressDiagnostics();
8723 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8724 auto *D = Lookup.getRepresentativeDecl();
8725 do {
8726 S = S->getParent();
8727 } while (S && !S->isDeclScope(D));
8728 if (S)
8729 S = S->getParent();
8730 Lookups.push_back(UnresolvedSet<8>());
8731 Lookups.back().append(Lookup.begin(), Lookup.end());
8732 Lookup.clear();
8733 }
8734 } else if (auto *ULE =
8735 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8736 Lookups.push_back(UnresolvedSet<8>());
8737 Decl *PrevD = nullptr;
8738 for(auto *D : ULE->decls()) {
8739 if (D == PrevD)
8740 Lookups.push_back(UnresolvedSet<8>());
8741 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8742 Lookups.back().addDecl(DRD);
8743 PrevD = D;
8744 }
8745 }
8746 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8747 Ty->containsUnexpandedParameterPack() ||
8748 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8749 return !D->isInvalidDecl() &&
8750 (D->getType()->isDependentType() ||
8751 D->getType()->isInstantiationDependentType() ||
8752 D->getType()->containsUnexpandedParameterPack());
8753 })) {
8754 UnresolvedSet<8> ResSet;
8755 for (auto &Set : Lookups) {
8756 ResSet.append(Set.begin(), Set.end());
8757 // The last item marks the end of all declarations at the specified scope.
8758 ResSet.addDecl(Set[Set.size() - 1]);
8759 }
8760 return UnresolvedLookupExpr::Create(
8761 SemaRef.Context, /*NamingClass=*/nullptr,
8762 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8763 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8764 }
8765 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8766 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8767 if (!D->isInvalidDecl() &&
8768 SemaRef.Context.hasSameType(D->getType(), Ty))
8769 return D;
8770 return nullptr;
8771 }))
8772 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8773 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8774 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8775 if (!D->isInvalidDecl() &&
8776 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8777 !Ty.isMoreQualifiedThan(D->getType()))
8778 return D;
8779 return nullptr;
8780 })) {
8781 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8782 /*DetectVirtual=*/false);
8783 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8784 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8785 VD->getType().getUnqualifiedType()))) {
8786 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8787 /*DiagID=*/0) !=
8788 Sema::AR_inaccessible) {
8789 SemaRef.BuildBasePathArray(Paths, BasePath);
8790 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8791 }
8792 }
8793 }
8794 }
8795 if (ReductionIdScopeSpec.isSet()) {
8796 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8797 return ExprError();
8798 }
8799 return ExprEmpty();
8800}
8801
Alexey Bataevc5e02582014-06-16 07:08:35 +00008802OMPClause *Sema::ActOnOpenMPReductionClause(
8803 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8804 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008805 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8806 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008807 auto DN = ReductionId.getName();
8808 auto OOK = DN.getCXXOverloadedOperator();
8809 BinaryOperatorKind BOK = BO_Comma;
8810
8811 // OpenMP [2.14.3.6, reduction clause]
8812 // C
8813 // reduction-identifier is either an identifier or one of the following
8814 // operators: +, -, *, &, |, ^, && and ||
8815 // C++
8816 // reduction-identifier is either an id-expression or one of the following
8817 // operators: +, -, *, &, |, ^, && and ||
8818 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8819 switch (OOK) {
8820 case OO_Plus:
8821 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008822 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008823 break;
8824 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008825 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008826 break;
8827 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008828 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008829 break;
8830 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008831 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008832 break;
8833 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008834 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008835 break;
8836 case OO_AmpAmp:
8837 BOK = BO_LAnd;
8838 break;
8839 case OO_PipePipe:
8840 BOK = BO_LOr;
8841 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008842 case OO_New:
8843 case OO_Delete:
8844 case OO_Array_New:
8845 case OO_Array_Delete:
8846 case OO_Slash:
8847 case OO_Percent:
8848 case OO_Tilde:
8849 case OO_Exclaim:
8850 case OO_Equal:
8851 case OO_Less:
8852 case OO_Greater:
8853 case OO_LessEqual:
8854 case OO_GreaterEqual:
8855 case OO_PlusEqual:
8856 case OO_MinusEqual:
8857 case OO_StarEqual:
8858 case OO_SlashEqual:
8859 case OO_PercentEqual:
8860 case OO_CaretEqual:
8861 case OO_AmpEqual:
8862 case OO_PipeEqual:
8863 case OO_LessLess:
8864 case OO_GreaterGreater:
8865 case OO_LessLessEqual:
8866 case OO_GreaterGreaterEqual:
8867 case OO_EqualEqual:
8868 case OO_ExclaimEqual:
8869 case OO_PlusPlus:
8870 case OO_MinusMinus:
8871 case OO_Comma:
8872 case OO_ArrowStar:
8873 case OO_Arrow:
8874 case OO_Call:
8875 case OO_Subscript:
8876 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008877 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008878 case NUM_OVERLOADED_OPERATORS:
8879 llvm_unreachable("Unexpected reduction identifier");
8880 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008881 if (auto II = DN.getAsIdentifierInfo()) {
8882 if (II->isStr("max"))
8883 BOK = BO_GT;
8884 else if (II->isStr("min"))
8885 BOK = BO_LT;
8886 }
8887 break;
8888 }
8889 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008890 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008891 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008892 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008893
8894 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008895 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008896 SmallVector<Expr *, 8> LHSs;
8897 SmallVector<Expr *, 8> RHSs;
8898 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008899 SmallVector<Decl *, 4> ExprCaptures;
8900 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008901 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8902 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008903 for (auto RefExpr : VarList) {
8904 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008905 // OpenMP [2.1, C/C++]
8906 // A list item is a variable or array section, subject to the restrictions
8907 // specified in Section 2.4 on page 42 and in each of the sections
8908 // describing clauses and directives for which a list appears.
8909 // OpenMP [2.14.3.3, Restrictions, p.1]
8910 // A variable that is part of another variable (as an array or
8911 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008912 if (!FirstIter && IR != ER)
8913 ++IR;
8914 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008915 SourceLocation ELoc;
8916 SourceRange ERange;
8917 Expr *SimpleRefExpr = RefExpr;
8918 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8919 /*AllowArraySection=*/true);
8920 if (Res.second) {
8921 // It will be analyzed later.
8922 Vars.push_back(RefExpr);
8923 Privates.push_back(nullptr);
8924 LHSs.push_back(nullptr);
8925 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008926 // Try to find 'declare reduction' corresponding construct before using
8927 // builtin/overloaded operators.
8928 QualType Type = Context.DependentTy;
8929 CXXCastPath BasePath;
8930 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8931 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8932 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8933 if (CurContext->isDependentContext() &&
8934 (DeclareReductionRef.isUnset() ||
8935 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8936 ReductionOps.push_back(DeclareReductionRef.get());
8937 else
8938 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008939 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008940 ValueDecl *D = Res.first;
8941 if (!D)
8942 continue;
8943
Alexey Bataeva1764212015-09-30 09:22:36 +00008944 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008945 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8946 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8947 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008948 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008949 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008950 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8951 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8952 Type = ATy->getElementType();
8953 else
8954 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008955 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008956 } else
8957 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8958 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008959
Alexey Bataevc5e02582014-06-16 07:08:35 +00008960 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8961 // A variable that appears in a private clause must not have an incomplete
8962 // type or a reference type.
8963 if (RequireCompleteType(ELoc, Type,
8964 diag::err_omp_reduction_incomplete_type))
8965 continue;
8966 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008967 // A list item that appears in a reduction clause must not be
8968 // const-qualified.
8969 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008970 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008971 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008972 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008973 bool IsDecl = !VD ||
8974 VD->isThisDeclarationADefinition(Context) ==
8975 VarDecl::DeclarationOnly;
8976 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008977 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008978 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008979 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008980 continue;
8981 }
8982 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8983 // If a list-item is a reference type then it must bind to the same object
8984 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008985 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008986 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008987 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008988 DSARefChecker Check(DSAStack);
8989 if (Check.Visit(VDDef->getInit())) {
8990 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8991 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8992 continue;
8993 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008994 }
8995 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008996
Alexey Bataevc5e02582014-06-16 07:08:35 +00008997 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8998 // in a Construct]
8999 // Variables with the predetermined data-sharing attributes may not be
9000 // listed in data-sharing attributes clauses, except for the cases
9001 // listed below. For these exceptions only, listing a predetermined
9002 // variable in a data-sharing attribute clause is allowed and overrides
9003 // the variable's predetermined data-sharing attributes.
9004 // OpenMP [2.14.3.6, Restrictions, p.3]
9005 // Any number of reduction clauses can be specified on the directive,
9006 // but a list item can appear only once in the reduction clauses for that
9007 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009008 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009009 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009010 if (DVar.CKind == OMPC_reduction) {
9011 Diag(ELoc, diag::err_omp_once_referenced)
9012 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009013 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009014 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009015 } else if (DVar.CKind != OMPC_unknown) {
9016 Diag(ELoc, diag::err_omp_wrong_dsa)
9017 << getOpenMPClauseName(DVar.CKind)
9018 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009019 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009020 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009021 }
9022
9023 // OpenMP [2.14.3.6, Restrictions, p.1]
9024 // A list item that appears in a reduction clause of a worksharing
9025 // construct must be shared in the parallel regions to which any of the
9026 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009027 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9028 if (isOpenMPWorksharingDirective(CurrDir) &&
9029 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009030 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009031 if (DVar.CKind != OMPC_shared) {
9032 Diag(ELoc, diag::err_omp_required_access)
9033 << getOpenMPClauseName(OMPC_reduction)
9034 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009035 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009036 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009037 }
9038 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009039
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009040 // Try to find 'declare reduction' corresponding construct before using
9041 // builtin/overloaded operators.
9042 CXXCastPath BasePath;
9043 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9044 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9045 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9046 if (DeclareReductionRef.isInvalid())
9047 continue;
9048 if (CurContext->isDependentContext() &&
9049 (DeclareReductionRef.isUnset() ||
9050 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9051 Vars.push_back(RefExpr);
9052 Privates.push_back(nullptr);
9053 LHSs.push_back(nullptr);
9054 RHSs.push_back(nullptr);
9055 ReductionOps.push_back(DeclareReductionRef.get());
9056 continue;
9057 }
9058 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9059 // Not allowed reduction identifier is found.
9060 Diag(ReductionId.getLocStart(),
9061 diag::err_omp_unknown_reduction_identifier)
9062 << Type << ReductionIdRange;
9063 continue;
9064 }
9065
9066 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9067 // The type of a list item that appears in a reduction clause must be valid
9068 // for the reduction-identifier. For a max or min reduction in C, the type
9069 // of the list item must be an allowed arithmetic data type: char, int,
9070 // float, double, or _Bool, possibly modified with long, short, signed, or
9071 // unsigned. For a max or min reduction in C++, the type of the list item
9072 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9073 // double, or bool, possibly modified with long, short, signed, or unsigned.
9074 if (DeclareReductionRef.isUnset()) {
9075 if ((BOK == BO_GT || BOK == BO_LT) &&
9076 !(Type->isScalarType() ||
9077 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9078 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9079 << getLangOpts().CPlusPlus;
9080 if (!ASE && !OASE) {
9081 bool IsDecl = !VD ||
9082 VD->isThisDeclarationADefinition(Context) ==
9083 VarDecl::DeclarationOnly;
9084 Diag(D->getLocation(),
9085 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9086 << D;
9087 }
9088 continue;
9089 }
9090 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9091 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9092 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9093 if (!ASE && !OASE) {
9094 bool IsDecl = !VD ||
9095 VD->isThisDeclarationADefinition(Context) ==
9096 VarDecl::DeclarationOnly;
9097 Diag(D->getLocation(),
9098 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9099 << D;
9100 }
9101 continue;
9102 }
9103 }
9104
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009105 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009106 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009107 D->hasAttrs() ? &D->getAttrs() : nullptr);
9108 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9109 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009110 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009111 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009112 (!ASE &&
9113 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009114 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009115 // Create pseudo array type for private copy. The size for this array will
9116 // be generated during codegen.
9117 // For array subscripts or single variables Private Ty is the same as Type
9118 // (type of the variable or single array element).
9119 PrivateTy = Context.getVariableArrayType(
9120 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9121 Context.getSizeType(), VK_RValue),
9122 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009123 } else if (!ASE && !OASE &&
9124 Context.getAsArrayType(D->getType().getNonReferenceType()))
9125 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009126 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009127 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9128 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009129 // Add initializer for private variable.
9130 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009131 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9132 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9133 if (DeclareReductionRef.isUsable()) {
9134 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9135 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9136 if (DRD->getInitializer()) {
9137 Init = DRDRef;
9138 RHSVD->setInit(DRDRef);
9139 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009140 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009141 } else {
9142 switch (BOK) {
9143 case BO_Add:
9144 case BO_Xor:
9145 case BO_Or:
9146 case BO_LOr:
9147 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9148 if (Type->isScalarType() || Type->isAnyComplexType())
9149 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9150 break;
9151 case BO_Mul:
9152 case BO_LAnd:
9153 if (Type->isScalarType() || Type->isAnyComplexType()) {
9154 // '*' and '&&' reduction ops - initializer is '1'.
9155 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009156 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009157 break;
9158 case BO_And: {
9159 // '&' reduction op - initializer is '~0'.
9160 QualType OrigType = Type;
9161 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9162 Type = ComplexTy->getElementType();
9163 if (Type->isRealFloatingType()) {
9164 llvm::APFloat InitValue =
9165 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9166 /*isIEEE=*/true);
9167 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9168 Type, ELoc);
9169 } else if (Type->isScalarType()) {
9170 auto Size = Context.getTypeSize(Type);
9171 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9172 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9173 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9174 }
9175 if (Init && OrigType->isAnyComplexType()) {
9176 // Init = 0xFFFF + 0xFFFFi;
9177 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9178 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9179 }
9180 Type = OrigType;
9181 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009182 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009183 case BO_LT:
9184 case BO_GT: {
9185 // 'min' reduction op - initializer is 'Largest representable number in
9186 // the reduction list item type'.
9187 // 'max' reduction op - initializer is 'Least representable number in
9188 // the reduction list item type'.
9189 if (Type->isIntegerType() || Type->isPointerType()) {
9190 bool IsSigned = Type->hasSignedIntegerRepresentation();
9191 auto Size = Context.getTypeSize(Type);
9192 QualType IntTy =
9193 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9194 llvm::APInt InitValue =
9195 (BOK != BO_LT)
9196 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9197 : llvm::APInt::getMinValue(Size)
9198 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9199 : llvm::APInt::getMaxValue(Size);
9200 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9201 if (Type->isPointerType()) {
9202 // Cast to pointer type.
9203 auto CastExpr = BuildCStyleCastExpr(
9204 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9205 SourceLocation(), Init);
9206 if (CastExpr.isInvalid())
9207 continue;
9208 Init = CastExpr.get();
9209 }
9210 } else if (Type->isRealFloatingType()) {
9211 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9212 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9213 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9214 Type, ELoc);
9215 }
9216 break;
9217 }
9218 case BO_PtrMemD:
9219 case BO_PtrMemI:
9220 case BO_MulAssign:
9221 case BO_Div:
9222 case BO_Rem:
9223 case BO_Sub:
9224 case BO_Shl:
9225 case BO_Shr:
9226 case BO_LE:
9227 case BO_GE:
9228 case BO_EQ:
9229 case BO_NE:
9230 case BO_AndAssign:
9231 case BO_XorAssign:
9232 case BO_OrAssign:
9233 case BO_Assign:
9234 case BO_AddAssign:
9235 case BO_SubAssign:
9236 case BO_DivAssign:
9237 case BO_RemAssign:
9238 case BO_ShlAssign:
9239 case BO_ShrAssign:
9240 case BO_Comma:
9241 llvm_unreachable("Unexpected reduction operation");
9242 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009243 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009244 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009245 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9246 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009247 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009248 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009249 if (RHSVD->isInvalidDecl())
9250 continue;
9251 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009252 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9253 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009254 bool IsDecl =
9255 !VD ||
9256 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9257 Diag(D->getLocation(),
9258 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9259 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009260 continue;
9261 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009262 // Store initializer for single element in private copy. Will be used during
9263 // codegen.
9264 PrivateVD->setInit(RHSVD->getInit());
9265 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009266 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009267 ExprResult ReductionOp;
9268 if (DeclareReductionRef.isUsable()) {
9269 QualType RedTy = DeclareReductionRef.get()->getType();
9270 QualType PtrRedTy = Context.getPointerType(RedTy);
9271 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9272 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9273 if (!BasePath.empty()) {
9274 LHS = DefaultLvalueConversion(LHS.get());
9275 RHS = DefaultLvalueConversion(RHS.get());
9276 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9277 CK_UncheckedDerivedToBase, LHS.get(),
9278 &BasePath, LHS.get()->getValueKind());
9279 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9280 CK_UncheckedDerivedToBase, RHS.get(),
9281 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009282 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009283 FunctionProtoType::ExtProtoInfo EPI;
9284 QualType Params[] = {PtrRedTy, PtrRedTy};
9285 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9286 auto *OVE = new (Context) OpaqueValueExpr(
9287 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9288 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9289 Expr *Args[] = {LHS.get(), RHS.get()};
9290 ReductionOp = new (Context)
9291 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9292 } else {
9293 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9294 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9295 if (ReductionOp.isUsable()) {
9296 if (BOK != BO_LT && BOK != BO_GT) {
9297 ReductionOp =
9298 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9299 BO_Assign, LHSDRE, ReductionOp.get());
9300 } else {
9301 auto *ConditionalOp = new (Context) ConditionalOperator(
9302 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9303 RHSDRE, Type, VK_LValue, OK_Ordinary);
9304 ReductionOp =
9305 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9306 BO_Assign, LHSDRE, ConditionalOp);
9307 }
9308 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9309 }
9310 if (ReductionOp.isInvalid())
9311 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009312 }
9313
Alexey Bataev60da77e2016-02-29 05:54:20 +00009314 DeclRefExpr *Ref = nullptr;
9315 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009316 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009317 if (ASE || OASE) {
9318 TransformExprToCaptures RebuildToCapture(*this, D);
9319 VarsExpr =
9320 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9321 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009322 } else {
9323 VarsExpr = Ref =
9324 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009325 }
9326 if (!IsOpenMPCapturedDecl(D)) {
9327 ExprCaptures.push_back(Ref->getDecl());
9328 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9329 ExprResult RefRes = DefaultLvalueConversion(Ref);
9330 if (!RefRes.isUsable())
9331 continue;
9332 ExprResult PostUpdateRes =
9333 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9334 SimpleRefExpr, RefRes.get());
9335 if (!PostUpdateRes.isUsable())
9336 continue;
9337 ExprPostUpdates.push_back(
9338 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009339 }
9340 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009341 }
9342 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9343 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009344 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009345 LHSs.push_back(LHSDRE);
9346 RHSs.push_back(RHSDRE);
9347 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009348 }
9349
9350 if (Vars.empty())
9351 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009352
Alexey Bataevc5e02582014-06-16 07:08:35 +00009353 return OMPReductionClause::Create(
9354 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009355 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009356 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9357 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009358}
9359
Alexey Bataevecba70f2016-04-12 11:02:11 +00009360bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9361 SourceLocation LinLoc) {
9362 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9363 LinKind == OMPC_LINEAR_unknown) {
9364 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9365 return true;
9366 }
9367 return false;
9368}
9369
9370bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9371 OpenMPLinearClauseKind LinKind,
9372 QualType Type) {
9373 auto *VD = dyn_cast_or_null<VarDecl>(D);
9374 // A variable must not have an incomplete type or a reference type.
9375 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9376 return true;
9377 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9378 !Type->isReferenceType()) {
9379 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9380 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9381 return true;
9382 }
9383 Type = Type.getNonReferenceType();
9384
9385 // A list item must not be const-qualified.
9386 if (Type.isConstant(Context)) {
9387 Diag(ELoc, diag::err_omp_const_variable)
9388 << getOpenMPClauseName(OMPC_linear);
9389 if (D) {
9390 bool IsDecl =
9391 !VD ||
9392 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9393 Diag(D->getLocation(),
9394 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9395 << D;
9396 }
9397 return true;
9398 }
9399
9400 // A list item must be of integral or pointer type.
9401 Type = Type.getUnqualifiedType().getCanonicalType();
9402 const auto *Ty = Type.getTypePtrOrNull();
9403 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9404 !Ty->isPointerType())) {
9405 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9406 if (D) {
9407 bool IsDecl =
9408 !VD ||
9409 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9410 Diag(D->getLocation(),
9411 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9412 << D;
9413 }
9414 return true;
9415 }
9416 return false;
9417}
9418
Alexey Bataev182227b2015-08-20 10:54:39 +00009419OMPClause *Sema::ActOnOpenMPLinearClause(
9420 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9421 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9422 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009423 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009424 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009425 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009426 SmallVector<Decl *, 4> ExprCaptures;
9427 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009428 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009429 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009430 for (auto &RefExpr : VarList) {
9431 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009432 SourceLocation ELoc;
9433 SourceRange ERange;
9434 Expr *SimpleRefExpr = RefExpr;
9435 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9436 /*AllowArraySection=*/false);
9437 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009438 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009439 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009440 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009441 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009442 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009443 ValueDecl *D = Res.first;
9444 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009445 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009446
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009447 QualType Type = D->getType();
9448 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009449
9450 // OpenMP [2.14.3.7, linear clause]
9451 // A list-item cannot appear in more than one linear clause.
9452 // A list-item that appears in a linear clause cannot appear in any
9453 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009454 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009455 if (DVar.RefExpr) {
9456 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9457 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009458 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009459 continue;
9460 }
9461
Alexey Bataevecba70f2016-04-12 11:02:11 +00009462 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009463 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009464 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009465
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009466 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009467 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9468 D->hasAttrs() ? &D->getAttrs() : nullptr);
9469 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009470 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009471 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009472 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009473 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009474 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009475 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9476 if (!IsOpenMPCapturedDecl(D)) {
9477 ExprCaptures.push_back(Ref->getDecl());
9478 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9479 ExprResult RefRes = DefaultLvalueConversion(Ref);
9480 if (!RefRes.isUsable())
9481 continue;
9482 ExprResult PostUpdateRes =
9483 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9484 SimpleRefExpr, RefRes.get());
9485 if (!PostUpdateRes.isUsable())
9486 continue;
9487 ExprPostUpdates.push_back(
9488 IgnoredValueConversions(PostUpdateRes.get()).get());
9489 }
9490 }
9491 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009492 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009493 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009494 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009495 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009496 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009497 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9498 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9499
9500 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009501 Vars.push_back((VD || CurContext->isDependentContext())
9502 ? RefExpr->IgnoreParens()
9503 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009504 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009505 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009506 }
9507
9508 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009509 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009510
9511 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009512 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009513 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9514 !Step->isInstantiationDependent() &&
9515 !Step->containsUnexpandedParameterPack()) {
9516 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009517 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009518 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009519 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009520 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009521
Alexander Musman3276a272015-03-21 10:12:56 +00009522 // Build var to save the step value.
9523 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009524 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009525 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009526 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009527 ExprResult CalcStep =
9528 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009529 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009530
Alexander Musman8dba6642014-04-22 13:09:42 +00009531 // Warn about zero linear step (it would be probably better specified as
9532 // making corresponding variables 'const').
9533 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009534 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9535 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009536 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9537 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009538 if (!IsConstant && CalcStep.isUsable()) {
9539 // Calculate the step beforehand instead of doing this on each iteration.
9540 // (This is not used if the number of iterations may be kfold-ed).
9541 CalcStepExpr = CalcStep.get();
9542 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009543 }
9544
Alexey Bataev182227b2015-08-20 10:54:39 +00009545 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9546 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009547 StepExpr, CalcStepExpr,
9548 buildPreInits(Context, ExprCaptures),
9549 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009550}
9551
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009552static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9553 Expr *NumIterations, Sema &SemaRef,
9554 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009555 // Walk the vars and build update/final expressions for the CodeGen.
9556 SmallVector<Expr *, 8> Updates;
9557 SmallVector<Expr *, 8> Finals;
9558 Expr *Step = Clause.getStep();
9559 Expr *CalcStep = Clause.getCalcStep();
9560 // OpenMP [2.14.3.7, linear clause]
9561 // If linear-step is not specified it is assumed to be 1.
9562 if (Step == nullptr)
9563 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009564 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009565 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009566 }
Alexander Musman3276a272015-03-21 10:12:56 +00009567 bool HasErrors = false;
9568 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009569 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009570 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009571 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009572 SourceLocation ELoc;
9573 SourceRange ERange;
9574 Expr *SimpleRefExpr = RefExpr;
9575 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9576 /*AllowArraySection=*/false);
9577 ValueDecl *D = Res.first;
9578 if (Res.second || !D) {
9579 Updates.push_back(nullptr);
9580 Finals.push_back(nullptr);
9581 HasErrors = true;
9582 continue;
9583 }
9584 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9585 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9586 ->getMemberDecl();
9587 }
9588 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009589 Expr *InitExpr = *CurInit;
9590
9591 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009592 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009593 Expr *CapturedRef;
9594 if (LinKind == OMPC_LINEAR_uval)
9595 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9596 else
9597 CapturedRef =
9598 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9599 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9600 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009601
9602 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009603 ExprResult Update;
9604 if (!Info.first) {
9605 Update =
9606 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9607 InitExpr, IV, Step, /* Subtract */ false);
9608 } else
9609 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009610 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9611 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009612
9613 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009614 ExprResult Final;
9615 if (!Info.first) {
9616 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9617 InitExpr, NumIterations, Step,
9618 /* Subtract */ false);
9619 } else
9620 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009621 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9622 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009623
Alexander Musman3276a272015-03-21 10:12:56 +00009624 if (!Update.isUsable() || !Final.isUsable()) {
9625 Updates.push_back(nullptr);
9626 Finals.push_back(nullptr);
9627 HasErrors = true;
9628 } else {
9629 Updates.push_back(Update.get());
9630 Finals.push_back(Final.get());
9631 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009632 ++CurInit;
9633 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009634 }
9635 Clause.setUpdates(Updates);
9636 Clause.setFinals(Finals);
9637 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009638}
9639
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009640OMPClause *Sema::ActOnOpenMPAlignedClause(
9641 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9642 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9643
9644 SmallVector<Expr *, 8> Vars;
9645 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009646 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9647 SourceLocation ELoc;
9648 SourceRange ERange;
9649 Expr *SimpleRefExpr = RefExpr;
9650 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9651 /*AllowArraySection=*/false);
9652 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009653 // It will be analyzed later.
9654 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009655 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009656 ValueDecl *D = Res.first;
9657 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009658 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009659
Alexey Bataev1efd1662016-03-29 10:59:56 +00009660 QualType QType = D->getType();
9661 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009662
9663 // OpenMP [2.8.1, simd construct, Restrictions]
9664 // The type of list items appearing in the aligned clause must be
9665 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009666 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009667 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009668 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009669 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009670 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009671 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009672 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009673 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009674 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009675 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009676 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009677 continue;
9678 }
9679
9680 // OpenMP [2.8.1, simd construct, Restrictions]
9681 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009682 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009683 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009684 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9685 << getOpenMPClauseName(OMPC_aligned);
9686 continue;
9687 }
9688
Alexey Bataev1efd1662016-03-29 10:59:56 +00009689 DeclRefExpr *Ref = nullptr;
9690 if (!VD && IsOpenMPCapturedDecl(D))
9691 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9692 Vars.push_back(DefaultFunctionArrayConversion(
9693 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9694 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009695 }
9696
9697 // OpenMP [2.8.1, simd construct, Description]
9698 // The parameter of the aligned clause, alignment, must be a constant
9699 // positive integer expression.
9700 // If no optional parameter is specified, implementation-defined default
9701 // alignments for SIMD instructions on the target platforms are assumed.
9702 if (Alignment != nullptr) {
9703 ExprResult AlignResult =
9704 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9705 if (AlignResult.isInvalid())
9706 return nullptr;
9707 Alignment = AlignResult.get();
9708 }
9709 if (Vars.empty())
9710 return nullptr;
9711
9712 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9713 EndLoc, Vars, Alignment);
9714}
9715
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009716OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9717 SourceLocation StartLoc,
9718 SourceLocation LParenLoc,
9719 SourceLocation EndLoc) {
9720 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009721 SmallVector<Expr *, 8> SrcExprs;
9722 SmallVector<Expr *, 8> DstExprs;
9723 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009724 for (auto &RefExpr : VarList) {
9725 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9726 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009727 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009728 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009729 SrcExprs.push_back(nullptr);
9730 DstExprs.push_back(nullptr);
9731 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009732 continue;
9733 }
9734
Alexey Bataeved09d242014-05-28 05:53:51 +00009735 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009736 // OpenMP [2.1, C/C++]
9737 // A list item is a variable name.
9738 // OpenMP [2.14.4.1, Restrictions, p.1]
9739 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009740 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009741 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009742 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9743 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009744 continue;
9745 }
9746
9747 Decl *D = DE->getDecl();
9748 VarDecl *VD = cast<VarDecl>(D);
9749
9750 QualType Type = VD->getType();
9751 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9752 // It will be analyzed later.
9753 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009754 SrcExprs.push_back(nullptr);
9755 DstExprs.push_back(nullptr);
9756 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009757 continue;
9758 }
9759
9760 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9761 // A list item that appears in a copyin clause must be threadprivate.
9762 if (!DSAStack->isThreadPrivate(VD)) {
9763 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009764 << getOpenMPClauseName(OMPC_copyin)
9765 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009766 continue;
9767 }
9768
9769 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9770 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009771 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009772 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009773 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009774 auto *SrcVD =
9775 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9776 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009777 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009778 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9779 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009780 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9781 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009782 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009783 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009784 // For arrays generate assignment operation for single element and replace
9785 // it by the original array element in CodeGen.
9786 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9787 PseudoDstExpr, PseudoSrcExpr);
9788 if (AssignmentOp.isInvalid())
9789 continue;
9790 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9791 /*DiscardedValue=*/true);
9792 if (AssignmentOp.isInvalid())
9793 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009794
9795 DSAStack->addDSA(VD, DE, OMPC_copyin);
9796 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009797 SrcExprs.push_back(PseudoSrcExpr);
9798 DstExprs.push_back(PseudoDstExpr);
9799 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009800 }
9801
Alexey Bataeved09d242014-05-28 05:53:51 +00009802 if (Vars.empty())
9803 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009804
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009805 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9806 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009807}
9808
Alexey Bataevbae9a792014-06-27 10:37:06 +00009809OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9810 SourceLocation StartLoc,
9811 SourceLocation LParenLoc,
9812 SourceLocation EndLoc) {
9813 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009814 SmallVector<Expr *, 8> SrcExprs;
9815 SmallVector<Expr *, 8> DstExprs;
9816 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009817 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009818 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9819 SourceLocation ELoc;
9820 SourceRange ERange;
9821 Expr *SimpleRefExpr = RefExpr;
9822 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9823 /*AllowArraySection=*/false);
9824 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009825 // It will be analyzed later.
9826 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009827 SrcExprs.push_back(nullptr);
9828 DstExprs.push_back(nullptr);
9829 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009830 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009831 ValueDecl *D = Res.first;
9832 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009833 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009834
Alexey Bataeve122da12016-03-17 10:50:17 +00009835 QualType Type = D->getType();
9836 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009837
9838 // OpenMP [2.14.4.2, Restrictions, p.2]
9839 // A list item that appears in a copyprivate clause may not appear in a
9840 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009841 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9842 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009843 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9844 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009845 Diag(ELoc, diag::err_omp_wrong_dsa)
9846 << getOpenMPClauseName(DVar.CKind)
9847 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009848 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009849 continue;
9850 }
9851
9852 // OpenMP [2.11.4.2, Restrictions, p.1]
9853 // All list items that appear in a copyprivate clause must be either
9854 // threadprivate or private in the enclosing context.
9855 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009856 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009857 if (DVar.CKind == OMPC_shared) {
9858 Diag(ELoc, diag::err_omp_required_access)
9859 << getOpenMPClauseName(OMPC_copyprivate)
9860 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009861 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009862 continue;
9863 }
9864 }
9865 }
9866
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009867 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009868 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009869 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009870 << getOpenMPClauseName(OMPC_copyprivate) << Type
9871 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009872 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009873 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009874 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009875 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009876 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009877 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009878 continue;
9879 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009880
Alexey Bataevbae9a792014-06-27 10:37:06 +00009881 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9882 // A variable of class type (or array thereof) that appears in a
9883 // copyin clause requires an accessible, unambiguous copy assignment
9884 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009885 Type = Context.getBaseElementType(Type.getNonReferenceType())
9886 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009887 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009888 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9889 D->hasAttrs() ? &D->getAttrs() : nullptr);
9890 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009891 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009892 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9893 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009894 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009895 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9896 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009897 PseudoDstExpr, PseudoSrcExpr);
9898 if (AssignmentOp.isInvalid())
9899 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009900 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009901 /*DiscardedValue=*/true);
9902 if (AssignmentOp.isInvalid())
9903 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009904
9905 // No need to mark vars as copyprivate, they are already threadprivate or
9906 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009907 assert(VD || IsOpenMPCapturedDecl(D));
9908 Vars.push_back(
9909 VD ? RefExpr->IgnoreParens()
9910 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009911 SrcExprs.push_back(PseudoSrcExpr);
9912 DstExprs.push_back(PseudoDstExpr);
9913 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009914 }
9915
9916 if (Vars.empty())
9917 return nullptr;
9918
Alexey Bataeva63048e2015-03-23 06:18:07 +00009919 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9920 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009921}
9922
Alexey Bataev6125da92014-07-21 11:26:11 +00009923OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9924 SourceLocation StartLoc,
9925 SourceLocation LParenLoc,
9926 SourceLocation EndLoc) {
9927 if (VarList.empty())
9928 return nullptr;
9929
9930 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9931}
Alexey Bataevdea47612014-07-23 07:46:59 +00009932
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009933OMPClause *
9934Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9935 SourceLocation DepLoc, SourceLocation ColonLoc,
9936 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9937 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009938 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009939 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009940 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009941 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009942 return nullptr;
9943 }
9944 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009945 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9946 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009947 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009948 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009949 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9950 /*Last=*/OMPC_DEPEND_unknown, Except)
9951 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009952 return nullptr;
9953 }
9954 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +00009955 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009956 llvm::APSInt DepCounter(/*BitWidth=*/32);
9957 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9958 if (DepKind == OMPC_DEPEND_sink) {
9959 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9960 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9961 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009962 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009963 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009964 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9965 DSAStack->getParentOrderedRegionParam()) {
9966 for (auto &RefExpr : VarList) {
9967 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +00009968 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009969 // It will be analyzed later.
9970 Vars.push_back(RefExpr);
9971 continue;
9972 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009973
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009974 SourceLocation ELoc = RefExpr->getExprLoc();
9975 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9976 if (DepKind == OMPC_DEPEND_sink) {
9977 if (DepCounter >= TotalDepCount) {
9978 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9979 continue;
9980 }
9981 ++DepCounter;
9982 // OpenMP [2.13.9, Summary]
9983 // depend(dependence-type : vec), where dependence-type is:
9984 // 'sink' and where vec is the iteration vector, which has the form:
9985 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9986 // where n is the value specified by the ordered clause in the loop
9987 // directive, xi denotes the loop iteration variable of the i-th nested
9988 // loop associated with the loop directive, and di is a constant
9989 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +00009990 if (CurContext->isDependentContext()) {
9991 // It will be analyzed later.
9992 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009993 continue;
9994 }
Alexey Bataev8b427062016-05-25 12:36:08 +00009995 SimpleExpr = SimpleExpr->IgnoreImplicit();
9996 OverloadedOperatorKind OOK = OO_None;
9997 SourceLocation OOLoc;
9998 Expr *LHS = SimpleExpr;
9999 Expr *RHS = nullptr;
10000 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10001 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10002 OOLoc = BO->getOperatorLoc();
10003 LHS = BO->getLHS()->IgnoreParenImpCasts();
10004 RHS = BO->getRHS()->IgnoreParenImpCasts();
10005 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10006 OOK = OCE->getOperator();
10007 OOLoc = OCE->getOperatorLoc();
10008 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10009 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10010 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10011 OOK = MCE->getMethodDecl()
10012 ->getNameInfo()
10013 .getName()
10014 .getCXXOverloadedOperator();
10015 OOLoc = MCE->getCallee()->getExprLoc();
10016 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10017 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10018 }
10019 SourceLocation ELoc;
10020 SourceRange ERange;
10021 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10022 /*AllowArraySection=*/false);
10023 if (Res.second) {
10024 // It will be analyzed later.
10025 Vars.push_back(RefExpr);
10026 }
10027 ValueDecl *D = Res.first;
10028 if (!D)
10029 continue;
10030
10031 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10032 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10033 continue;
10034 }
10035 if (RHS) {
10036 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10037 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10038 if (RHSRes.isInvalid())
10039 continue;
10040 }
10041 if (!CurContext->isDependentContext() &&
10042 DSAStack->getParentOrderedRegionParam() &&
10043 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10044 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10045 << DSAStack->getParentLoopControlVariable(
10046 DepCounter.getZExtValue());
10047 continue;
10048 }
10049 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010050 } else {
10051 // OpenMP [2.11.1.1, Restrictions, p.3]
10052 // A variable that is part of another variable (such as a field of a
10053 // structure) but is not an array element or an array section cannot
10054 // appear in a depend clause.
10055 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10056 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10057 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10058 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10059 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010060 (ASE &&
10061 !ASE->getBase()
10062 ->getType()
10063 .getNonReferenceType()
10064 ->isPointerType() &&
10065 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010066 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10067 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010068 continue;
10069 }
10070 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010071 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10072 }
10073
10074 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10075 TotalDepCount > VarList.size() &&
10076 DSAStack->getParentOrderedRegionParam()) {
10077 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10078 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10079 }
10080 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10081 Vars.empty())
10082 return nullptr;
10083 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010084 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10085 DepKind, DepLoc, ColonLoc, Vars);
10086 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10087 DSAStack->addDoacrossDependClause(C, OpsOffs);
10088 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010089}
Michael Wonge710d542015-08-07 16:16:36 +000010090
10091OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10092 SourceLocation LParenLoc,
10093 SourceLocation EndLoc) {
10094 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010095
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010096 // OpenMP [2.9.1, Restrictions]
10097 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010098 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10099 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010100 return nullptr;
10101
Michael Wonge710d542015-08-07 16:16:36 +000010102 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10103}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010104
10105static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10106 DSAStackTy *Stack, CXXRecordDecl *RD) {
10107 if (!RD || RD->isInvalidDecl())
10108 return true;
10109
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010110 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10111 if (auto *CTD = CTSD->getSpecializedTemplate())
10112 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010113 auto QTy = SemaRef.Context.getRecordType(RD);
10114 if (RD->isDynamicClass()) {
10115 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10116 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10117 return false;
10118 }
10119 auto *DC = RD;
10120 bool IsCorrect = true;
10121 for (auto *I : DC->decls()) {
10122 if (I) {
10123 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10124 if (MD->isStatic()) {
10125 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10126 SemaRef.Diag(MD->getLocation(),
10127 diag::note_omp_static_member_in_target);
10128 IsCorrect = false;
10129 }
10130 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10131 if (VD->isStaticDataMember()) {
10132 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10133 SemaRef.Diag(VD->getLocation(),
10134 diag::note_omp_static_member_in_target);
10135 IsCorrect = false;
10136 }
10137 }
10138 }
10139 }
10140
10141 for (auto &I : RD->bases()) {
10142 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10143 I.getType()->getAsCXXRecordDecl()))
10144 IsCorrect = false;
10145 }
10146 return IsCorrect;
10147}
10148
10149static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10150 DSAStackTy *Stack, QualType QTy) {
10151 NamedDecl *ND;
10152 if (QTy->isIncompleteType(&ND)) {
10153 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10154 return false;
10155 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10156 if (!RD->isInvalidDecl() &&
10157 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10158 return false;
10159 }
10160 return true;
10161}
10162
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010163/// \brief Return true if it can be proven that the provided array expression
10164/// (array section or array subscript) does NOT specify the whole size of the
10165/// array whose base type is \a BaseQTy.
10166static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10167 const Expr *E,
10168 QualType BaseQTy) {
10169 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10170
10171 // If this is an array subscript, it refers to the whole size if the size of
10172 // the dimension is constant and equals 1. Also, an array section assumes the
10173 // format of an array subscript if no colon is used.
10174 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10175 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10176 return ATy->getSize().getSExtValue() != 1;
10177 // Size can't be evaluated statically.
10178 return false;
10179 }
10180
10181 assert(OASE && "Expecting array section if not an array subscript.");
10182 auto *LowerBound = OASE->getLowerBound();
10183 auto *Length = OASE->getLength();
10184
10185 // If there is a lower bound that does not evaluates to zero, we are not
10186 // convering the whole dimension.
10187 if (LowerBound) {
10188 llvm::APSInt ConstLowerBound;
10189 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10190 return false; // Can't get the integer value as a constant.
10191 if (ConstLowerBound.getSExtValue())
10192 return true;
10193 }
10194
10195 // If we don't have a length we covering the whole dimension.
10196 if (!Length)
10197 return false;
10198
10199 // If the base is a pointer, we don't have a way to get the size of the
10200 // pointee.
10201 if (BaseQTy->isPointerType())
10202 return false;
10203
10204 // We can only check if the length is the same as the size of the dimension
10205 // if we have a constant array.
10206 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10207 if (!CATy)
10208 return false;
10209
10210 llvm::APSInt ConstLength;
10211 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10212 return false; // Can't get the integer value as a constant.
10213
10214 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10215}
10216
10217// Return true if it can be proven that the provided array expression (array
10218// section or array subscript) does NOT specify a single element of the array
10219// whose base type is \a BaseQTy.
10220static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10221 const Expr *E,
10222 QualType BaseQTy) {
10223 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10224
10225 // An array subscript always refer to a single element. Also, an array section
10226 // assumes the format of an array subscript if no colon is used.
10227 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10228 return false;
10229
10230 assert(OASE && "Expecting array section if not an array subscript.");
10231 auto *Length = OASE->getLength();
10232
10233 // If we don't have a length we have to check if the array has unitary size
10234 // for this dimension. Also, we should always expect a length if the base type
10235 // is pointer.
10236 if (!Length) {
10237 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10238 return ATy->getSize().getSExtValue() != 1;
10239 // We cannot assume anything.
10240 return false;
10241 }
10242
10243 // Check if the length evaluates to 1.
10244 llvm::APSInt ConstLength;
10245 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10246 return false; // Can't get the integer value as a constant.
10247
10248 return ConstLength.getSExtValue() != 1;
10249}
10250
Samuel Antao661c0902016-05-26 17:39:58 +000010251// Return the expression of the base of the mappable expression or null if it
10252// cannot be determined and do all the necessary checks to see if the expression
10253// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010254// components of the expression.
10255static Expr *CheckMapClauseExpressionBase(
10256 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010257 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10258 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010259 SourceLocation ELoc = E->getExprLoc();
10260 SourceRange ERange = E->getSourceRange();
10261
10262 // The base of elements of list in a map clause have to be either:
10263 // - a reference to variable or field.
10264 // - a member expression.
10265 // - an array expression.
10266 //
10267 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10268 // reference to 'r'.
10269 //
10270 // If we have:
10271 //
10272 // struct SS {
10273 // Bla S;
10274 // foo() {
10275 // #pragma omp target map (S.Arr[:12]);
10276 // }
10277 // }
10278 //
10279 // We want to retrieve the member expression 'this->S';
10280
10281 Expr *RelevantExpr = nullptr;
10282
Samuel Antao5de996e2016-01-22 20:21:36 +000010283 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10284 // If a list item is an array section, it must specify contiguous storage.
10285 //
10286 // For this restriction it is sufficient that we make sure only references
10287 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010288 // exist except in the rightmost expression (unless they cover the whole
10289 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010290 //
10291 // r.ArrS[3:5].Arr[6:7]
10292 //
10293 // r.ArrS[3:5].x
10294 //
10295 // but these would be valid:
10296 // r.ArrS[3].Arr[6:7]
10297 //
10298 // r.ArrS[3].x
10299
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010300 bool AllowUnitySizeArraySection = true;
10301 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010302
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010303 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010304 E = E->IgnoreParenImpCasts();
10305
10306 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10307 if (!isa<VarDecl>(CurE->getDecl()))
10308 break;
10309
10310 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010311
10312 // If we got a reference to a declaration, we should not expect any array
10313 // section before that.
10314 AllowUnitySizeArraySection = false;
10315 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010316
10317 // Record the component.
10318 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10319 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010320 continue;
10321 }
10322
10323 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10324 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10325
10326 if (isa<CXXThisExpr>(BaseE))
10327 // We found a base expression: this->Val.
10328 RelevantExpr = CurE;
10329 else
10330 E = BaseE;
10331
10332 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10333 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10334 << CurE->getSourceRange();
10335 break;
10336 }
10337
10338 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10339
10340 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10341 // A bit-field cannot appear in a map clause.
10342 //
10343 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010344 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10345 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010346 break;
10347 }
10348
10349 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10350 // If the type of a list item is a reference to a type T then the type
10351 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010352 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010353
10354 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10355 // A list item cannot be a variable that is a member of a structure with
10356 // a union type.
10357 //
10358 if (auto *RT = CurType->getAs<RecordType>())
10359 if (RT->isUnionType()) {
10360 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10361 << CurE->getSourceRange();
10362 break;
10363 }
10364
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010365 // If we got a member expression, we should not expect any array section
10366 // before that:
10367 //
10368 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10369 // If a list item is an element of a structure, only the rightmost symbol
10370 // of the variable reference can be an array section.
10371 //
10372 AllowUnitySizeArraySection = false;
10373 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010374
10375 // Record the component.
10376 CurComponents.push_back(
10377 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010378 continue;
10379 }
10380
10381 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10382 E = CurE->getBase()->IgnoreParenImpCasts();
10383
10384 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10385 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10386 << 0 << CurE->getSourceRange();
10387 break;
10388 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010389
10390 // If we got an array subscript that express the whole dimension we
10391 // can have any array expressions before. If it only expressing part of
10392 // the dimension, we can only have unitary-size array expressions.
10393 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10394 E->getType()))
10395 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010396
10397 // Record the component - we don't have any declaration associated.
10398 CurComponents.push_back(
10399 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010400 continue;
10401 }
10402
10403 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010404 E = CurE->getBase()->IgnoreParenImpCasts();
10405
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010406 auto CurType =
10407 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10408
Samuel Antao5de996e2016-01-22 20:21:36 +000010409 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10410 // If the type of a list item is a reference to a type T then the type
10411 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010412 if (CurType->isReferenceType())
10413 CurType = CurType->getPointeeType();
10414
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010415 bool IsPointer = CurType->isAnyPointerType();
10416
10417 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010418 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10419 << 0 << CurE->getSourceRange();
10420 break;
10421 }
10422
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010423 bool NotWhole =
10424 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10425 bool NotUnity =
10426 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10427
10428 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
10429 // Any array section is currently allowed.
10430 //
10431 // If this array section refers to the whole dimension we can still
10432 // accept other array sections before this one, except if the base is a
10433 // pointer. Otherwise, only unitary sections are accepted.
10434 if (NotWhole || IsPointer)
10435 AllowWholeSizeArraySection = false;
10436 } else if ((AllowUnitySizeArraySection && NotUnity) ||
10437 (AllowWholeSizeArraySection && NotWhole)) {
10438 // A unity or whole array section is not allowed and that is not
10439 // compatible with the properties of the current array section.
10440 SemaRef.Diag(
10441 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10442 << CurE->getSourceRange();
10443 break;
10444 }
Samuel Antao90927002016-04-26 14:54:23 +000010445
10446 // Record the component - we don't have any declaration associated.
10447 CurComponents.push_back(
10448 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010449 continue;
10450 }
10451
10452 // If nothing else worked, this is not a valid map clause expression.
10453 SemaRef.Diag(ELoc,
10454 diag::err_omp_expected_named_var_member_or_array_expression)
10455 << ERange;
10456 break;
10457 }
10458
10459 return RelevantExpr;
10460}
10461
10462// Return true if expression E associated with value VD has conflicts with other
10463// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010464static bool CheckMapConflicts(
10465 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10466 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010467 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10468 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010469 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010470 SourceLocation ELoc = E->getExprLoc();
10471 SourceRange ERange = E->getSourceRange();
10472
10473 // In order to easily check the conflicts we need to match each component of
10474 // the expression under test with the components of the expressions that are
10475 // already in the stack.
10476
Samuel Antao5de996e2016-01-22 20:21:36 +000010477 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010478 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010479 "Map clause expression with unexpected base!");
10480
10481 // Variables to help detecting enclosing problems in data environment nests.
10482 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010483 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010484
Samuel Antao90927002016-04-26 14:54:23 +000010485 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10486 VD, CurrentRegionOnly,
10487 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10488 StackComponents) -> bool {
10489
Samuel Antao5de996e2016-01-22 20:21:36 +000010490 assert(!StackComponents.empty() &&
10491 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010492 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010493 "Map clause expression with unexpected base!");
10494
Samuel Antao90927002016-04-26 14:54:23 +000010495 // The whole expression in the stack.
10496 auto *RE = StackComponents.front().getAssociatedExpression();
10497
Samuel Antao5de996e2016-01-22 20:21:36 +000010498 // Expressions must start from the same base. Here we detect at which
10499 // point both expressions diverge from each other and see if we can
10500 // detect if the memory referred to both expressions is contiguous and
10501 // do not overlap.
10502 auto CI = CurComponents.rbegin();
10503 auto CE = CurComponents.rend();
10504 auto SI = StackComponents.rbegin();
10505 auto SE = StackComponents.rend();
10506 for (; CI != CE && SI != SE; ++CI, ++SI) {
10507
10508 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10509 // At most one list item can be an array item derived from a given
10510 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010511 if (CurrentRegionOnly &&
10512 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10513 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10514 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10515 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10516 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010517 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010518 << CI->getAssociatedExpression()->getSourceRange();
10519 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10520 diag::note_used_here)
10521 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010522 return true;
10523 }
10524
10525 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010526 if (CI->getAssociatedExpression()->getStmtClass() !=
10527 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010528 break;
10529
10530 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010531 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010532 break;
10533 }
10534
10535 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10536 // List items of map clauses in the same construct must not share
10537 // original storage.
10538 //
10539 // If the expressions are exactly the same or one is a subset of the
10540 // other, it means they are sharing storage.
10541 if (CI == CE && SI == SE) {
10542 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010543 if (CKind == OMPC_map)
10544 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10545 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010546 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010547 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10548 << ERange;
10549 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010550 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10551 << RE->getSourceRange();
10552 return true;
10553 } else {
10554 // If we find the same expression in the enclosing data environment,
10555 // that is legal.
10556 IsEnclosedByDataEnvironmentExpr = true;
10557 return false;
10558 }
10559 }
10560
Samuel Antao90927002016-04-26 14:54:23 +000010561 QualType DerivedType =
10562 std::prev(CI)->getAssociatedDeclaration()->getType();
10563 SourceLocation DerivedLoc =
10564 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010565
10566 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10567 // If the type of a list item is a reference to a type T then the type
10568 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010569 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010570
10571 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10572 // A variable for which the type is pointer and an array section
10573 // derived from that variable must not appear as list items of map
10574 // clauses of the same construct.
10575 //
10576 // Also, cover one of the cases in:
10577 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10578 // If any part of the original storage of a list item has corresponding
10579 // storage in the device data environment, all of the original storage
10580 // must have corresponding storage in the device data environment.
10581 //
10582 if (DerivedType->isAnyPointerType()) {
10583 if (CI == CE || SI == SE) {
10584 SemaRef.Diag(
10585 DerivedLoc,
10586 diag::err_omp_pointer_mapped_along_with_derived_section)
10587 << DerivedLoc;
10588 } else {
10589 assert(CI != CE && SI != SE);
10590 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10591 << DerivedLoc;
10592 }
10593 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10594 << RE->getSourceRange();
10595 return true;
10596 }
10597
10598 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10599 // List items of map clauses in the same construct must not share
10600 // original storage.
10601 //
10602 // An expression is a subset of the other.
10603 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010604 if (CKind == OMPC_map)
10605 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10606 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010607 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010608 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10609 << ERange;
10610 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010611 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10612 << RE->getSourceRange();
10613 return true;
10614 }
10615
10616 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010617 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010618 if (!CurrentRegionOnly && SI != SE)
10619 EnclosingExpr = RE;
10620
10621 // The current expression is a subset of the expression in the data
10622 // environment.
10623 IsEnclosedByDataEnvironmentExpr |=
10624 (!CurrentRegionOnly && CI != CE && SI == SE);
10625
10626 return false;
10627 });
10628
10629 if (CurrentRegionOnly)
10630 return FoundError;
10631
10632 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10633 // If any part of the original storage of a list item has corresponding
10634 // storage in the device data environment, all of the original storage must
10635 // have corresponding storage in the device data environment.
10636 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10637 // If a list item is an element of a structure, and a different element of
10638 // the structure has a corresponding list item in the device data environment
10639 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010640 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010641 // data environment prior to the task encountering the construct.
10642 //
10643 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10644 SemaRef.Diag(ELoc,
10645 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10646 << ERange;
10647 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10648 << EnclosingExpr->getSourceRange();
10649 return true;
10650 }
10651
10652 return FoundError;
10653}
10654
Samuel Antao661c0902016-05-26 17:39:58 +000010655namespace {
10656// Utility struct that gathers all the related lists associated with a mappable
10657// expression.
10658struct MappableVarListInfo final {
10659 // The list of expressions.
10660 ArrayRef<Expr *> VarList;
10661 // The list of processed expressions.
10662 SmallVector<Expr *, 16> ProcessedVarList;
10663 // The mappble components for each expression.
10664 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10665 // The base declaration of the variable.
10666 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10667
10668 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10669 // We have a list of components and base declarations for each entry in the
10670 // variable list.
10671 VarComponents.reserve(VarList.size());
10672 VarBaseDeclarations.reserve(VarList.size());
10673 }
10674};
10675}
10676
10677// Check the validity of the provided variable list for the provided clause kind
10678// \a CKind. In the check process the valid expressions, and mappable expression
10679// components and variables are extracted and used to fill \a Vars,
10680// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10681// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10682static void
10683checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10684 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10685 SourceLocation StartLoc,
10686 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10687 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010688 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10689 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010690 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010691
Samuel Antao90927002016-04-26 14:54:23 +000010692 // Keep track of the mappable components and base declarations in this clause.
10693 // Each entry in the list is going to have a list of components associated. We
10694 // record each set of the components so that we can build the clause later on.
10695 // In the end we should have the same amount of declarations and component
10696 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010697
Samuel Antao661c0902016-05-26 17:39:58 +000010698 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010699 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010700 SourceLocation ELoc = RE->getExprLoc();
10701
Kelvin Li0bff7af2015-11-23 05:32:03 +000010702 auto *VE = RE->IgnoreParenLValueCasts();
10703
10704 if (VE->isValueDependent() || VE->isTypeDependent() ||
10705 VE->isInstantiationDependent() ||
10706 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010707 // We can only analyze this information once the missing information is
10708 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000010709 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010710 continue;
10711 }
10712
10713 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010714
Samuel Antao5de996e2016-01-22 20:21:36 +000010715 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010716 SemaRef.Diag(ELoc,
10717 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000010718 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010719 continue;
10720 }
10721
Samuel Antao90927002016-04-26 14:54:23 +000010722 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10723 ValueDecl *CurDeclaration = nullptr;
10724
10725 // Obtain the array or member expression bases if required. Also, fill the
10726 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000010727 auto *BE =
10728 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010729 if (!BE)
10730 continue;
10731
Samuel Antao90927002016-04-26 14:54:23 +000010732 assert(!CurComponents.empty() &&
10733 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010734
Samuel Antao90927002016-04-26 14:54:23 +000010735 // For the following checks, we rely on the base declaration which is
10736 // expected to be associated with the last component. The declaration is
10737 // expected to be a variable or a field (if 'this' is being mapped).
10738 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10739 assert(CurDeclaration && "Null decl on map clause.");
10740 assert(
10741 CurDeclaration->isCanonicalDecl() &&
10742 "Expecting components to have associated only canonical declarations.");
10743
10744 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10745 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010746
10747 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010748 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010749
10750 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000010751 // threadprivate variables cannot appear in a map clause.
10752 // OpenMP 4.5 [2.10.5, target update Construct]
10753 // threadprivate variables cannot appear in a from clause.
10754 if (VD && DSAS->isThreadPrivate(VD)) {
10755 auto DVar = DSAS->getTopDSA(VD, false);
10756 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10757 << getOpenMPClauseName(CKind);
10758 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010759 continue;
10760 }
10761
Samuel Antao5de996e2016-01-22 20:21:36 +000010762 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10763 // A list item cannot appear in both a map clause and a data-sharing
10764 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010765
Samuel Antao5de996e2016-01-22 20:21:36 +000010766 // Check conflicts with other map clause expressions. We check the conflicts
10767 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000010768 // environment, because the restrictions are different. We only have to
10769 // check conflicts across regions for the map clauses.
10770 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10771 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010772 break;
Samuel Antao661c0902016-05-26 17:39:58 +000010773 if (CKind == OMPC_map &&
10774 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10775 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010776 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010777
Samuel Antao661c0902016-05-26 17:39:58 +000010778 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000010779 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10780 // If the type of a list item is a reference to a type T then the type will
10781 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010782 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010783
Samuel Antao661c0902016-05-26 17:39:58 +000010784 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10785 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000010786 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010787 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000010788 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10789 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010790 continue;
10791
Samuel Antao661c0902016-05-26 17:39:58 +000010792 if (CKind == OMPC_map) {
10793 // target enter data
10794 // OpenMP [2.10.2, Restrictions, p. 99]
10795 // A map-type must be specified in all map clauses and must be either
10796 // to or alloc.
10797 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10798 if (DKind == OMPD_target_enter_data &&
10799 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10800 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10801 << (IsMapTypeImplicit ? 1 : 0)
10802 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10803 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010804 continue;
10805 }
Samuel Antao661c0902016-05-26 17:39:58 +000010806
10807 // target exit_data
10808 // OpenMP [2.10.3, Restrictions, p. 102]
10809 // A map-type must be specified in all map clauses and must be either
10810 // from, release, or delete.
10811 if (DKind == OMPD_target_exit_data &&
10812 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10813 MapType == OMPC_MAP_delete)) {
10814 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10815 << (IsMapTypeImplicit ? 1 : 0)
10816 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10817 << getOpenMPDirectiveName(DKind);
10818 continue;
10819 }
10820
10821 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10822 // A list item cannot appear in both a map clause and a data-sharing
10823 // attribute clause on the same construct
10824 if (DKind == OMPD_target && VD) {
10825 auto DVar = DSAS->getTopDSA(VD, false);
10826 if (isOpenMPPrivate(DVar.CKind)) {
10827 SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10828 << getOpenMPClauseName(DVar.CKind)
10829 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
10830 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
10831 continue;
10832 }
10833 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010834 }
10835
Samuel Antao90927002016-04-26 14:54:23 +000010836 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000010837 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010838
10839 // Store the components in the stack so that they can be used to check
10840 // against other clauses later on.
Samuel Antao661c0902016-05-26 17:39:58 +000010841 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
Samuel Antao90927002016-04-26 14:54:23 +000010842
10843 // Save the components and declaration to create the clause. For purposes of
10844 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000010845 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000010846 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
10847 MVLI.VarComponents.back().append(CurComponents.begin(),
10848 CurComponents.end());
10849 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10850 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010851 }
Samuel Antao661c0902016-05-26 17:39:58 +000010852}
10853
10854OMPClause *
10855Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10856 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10857 SourceLocation MapLoc, SourceLocation ColonLoc,
10858 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10859 SourceLocation LParenLoc, SourceLocation EndLoc) {
10860 MappableVarListInfo MVLI(VarList);
10861 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
10862 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010863
Samuel Antao5de996e2016-01-22 20:21:36 +000010864 // We need to produce a map clause even if we don't have variables so that
10865 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000010866 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10867 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
10868 MVLI.VarComponents, MapTypeModifier, MapType,
10869 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010870}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010871
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010872QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10873 TypeResult ParsedType) {
10874 assert(ParsedType.isUsable());
10875
10876 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10877 if (ReductionType.isNull())
10878 return QualType();
10879
10880 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10881 // A type name in a declare reduction directive cannot be a function type, an
10882 // array type, a reference type, or a type qualified with const, volatile or
10883 // restrict.
10884 if (ReductionType.hasQualifiers()) {
10885 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10886 return QualType();
10887 }
10888
10889 if (ReductionType->isFunctionType()) {
10890 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10891 return QualType();
10892 }
10893 if (ReductionType->isReferenceType()) {
10894 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10895 return QualType();
10896 }
10897 if (ReductionType->isArrayType()) {
10898 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10899 return QualType();
10900 }
10901 return ReductionType;
10902}
10903
10904Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10905 Scope *S, DeclContext *DC, DeclarationName Name,
10906 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10907 AccessSpecifier AS, Decl *PrevDeclInScope) {
10908 SmallVector<Decl *, 8> Decls;
10909 Decls.reserve(ReductionTypes.size());
10910
10911 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10912 ForRedeclaration);
10913 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10914 // A reduction-identifier may not be re-declared in the current scope for the
10915 // same type or for a type that is compatible according to the base language
10916 // rules.
10917 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10918 OMPDeclareReductionDecl *PrevDRD = nullptr;
10919 bool InCompoundScope = true;
10920 if (S != nullptr) {
10921 // Find previous declaration with the same name not referenced in other
10922 // declarations.
10923 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10924 InCompoundScope =
10925 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10926 LookupName(Lookup, S);
10927 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10928 /*AllowInlineNamespace=*/false);
10929 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10930 auto Filter = Lookup.makeFilter();
10931 while (Filter.hasNext()) {
10932 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10933 if (InCompoundScope) {
10934 auto I = UsedAsPrevious.find(PrevDecl);
10935 if (I == UsedAsPrevious.end())
10936 UsedAsPrevious[PrevDecl] = false;
10937 if (auto *D = PrevDecl->getPrevDeclInScope())
10938 UsedAsPrevious[D] = true;
10939 }
10940 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10941 PrevDecl->getLocation();
10942 }
10943 Filter.done();
10944 if (InCompoundScope) {
10945 for (auto &PrevData : UsedAsPrevious) {
10946 if (!PrevData.second) {
10947 PrevDRD = PrevData.first;
10948 break;
10949 }
10950 }
10951 }
10952 } else if (PrevDeclInScope != nullptr) {
10953 auto *PrevDRDInScope = PrevDRD =
10954 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10955 do {
10956 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10957 PrevDRDInScope->getLocation();
10958 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10959 } while (PrevDRDInScope != nullptr);
10960 }
10961 for (auto &TyData : ReductionTypes) {
10962 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10963 bool Invalid = false;
10964 if (I != PreviousRedeclTypes.end()) {
10965 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10966 << TyData.first;
10967 Diag(I->second, diag::note_previous_definition);
10968 Invalid = true;
10969 }
10970 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10971 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10972 Name, TyData.first, PrevDRD);
10973 DC->addDecl(DRD);
10974 DRD->setAccess(AS);
10975 Decls.push_back(DRD);
10976 if (Invalid)
10977 DRD->setInvalidDecl();
10978 else
10979 PrevDRD = DRD;
10980 }
10981
10982 return DeclGroupPtrTy::make(
10983 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10984}
10985
10986void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10987 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10988
10989 // Enter new function scope.
10990 PushFunctionScope();
10991 getCurFunction()->setHasBranchProtectedScope();
10992 getCurFunction()->setHasOMPDeclareReductionCombiner();
10993
10994 if (S != nullptr)
10995 PushDeclContext(S, DRD);
10996 else
10997 CurContext = DRD;
10998
10999 PushExpressionEvaluationContext(PotentiallyEvaluated);
11000
11001 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011002 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11003 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11004 // uses semantics of argument handles by value, but it should be passed by
11005 // reference. C lang does not support references, so pass all parameters as
11006 // pointers.
11007 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011008 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011009 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011010 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11011 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11012 // uses semantics of argument handles by value, but it should be passed by
11013 // reference. C lang does not support references, so pass all parameters as
11014 // pointers.
11015 // Create 'T omp_out;' variable.
11016 auto *OmpOutParm =
11017 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11018 if (S != nullptr) {
11019 PushOnScopeChains(OmpInParm, S);
11020 PushOnScopeChains(OmpOutParm, S);
11021 } else {
11022 DRD->addDecl(OmpInParm);
11023 DRD->addDecl(OmpOutParm);
11024 }
11025}
11026
11027void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11028 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11029 DiscardCleanupsInEvaluationContext();
11030 PopExpressionEvaluationContext();
11031
11032 PopDeclContext();
11033 PopFunctionScopeInfo();
11034
11035 if (Combiner != nullptr)
11036 DRD->setCombiner(Combiner);
11037 else
11038 DRD->setInvalidDecl();
11039}
11040
11041void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11042 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11043
11044 // Enter new function scope.
11045 PushFunctionScope();
11046 getCurFunction()->setHasBranchProtectedScope();
11047
11048 if (S != nullptr)
11049 PushDeclContext(S, DRD);
11050 else
11051 CurContext = DRD;
11052
11053 PushExpressionEvaluationContext(PotentiallyEvaluated);
11054
11055 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011056 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11057 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11058 // uses semantics of argument handles by value, but it should be passed by
11059 // reference. C lang does not support references, so pass all parameters as
11060 // pointers.
11061 // Create 'T omp_priv;' variable.
11062 auto *OmpPrivParm =
11063 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011064 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11065 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11066 // uses semantics of argument handles by value, but it should be passed by
11067 // reference. C lang does not support references, so pass all parameters as
11068 // pointers.
11069 // Create 'T omp_orig;' variable.
11070 auto *OmpOrigParm =
11071 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011072 if (S != nullptr) {
11073 PushOnScopeChains(OmpPrivParm, S);
11074 PushOnScopeChains(OmpOrigParm, S);
11075 } else {
11076 DRD->addDecl(OmpPrivParm);
11077 DRD->addDecl(OmpOrigParm);
11078 }
11079}
11080
11081void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11082 Expr *Initializer) {
11083 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11084 DiscardCleanupsInEvaluationContext();
11085 PopExpressionEvaluationContext();
11086
11087 PopDeclContext();
11088 PopFunctionScopeInfo();
11089
11090 if (Initializer != nullptr)
11091 DRD->setInitializer(Initializer);
11092 else
11093 DRD->setInvalidDecl();
11094}
11095
11096Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11097 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11098 for (auto *D : DeclReductions.get()) {
11099 if (IsValid) {
11100 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11101 if (S != nullptr)
11102 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11103 } else
11104 D->setInvalidDecl();
11105 }
11106 return DeclReductions;
11107}
11108
Kelvin Li099bb8c2015-11-24 20:50:12 +000011109OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11110 SourceLocation StartLoc,
11111 SourceLocation LParenLoc,
11112 SourceLocation EndLoc) {
11113 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011114
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011115 // OpenMP [teams Constrcut, Restrictions]
11116 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011117 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11118 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011119 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011120
11121 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11122}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011123
11124OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11125 SourceLocation StartLoc,
11126 SourceLocation LParenLoc,
11127 SourceLocation EndLoc) {
11128 Expr *ValExpr = ThreadLimit;
11129
11130 // OpenMP [teams Constrcut, Restrictions]
11131 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011132 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11133 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011134 return nullptr;
11135
11136 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11137 EndLoc);
11138}
Alexey Bataeva0569352015-12-01 10:17:31 +000011139
11140OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11141 SourceLocation StartLoc,
11142 SourceLocation LParenLoc,
11143 SourceLocation EndLoc) {
11144 Expr *ValExpr = Priority;
11145
11146 // OpenMP [2.9.1, task Constrcut]
11147 // The priority-value is a non-negative numerical scalar expression.
11148 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11149 /*StrictlyPositive=*/false))
11150 return nullptr;
11151
11152 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11153}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011154
11155OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11156 SourceLocation StartLoc,
11157 SourceLocation LParenLoc,
11158 SourceLocation EndLoc) {
11159 Expr *ValExpr = Grainsize;
11160
11161 // OpenMP [2.9.2, taskloop Constrcut]
11162 // The parameter of the grainsize clause must be a positive integer
11163 // expression.
11164 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11165 /*StrictlyPositive=*/true))
11166 return nullptr;
11167
11168 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11169}
Alexey Bataev382967a2015-12-08 12:06:20 +000011170
11171OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11172 SourceLocation StartLoc,
11173 SourceLocation LParenLoc,
11174 SourceLocation EndLoc) {
11175 Expr *ValExpr = NumTasks;
11176
11177 // OpenMP [2.9.2, taskloop Constrcut]
11178 // The parameter of the num_tasks clause must be a positive integer
11179 // expression.
11180 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11181 /*StrictlyPositive=*/true))
11182 return nullptr;
11183
11184 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11185}
11186
Alexey Bataev28c75412015-12-15 08:19:24 +000011187OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11188 SourceLocation LParenLoc,
11189 SourceLocation EndLoc) {
11190 // OpenMP [2.13.2, critical construct, Description]
11191 // ... where hint-expression is an integer constant expression that evaluates
11192 // to a valid lock hint.
11193 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11194 if (HintExpr.isInvalid())
11195 return nullptr;
11196 return new (Context)
11197 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11198}
11199
Carlo Bertollib4adf552016-01-15 18:50:31 +000011200OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11201 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11202 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11203 SourceLocation EndLoc) {
11204 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11205 std::string Values;
11206 Values += "'";
11207 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11208 Values += "'";
11209 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11210 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11211 return nullptr;
11212 }
11213 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011214 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011215 if (ChunkSize) {
11216 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11217 !ChunkSize->isInstantiationDependent() &&
11218 !ChunkSize->containsUnexpandedParameterPack()) {
11219 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11220 ExprResult Val =
11221 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11222 if (Val.isInvalid())
11223 return nullptr;
11224
11225 ValExpr = Val.get();
11226
11227 // OpenMP [2.7.1, Restrictions]
11228 // chunk_size must be a loop invariant integer expression with a positive
11229 // value.
11230 llvm::APSInt Result;
11231 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11232 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11233 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11234 << "dist_schedule" << ChunkSize->getSourceRange();
11235 return nullptr;
11236 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011237 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11238 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011239 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11240 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11241 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011242 }
11243 }
11244 }
11245
11246 return new (Context)
11247 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011248 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011249}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011250
11251OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11252 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11253 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11254 SourceLocation KindLoc, SourceLocation EndLoc) {
11255 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11256 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11257 Kind != OMPC_DEFAULTMAP_scalar) {
11258 std::string Value;
11259 SourceLocation Loc;
11260 Value += "'";
11261 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11262 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11263 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11264 Loc = MLoc;
11265 } else {
11266 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11267 OMPC_DEFAULTMAP_scalar);
11268 Loc = KindLoc;
11269 }
11270 Value += "'";
11271 Diag(Loc, diag::err_omp_unexpected_clause_value)
11272 << Value << getOpenMPClauseName(OMPC_defaultmap);
11273 return nullptr;
11274 }
11275
11276 return new (Context)
11277 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11278}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011279
11280bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11281 DeclContext *CurLexicalContext = getCurLexicalContext();
11282 if (!CurLexicalContext->isFileContext() &&
11283 !CurLexicalContext->isExternCContext() &&
11284 !CurLexicalContext->isExternCXXContext()) {
11285 Diag(Loc, diag::err_omp_region_not_file_context);
11286 return false;
11287 }
11288 if (IsInOpenMPDeclareTargetContext) {
11289 Diag(Loc, diag::err_omp_enclosed_declare_target);
11290 return false;
11291 }
11292
11293 IsInOpenMPDeclareTargetContext = true;
11294 return true;
11295}
11296
11297void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11298 assert(IsInOpenMPDeclareTargetContext &&
11299 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11300
11301 IsInOpenMPDeclareTargetContext = false;
11302}
11303
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011304void
11305Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11306 const DeclarationNameInfo &Id,
11307 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11308 NamedDeclSetType &SameDirectiveDecls) {
11309 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11310 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11311
11312 if (Lookup.isAmbiguous())
11313 return;
11314 Lookup.suppressDiagnostics();
11315
11316 if (!Lookup.isSingleResult()) {
11317 if (TypoCorrection Corrected =
11318 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11319 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11320 CTK_ErrorRecovery)) {
11321 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11322 << Id.getName());
11323 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11324 return;
11325 }
11326
11327 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11328 return;
11329 }
11330
11331 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11332 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11333 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11334 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11335
11336 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11337 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11338 ND->addAttr(A);
11339 if (ASTMutationListener *ML = Context.getASTMutationListener())
11340 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11341 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11342 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11343 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11344 << Id.getName();
11345 }
11346 } else
11347 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11348}
11349
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011350static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11351 Sema &SemaRef, Decl *D) {
11352 if (!D)
11353 return;
11354 Decl *LD = nullptr;
11355 if (isa<TagDecl>(D)) {
11356 LD = cast<TagDecl>(D)->getDefinition();
11357 } else if (isa<VarDecl>(D)) {
11358 LD = cast<VarDecl>(D)->getDefinition();
11359
11360 // If this is an implicit variable that is legal and we do not need to do
11361 // anything.
11362 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011363 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11364 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11365 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011366 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011367 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011368 return;
11369 }
11370
11371 } else if (isa<FunctionDecl>(D)) {
11372 const FunctionDecl *FD = nullptr;
11373 if (cast<FunctionDecl>(D)->hasBody(FD))
11374 LD = const_cast<FunctionDecl *>(FD);
11375
11376 // If the definition is associated with the current declaration in the
11377 // target region (it can be e.g. a lambda) that is legal and we do not need
11378 // to do anything else.
11379 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011380 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11381 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11382 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011383 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011384 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011385 return;
11386 }
11387 }
11388 if (!LD)
11389 LD = D;
11390 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11391 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11392 // Outlined declaration is not declared target.
11393 if (LD->isOutOfLine()) {
11394 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11395 SemaRef.Diag(SL, diag::note_used_here) << SR;
11396 } else {
11397 DeclContext *DC = LD->getDeclContext();
11398 while (DC) {
11399 if (isa<FunctionDecl>(DC) &&
11400 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11401 break;
11402 DC = DC->getParent();
11403 }
11404 if (DC)
11405 return;
11406
11407 // Is not declared in target context.
11408 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11409 SemaRef.Diag(SL, diag::note_used_here) << SR;
11410 }
11411 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011412 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11413 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11414 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011415 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011416 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011417 }
11418}
11419
11420static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11421 Sema &SemaRef, DSAStackTy *Stack,
11422 ValueDecl *VD) {
11423 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11424 return true;
11425 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11426 return false;
11427 return true;
11428}
11429
11430void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11431 if (!D || D->isInvalidDecl())
11432 return;
11433 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11434 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11435 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11436 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11437 if (DSAStack->isThreadPrivate(VD)) {
11438 Diag(SL, diag::err_omp_threadprivate_in_target);
11439 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11440 return;
11441 }
11442 }
11443 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11444 // Problem if any with var declared with incomplete type will be reported
11445 // as normal, so no need to check it here.
11446 if ((E || !VD->getType()->isIncompleteType()) &&
11447 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11448 // Mark decl as declared target to prevent further diagnostic.
11449 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011450 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11451 Context, OMPDeclareTargetDeclAttr::MT_To);
11452 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011453 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011454 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011455 }
11456 return;
11457 }
11458 }
11459 if (!E) {
11460 // Checking declaration inside declare target region.
11461 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11462 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011463 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11464 Context, OMPDeclareTargetDeclAttr::MT_To);
11465 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011466 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011467 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011468 }
11469 return;
11470 }
11471 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11472}
Samuel Antao661c0902016-05-26 17:39:58 +000011473
11474OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11475 SourceLocation StartLoc,
11476 SourceLocation LParenLoc,
11477 SourceLocation EndLoc) {
11478 MappableVarListInfo MVLI(VarList);
11479 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11480 if (MVLI.ProcessedVarList.empty())
11481 return nullptr;
11482
11483 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11484 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11485 MVLI.VarComponents);
11486}
Samuel Antaoec172c62016-05-26 17:49:04 +000011487
11488OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11489 SourceLocation StartLoc,
11490 SourceLocation LParenLoc,
11491 SourceLocation EndLoc) {
11492 MappableVarListInfo MVLI(VarList);
11493 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11494 if (MVLI.ProcessedVarList.empty())
11495 return nullptr;
11496
11497 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11498 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11499 MVLI.VarComponents);
11500}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011501
11502OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11503 SourceLocation StartLoc,
11504 SourceLocation LParenLoc,
11505 SourceLocation EndLoc) {
11506 SmallVector<Expr *, 8> Vars;
11507 for (auto &RefExpr : VarList) {
11508 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11509 SourceLocation ELoc;
11510 SourceRange ERange;
11511 Expr *SimpleRefExpr = RefExpr;
11512 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11513 if (Res.second) {
11514 // It will be analyzed later.
11515 Vars.push_back(RefExpr);
11516 }
11517 ValueDecl *D = Res.first;
11518 if (!D)
11519 continue;
11520
11521 QualType Type = D->getType();
11522 // item should be a pointer or reference to pointer
11523 if (!Type.getNonReferenceType()->isPointerType()) {
11524 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11525 << 0 << RefExpr->getSourceRange();
11526 continue;
11527 }
11528 Vars.push_back(RefExpr->IgnoreParens());
11529 }
11530
11531 if (Vars.empty())
11532 return nullptr;
11533
11534 return OMPUseDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11535 Vars);
11536}
Carlo Bertolli70594e92016-07-13 17:16:49 +000011537
11538OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11539 SourceLocation StartLoc,
11540 SourceLocation LParenLoc,
11541 SourceLocation EndLoc) {
11542 SmallVector<Expr *, 8> Vars;
11543 for (auto &RefExpr : VarList) {
11544 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11545 SourceLocation ELoc;
11546 SourceRange ERange;
11547 Expr *SimpleRefExpr = RefExpr;
11548 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11549 if (Res.second) {
11550 // It will be analyzed later.
11551 Vars.push_back(RefExpr);
11552 }
11553 ValueDecl *D = Res.first;
11554 if (!D)
11555 continue;
11556
11557 QualType Type = D->getType();
11558 // item should be a pointer or array or reference to pointer or array
11559 if (!Type.getNonReferenceType()->isPointerType() &&
11560 !Type.getNonReferenceType()->isArrayType()) {
11561 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11562 << 0 << RefExpr->getSourceRange();
11563 continue;
11564 }
11565 Vars.push_back(RefExpr->IgnoreParens());
11566 }
11567
11568 if (Vars.empty())
11569 return nullptr;
11570
11571 return OMPIsDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11572 Vars);
11573}