blob: 979c5215f111415b8a6dc4d34d4903f965bf07da [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) {
1586 case OMPD_parallel: {
1587 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001588 QualType KmpInt32PtrTy =
1589 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001590 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001591 std::make_pair(".global_tid.", KmpInt32PtrTy),
1592 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1593 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001594 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001595 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1596 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001597 break;
1598 }
1599 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001600 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001601 std::make_pair(StringRef(), QualType()) // __context with shared vars
1602 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001603 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1604 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001605 break;
1606 }
1607 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001608 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001609 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001610 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001611 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1612 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001613 break;
1614 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001615 case OMPD_for_simd: {
1616 Sema::CapturedParamNameType Params[] = {
1617 std::make_pair(StringRef(), QualType()) // __context with shared vars
1618 };
1619 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1620 Params);
1621 break;
1622 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001623 case OMPD_sections: {
1624 Sema::CapturedParamNameType Params[] = {
1625 std::make_pair(StringRef(), QualType()) // __context with shared vars
1626 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001627 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1628 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001629 break;
1630 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001631 case OMPD_section: {
1632 Sema::CapturedParamNameType Params[] = {
1633 std::make_pair(StringRef(), QualType()) // __context with shared vars
1634 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001635 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1636 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001637 break;
1638 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001639 case OMPD_single: {
1640 Sema::CapturedParamNameType Params[] = {
1641 std::make_pair(StringRef(), QualType()) // __context with shared vars
1642 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001643 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1644 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001645 break;
1646 }
Alexander Musman80c22892014-07-17 08:54:58 +00001647 case OMPD_master: {
1648 Sema::CapturedParamNameType Params[] = {
1649 std::make_pair(StringRef(), QualType()) // __context with shared vars
1650 };
1651 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1652 Params);
1653 break;
1654 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001655 case OMPD_critical: {
1656 Sema::CapturedParamNameType Params[] = {
1657 std::make_pair(StringRef(), QualType()) // __context with shared vars
1658 };
1659 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1660 Params);
1661 break;
1662 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001663 case OMPD_parallel_for: {
1664 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001665 QualType KmpInt32PtrTy =
1666 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001667 Sema::CapturedParamNameType Params[] = {
1668 std::make_pair(".global_tid.", KmpInt32PtrTy),
1669 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1670 std::make_pair(StringRef(), QualType()) // __context with shared vars
1671 };
1672 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1673 Params);
1674 break;
1675 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001676 case OMPD_parallel_for_simd: {
1677 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001678 QualType KmpInt32PtrTy =
1679 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001680 Sema::CapturedParamNameType Params[] = {
1681 std::make_pair(".global_tid.", KmpInt32PtrTy),
1682 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1683 std::make_pair(StringRef(), QualType()) // __context with shared vars
1684 };
1685 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1686 Params);
1687 break;
1688 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001689 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001690 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001691 QualType KmpInt32PtrTy =
1692 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001693 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001694 std::make_pair(".global_tid.", KmpInt32PtrTy),
1695 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001696 std::make_pair(StringRef(), QualType()) // __context with shared vars
1697 };
1698 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1699 Params);
1700 break;
1701 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001702 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001703 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001704 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1705 FunctionProtoType::ExtProtoInfo EPI;
1706 EPI.Variadic = true;
1707 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001708 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001709 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001710 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1711 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1712 std::make_pair(".copy_fn.",
1713 Context.getPointerType(CopyFnType).withConst()),
1714 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001715 std::make_pair(StringRef(), QualType()) // __context with shared vars
1716 };
1717 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1718 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001719 // Mark this captured region as inlined, because we don't use outlined
1720 // function directly.
1721 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1722 AlwaysInlineAttr::CreateImplicit(
1723 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001724 break;
1725 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001726 case OMPD_ordered: {
1727 Sema::CapturedParamNameType Params[] = {
1728 std::make_pair(StringRef(), QualType()) // __context with shared vars
1729 };
1730 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1731 Params);
1732 break;
1733 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001734 case OMPD_atomic: {
1735 Sema::CapturedParamNameType Params[] = {
1736 std::make_pair(StringRef(), QualType()) // __context with shared vars
1737 };
1738 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1739 Params);
1740 break;
1741 }
Michael Wong65f367f2015-07-21 13:44:28 +00001742 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001743 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001744 case OMPD_target_parallel:
1745 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001746 Sema::CapturedParamNameType Params[] = {
1747 std::make_pair(StringRef(), QualType()) // __context with shared vars
1748 };
1749 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1750 Params);
1751 break;
1752 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001753 case OMPD_teams: {
1754 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001755 QualType KmpInt32PtrTy =
1756 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001757 Sema::CapturedParamNameType Params[] = {
1758 std::make_pair(".global_tid.", KmpInt32PtrTy),
1759 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1760 std::make_pair(StringRef(), QualType()) // __context with shared vars
1761 };
1762 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1763 Params);
1764 break;
1765 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001766 case OMPD_taskgroup: {
1767 Sema::CapturedParamNameType Params[] = {
1768 std::make_pair(StringRef(), QualType()) // __context with shared vars
1769 };
1770 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1771 Params);
1772 break;
1773 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001774 case OMPD_taskloop:
1775 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001776 QualType KmpInt32Ty =
1777 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1778 QualType KmpUInt64Ty =
1779 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1780 QualType KmpInt64Ty =
1781 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1782 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1783 FunctionProtoType::ExtProtoInfo EPI;
1784 EPI.Variadic = true;
1785 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001786 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001787 std::make_pair(".global_tid.", KmpInt32Ty),
1788 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1789 std::make_pair(".privates.",
1790 Context.VoidPtrTy.withConst().withRestrict()),
1791 std::make_pair(
1792 ".copy_fn.",
1793 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1794 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1795 std::make_pair(".lb.", KmpUInt64Ty),
1796 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1797 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001798 std::make_pair(StringRef(), QualType()) // __context with shared vars
1799 };
1800 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1801 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001802 // Mark this captured region as inlined, because we don't use outlined
1803 // function directly.
1804 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1805 AlwaysInlineAttr::CreateImplicit(
1806 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001807 break;
1808 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001809 case OMPD_distribute: {
1810 Sema::CapturedParamNameType Params[] = {
1811 std::make_pair(StringRef(), QualType()) // __context with shared vars
1812 };
1813 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1814 Params);
1815 break;
1816 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001817 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001818 case OMPD_distribute_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001819 case OMPD_distribute_parallel_for: {
1820 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1821 QualType KmpInt32PtrTy =
1822 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1823 Sema::CapturedParamNameType Params[] = {
1824 std::make_pair(".global_tid.", KmpInt32PtrTy),
1825 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1826 std::make_pair(".previous.lb.", Context.getSizeType()),
1827 std::make_pair(".previous.ub.", Context.getSizeType()),
1828 std::make_pair(StringRef(), QualType()) // __context with shared vars
1829 };
1830 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1831 Params);
1832 break;
1833 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001834 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001835 case OMPD_taskyield:
1836 case OMPD_barrier:
1837 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001838 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001839 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001840 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001841 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001842 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001843 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001844 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001845 case OMPD_declare_target:
1846 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001847 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001848 llvm_unreachable("OpenMP Directive is not allowed");
1849 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001850 llvm_unreachable("Unknown OpenMP directive");
1851 }
1852}
1853
Alexey Bataev3392d762016-02-16 11:18:12 +00001854static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001855 Expr *CaptureExpr, bool WithInit,
1856 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001857 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001858 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001859 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001860 QualType Ty = Init->getType();
1861 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1862 if (S.getLangOpts().CPlusPlus)
1863 Ty = C.getLValueReferenceType(Ty);
1864 else {
1865 Ty = C.getPointerType(Ty);
1866 ExprResult Res =
1867 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1868 if (!Res.isUsable())
1869 return nullptr;
1870 Init = Res.get();
1871 }
Alexey Bataev61205072016-03-02 04:57:40 +00001872 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001873 }
1874 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001875 if (!WithInit)
1876 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001877 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001878 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1879 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001880 return CED;
1881}
1882
Alexey Bataev61205072016-03-02 04:57:40 +00001883static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1884 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001885 OMPCapturedExprDecl *CD;
1886 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1887 CD = cast<OMPCapturedExprDecl>(VD);
1888 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001889 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1890 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001891 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001892 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001893}
1894
Alexey Bataev5a3af132016-03-29 08:58:54 +00001895static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1896 if (!Ref) {
1897 auto *CD =
1898 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1899 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1900 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1901 CaptureExpr->getExprLoc());
1902 }
1903 ExprResult Res = Ref;
1904 if (!S.getLangOpts().CPlusPlus &&
1905 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1906 Ref->getType()->isPointerType())
1907 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1908 if (!Res.isUsable())
1909 return ExprError();
1910 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001911}
1912
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001913StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1914 ArrayRef<OMPClause *> Clauses) {
1915 if (!S.isUsable()) {
1916 ActOnCapturedRegionError();
1917 return StmtError();
1918 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001919
1920 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001921 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001922 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001923 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001924 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001925 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001926 Clause->getClauseKind() == OMPC_copyprivate ||
1927 (getLangOpts().OpenMPUseTLS &&
1928 getASTContext().getTargetInfo().isTLSSupported() &&
1929 Clause->getClauseKind() == OMPC_copyin)) {
1930 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001931 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001932 for (auto *VarRef : Clause->children()) {
1933 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001934 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001935 }
1936 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001937 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001938 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001939 // Mark all variables in private list clauses as used in inner region.
1940 // Required for proper codegen of combined directives.
1941 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001942 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001943 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1944 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001945 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1946 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001947 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001948 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1949 if (auto *E = C->getPostUpdateExpr())
1950 MarkDeclarationsReferencedInExpr(E);
1951 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001952 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001953 if (Clause->getClauseKind() == OMPC_schedule)
1954 SC = cast<OMPScheduleClause>(Clause);
1955 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001956 OC = cast<OMPOrderedClause>(Clause);
1957 else if (Clause->getClauseKind() == OMPC_linear)
1958 LCs.push_back(cast<OMPLinearClause>(Clause));
1959 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001960 bool ErrorFound = false;
1961 // OpenMP, 2.7.1 Loop Construct, Restrictions
1962 // The nonmonotonic modifier cannot be specified if an ordered clause is
1963 // specified.
1964 if (SC &&
1965 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1966 SC->getSecondScheduleModifier() ==
1967 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1968 OC) {
1969 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1970 ? SC->getFirstScheduleModifierLoc()
1971 : SC->getSecondScheduleModifierLoc(),
1972 diag::err_omp_schedule_nonmonotonic_ordered)
1973 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1974 ErrorFound = true;
1975 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001976 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1977 for (auto *C : LCs) {
1978 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1979 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1980 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001981 ErrorFound = true;
1982 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001983 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1984 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1985 OC->getNumForLoops()) {
1986 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1987 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1988 ErrorFound = true;
1989 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001990 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001991 ActOnCapturedRegionError();
1992 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001993 }
1994 return ActOnCapturedRegionEnd(S.get());
1995}
1996
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001997static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1998 OpenMPDirectiveKind CurrentRegion,
1999 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002000 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002001 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002002 // Allowed nesting of constructs
2003 // +------------------+-----------------+------------------------------------+
2004 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
2005 // +------------------+-----------------+------------------------------------+
2006 // | parallel | parallel | * |
2007 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002008 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002009 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002010 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002011 // | parallel | simd | * |
2012 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002013 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002014 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002015 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002016 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002017 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002018 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002019 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002020 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002021 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002022 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002023 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002024 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002025 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002026 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002027 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002028 // | parallel | target parallel | * |
2029 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002030 // | parallel | target enter | * |
2031 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002032 // | parallel | target exit | * |
2033 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002034 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002035 // | parallel | cancellation | |
2036 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002037 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002038 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002039 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002040 // | parallel | distribute | + |
2041 // | parallel | distribute | + |
2042 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002043 // | parallel | distribute | + |
2044 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002045 // | parallel | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002046 // +------------------+-----------------+------------------------------------+
2047 // | for | parallel | * |
2048 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002049 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002050 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002051 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002052 // | for | simd | * |
2053 // | for | sections | + |
2054 // | for | section | + |
2055 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002056 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002057 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002058 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002059 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002060 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002061 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002062 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002063 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002064 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002065 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002066 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002067 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002068 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002069 // | for | target parallel | * |
2070 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002071 // | for | target enter | * |
2072 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002073 // | for | target exit | * |
2074 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002075 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002076 // | for | cancellation | |
2077 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002078 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002079 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002080 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002081 // | for | distribute | + |
2082 // | for | distribute | + |
2083 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002084 // | for | distribute | + |
2085 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002086 // | for | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002087 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00002088 // | master | parallel | * |
2089 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002090 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002091 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002092 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002093 // | master | simd | * |
2094 // | master | sections | + |
2095 // | master | section | + |
2096 // | master | single | + |
2097 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002098 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00002099 // | master |parallel sections| * |
2100 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002101 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002102 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002103 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002104 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002105 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002106 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002107 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002108 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002109 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002110 // | master | target parallel | * |
2111 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002112 // | master | target enter | * |
2113 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002114 // | master | target exit | * |
2115 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002116 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002117 // | master | cancellation | |
2118 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002119 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002120 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002121 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002122 // | master | distribute | + |
2123 // | master | distribute | + |
2124 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002125 // | master | distribute | + |
2126 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002127 // | master | distribute simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002128 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002129 // | critical | parallel | * |
2130 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002131 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002132 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002133 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002134 // | critical | simd | * |
2135 // | critical | sections | + |
2136 // | critical | section | + |
2137 // | critical | single | + |
2138 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002139 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002140 // | critical |parallel sections| * |
2141 // | critical | task | * |
2142 // | critical | taskyield | * |
2143 // | critical | barrier | + |
2144 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002145 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002146 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002147 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002148 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002149 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002150 // | critical | target parallel | * |
2151 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002152 // | critical | target enter | * |
2153 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002154 // | critical | target exit | * |
2155 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002156 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002157 // | critical | cancellation | |
2158 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002159 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002160 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002161 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002162 // | critical | distribute | + |
2163 // | critical | distribute | + |
2164 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002165 // | critical | distribute | + |
2166 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002167 // | critical | distribute simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002168 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002169 // | simd | parallel | |
2170 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002171 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002172 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002173 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002174 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002175 // | simd | sections | |
2176 // | simd | section | |
2177 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002178 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002179 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002180 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002181 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002182 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002183 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002184 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002185 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002186 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002187 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002188 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002189 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002190 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002191 // | simd | target parallel | |
2192 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002193 // | simd | target enter | |
2194 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002195 // | simd | target exit | |
2196 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002197 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002198 // | simd | cancellation | |
2199 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002200 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002201 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002202 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002203 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002204 // | simd | distribute | |
2205 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002206 // | simd | distribute | |
2207 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002208 // | simd | distribute simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002209 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002210 // | for simd | parallel | |
2211 // | for simd | for | |
2212 // | for simd | for simd | |
2213 // | for simd | master | |
2214 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002215 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002216 // | for simd | sections | |
2217 // | for simd | section | |
2218 // | for simd | single | |
2219 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002220 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002221 // | for simd |parallel sections| |
2222 // | for simd | task | |
2223 // | for simd | taskyield | |
2224 // | for simd | barrier | |
2225 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002226 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002227 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002228 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002229 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002230 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002231 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002232 // | for simd | target parallel | |
2233 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002234 // | for simd | target enter | |
2235 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002236 // | for simd | target exit | |
2237 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002238 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002239 // | for simd | cancellation | |
2240 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002241 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002242 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002243 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002244 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002245 // | for simd | distribute | |
2246 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002247 // | for simd | distribute | |
2248 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002249 // | for simd | distribute simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002250 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002251 // | parallel for simd| parallel | |
2252 // | parallel for simd| for | |
2253 // | parallel for simd| for simd | |
2254 // | parallel for simd| master | |
2255 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002256 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002257 // | parallel for simd| sections | |
2258 // | parallel for simd| section | |
2259 // | parallel for simd| single | |
2260 // | parallel for simd| parallel for | |
2261 // | parallel for simd|parallel for simd| |
2262 // | parallel for simd|parallel sections| |
2263 // | parallel for simd| task | |
2264 // | parallel for simd| taskyield | |
2265 // | parallel for simd| barrier | |
2266 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002267 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002268 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002269 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002270 // | parallel for simd| atomic | |
2271 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002272 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002273 // | parallel for simd| target parallel | |
2274 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002275 // | parallel for simd| target enter | |
2276 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002277 // | parallel for simd| target exit | |
2278 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002279 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002280 // | parallel for simd| cancellation | |
2281 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002282 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002283 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002284 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002285 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002286 // | parallel for simd| distribute | |
2287 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002288 // | parallel for simd| distribute | |
2289 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002290 // | parallel for simd| distribute simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002291 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002292 // | sections | parallel | * |
2293 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002294 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002295 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002296 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002297 // | sections | simd | * |
2298 // | sections | sections | + |
2299 // | sections | section | * |
2300 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002301 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002302 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002303 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002304 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002305 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002306 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002307 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002308 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002309 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002310 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002311 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002312 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002313 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002314 // | sections | target parallel | * |
2315 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002316 // | sections | target enter | * |
2317 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002318 // | sections | target exit | * |
2319 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002320 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002321 // | sections | cancellation | |
2322 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002323 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002324 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002325 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002326 // | sections | distribute | + |
2327 // | sections | distribute | + |
2328 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002329 // | sections | distribute | + |
2330 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002331 // | sections | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002332 // +------------------+-----------------+------------------------------------+
2333 // | section | parallel | * |
2334 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002335 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002336 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002337 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002338 // | section | simd | * |
2339 // | section | sections | + |
2340 // | section | section | + |
2341 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002342 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002343 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002344 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002345 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002346 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002347 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002348 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002349 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002350 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002351 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002352 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002353 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002354 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002355 // | section | target parallel | * |
2356 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002357 // | section | target enter | * |
2358 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002359 // | section | target exit | * |
2360 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002361 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002362 // | section | cancellation | |
2363 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002364 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002365 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002366 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002367 // | section | distribute | + |
2368 // | section | distribute | + |
2369 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002370 // | section | distribute | + |
2371 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002372 // | section | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002373 // +------------------+-----------------+------------------------------------+
2374 // | single | parallel | * |
2375 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002376 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002377 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002378 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002379 // | single | simd | * |
2380 // | single | sections | + |
2381 // | single | section | + |
2382 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002383 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002384 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002385 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002386 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002387 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002388 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002389 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002390 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002391 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002392 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002393 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002394 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002395 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002396 // | single | target parallel | * |
2397 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002398 // | single | target enter | * |
2399 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002400 // | single | target exit | * |
2401 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002402 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002403 // | single | cancellation | |
2404 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002405 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002406 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002407 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002408 // | single | distribute | + |
2409 // | single | distribute | + |
2410 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002411 // | single | distribute | + |
2412 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002413 // | single | distribute simd | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002414 // +------------------+-----------------+------------------------------------+
2415 // | parallel for | parallel | * |
2416 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002417 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002418 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002419 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002420 // | parallel for | simd | * |
2421 // | parallel for | sections | + |
2422 // | parallel for | section | + |
2423 // | parallel for | single | + |
2424 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002425 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002426 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002427 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002428 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002429 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002430 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002431 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002432 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002433 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002434 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002435 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002436 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002437 // | parallel for | target parallel | * |
2438 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002439 // | parallel for | target enter | * |
2440 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002441 // | parallel for | target exit | * |
2442 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002443 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002444 // | parallel for | cancellation | |
2445 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002446 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002447 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002448 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002449 // | parallel for | distribute | + |
2450 // | parallel for | distribute | + |
2451 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002452 // | parallel for | distribute | + |
2453 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002454 // | parallel for | distribute simd | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002455 // +------------------+-----------------+------------------------------------+
2456 // | parallel sections| parallel | * |
2457 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002458 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002459 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002460 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002461 // | parallel sections| simd | * |
2462 // | parallel sections| sections | + |
2463 // | parallel sections| section | * |
2464 // | parallel sections| single | + |
2465 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002466 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002467 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002468 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002469 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002470 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002471 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002472 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002473 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002474 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002475 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002476 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002477 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002478 // | parallel sections| target parallel | * |
2479 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002480 // | parallel sections| target enter | * |
2481 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002482 // | parallel sections| target exit | * |
2483 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002484 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002485 // | parallel sections| cancellation | |
2486 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002487 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002488 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002489 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002490 // | parallel sections| distribute | + |
2491 // | parallel sections| distribute | + |
2492 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002493 // | parallel sections| distribute | + |
2494 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002495 // | parallel sections| distribute simd | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002496 // +------------------+-----------------+------------------------------------+
2497 // | task | parallel | * |
2498 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002499 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002500 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002501 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002502 // | task | simd | * |
2503 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002504 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002505 // | task | single | + |
2506 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002507 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002508 // | task |parallel sections| * |
2509 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002510 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002511 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002512 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002513 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002514 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002515 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002516 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002517 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002518 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002519 // | task | target parallel | * |
2520 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002521 // | task | target enter | * |
2522 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002523 // | task | target exit | * |
2524 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002525 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002526 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002527 // | | point | ! |
2528 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002529 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002530 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002531 // | task | distribute | + |
2532 // | task | distribute | + |
2533 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002534 // | task | distribute | + |
2535 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002536 // | task | distribute simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002537 // +------------------+-----------------+------------------------------------+
2538 // | ordered | parallel | * |
2539 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002540 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002541 // | ordered | master | * |
2542 // | ordered | critical | * |
2543 // | ordered | simd | * |
2544 // | ordered | sections | + |
2545 // | ordered | section | + |
2546 // | ordered | single | + |
2547 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002548 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002549 // | ordered |parallel sections| * |
2550 // | ordered | task | * |
2551 // | ordered | taskyield | * |
2552 // | ordered | barrier | + |
2553 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002554 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002555 // | ordered | flush | * |
2556 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002557 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002558 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002559 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002560 // | ordered | target parallel | * |
2561 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002562 // | ordered | target enter | * |
2563 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002564 // | ordered | target exit | * |
2565 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002566 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002567 // | ordered | cancellation | |
2568 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002569 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002570 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002571 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002572 // | ordered | distribute | + |
2573 // | ordered | distribute | + |
2574 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002575 // | ordered | distribute | + |
2576 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002577 // | ordered | distribute simd | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002578 // +------------------+-----------------+------------------------------------+
2579 // | atomic | parallel | |
2580 // | atomic | for | |
2581 // | atomic | for simd | |
2582 // | atomic | master | |
2583 // | atomic | critical | |
2584 // | atomic | simd | |
2585 // | atomic | sections | |
2586 // | atomic | section | |
2587 // | atomic | single | |
2588 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002589 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002590 // | atomic |parallel sections| |
2591 // | atomic | task | |
2592 // | atomic | taskyield | |
2593 // | atomic | barrier | |
2594 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002595 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002596 // | atomic | flush | |
2597 // | atomic | ordered | |
2598 // | atomic | atomic | |
2599 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002600 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002601 // | atomic | target parallel | |
2602 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002603 // | atomic | target enter | |
2604 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002605 // | atomic | target exit | |
2606 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002607 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002608 // | atomic | cancellation | |
2609 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002610 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002611 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002612 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002613 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002614 // | atomic | distribute | |
2615 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002616 // | atomic | distribute | |
2617 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002618 // | atomic | distribute simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002619 // +------------------+-----------------+------------------------------------+
2620 // | target | parallel | * |
2621 // | target | for | * |
2622 // | target | for simd | * |
2623 // | target | master | * |
2624 // | target | critical | * |
2625 // | target | simd | * |
2626 // | target | sections | * |
2627 // | target | section | * |
2628 // | target | single | * |
2629 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002630 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002631 // | target |parallel sections| * |
2632 // | target | task | * |
2633 // | target | taskyield | * |
2634 // | target | barrier | * |
2635 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002636 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002637 // | target | flush | * |
2638 // | target | ordered | * |
2639 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002640 // | target | target | |
2641 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002642 // | target | target parallel | |
2643 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002644 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002645 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002646 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002647 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002648 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002649 // | target | cancellation | |
2650 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002651 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002652 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002653 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002654 // | target | distribute | + |
2655 // | target | distribute | + |
2656 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002657 // | target | distribute | + |
2658 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002659 // | target | distribute simd | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002660 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002661 // | target parallel | parallel | * |
2662 // | target parallel | for | * |
2663 // | target parallel | for simd | * |
2664 // | target parallel | master | * |
2665 // | target parallel | critical | * |
2666 // | target parallel | simd | * |
2667 // | target parallel | sections | * |
2668 // | target parallel | section | * |
2669 // | target parallel | single | * |
2670 // | target parallel | parallel for | * |
2671 // | target parallel |parallel for simd| * |
2672 // | target parallel |parallel sections| * |
2673 // | target parallel | task | * |
2674 // | target parallel | taskyield | * |
2675 // | target parallel | barrier | * |
2676 // | target parallel | taskwait | * |
2677 // | target parallel | taskgroup | * |
2678 // | target parallel | flush | * |
2679 // | target parallel | ordered | * |
2680 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002681 // | target parallel | target | |
2682 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002683 // | target parallel | target parallel | |
2684 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002685 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002686 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002687 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002688 // | | data | |
2689 // | target parallel | teams | |
2690 // | target parallel | cancellation | |
2691 // | | point | ! |
2692 // | target parallel | cancel | ! |
2693 // | target parallel | taskloop | * |
2694 // | target parallel | taskloop simd | * |
2695 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002696 // | target parallel | distribute | |
2697 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002698 // | target parallel | distribute | |
2699 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002700 // | target parallel | distribute simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002701 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002702 // | target parallel | parallel | * |
2703 // | for | | |
2704 // | target parallel | for | * |
2705 // | for | | |
2706 // | target parallel | for simd | * |
2707 // | for | | |
2708 // | target parallel | master | * |
2709 // | for | | |
2710 // | target parallel | critical | * |
2711 // | for | | |
2712 // | target parallel | simd | * |
2713 // | for | | |
2714 // | target parallel | sections | * |
2715 // | for | | |
2716 // | target parallel | section | * |
2717 // | for | | |
2718 // | target parallel | single | * |
2719 // | for | | |
2720 // | target parallel | parallel for | * |
2721 // | for | | |
2722 // | target parallel |parallel for simd| * |
2723 // | for | | |
2724 // | target parallel |parallel sections| * |
2725 // | for | | |
2726 // | target parallel | task | * |
2727 // | for | | |
2728 // | target parallel | taskyield | * |
2729 // | for | | |
2730 // | target parallel | barrier | * |
2731 // | for | | |
2732 // | target parallel | taskwait | * |
2733 // | for | | |
2734 // | target parallel | taskgroup | * |
2735 // | for | | |
2736 // | target parallel | flush | * |
2737 // | for | | |
2738 // | target parallel | ordered | * |
2739 // | for | | |
2740 // | target parallel | atomic | * |
2741 // | for | | |
2742 // | target parallel | target | |
2743 // | for | | |
2744 // | target parallel | target parallel | |
2745 // | for | | |
2746 // | target parallel | target parallel | |
2747 // | for | for | |
2748 // | target parallel | target enter | |
2749 // | for | data | |
2750 // | target parallel | target exit | |
2751 // | for | data | |
2752 // | target parallel | teams | |
2753 // | for | | |
2754 // | target parallel | cancellation | |
2755 // | for | point | ! |
2756 // | target parallel | cancel | ! |
2757 // | for | | |
2758 // | target parallel | taskloop | * |
2759 // | for | | |
2760 // | target parallel | taskloop simd | * |
2761 // | for | | |
2762 // | target parallel | distribute | |
2763 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002764 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002765 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002766 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002767 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002768 // | target parallel | distribute simd | |
2769 // | for | | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002770 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002771 // | teams | parallel | * |
2772 // | teams | for | + |
2773 // | teams | for simd | + |
2774 // | teams | master | + |
2775 // | teams | critical | + |
2776 // | teams | simd | + |
2777 // | teams | sections | + |
2778 // | teams | section | + |
2779 // | teams | single | + |
2780 // | teams | parallel for | * |
2781 // | teams |parallel for simd| * |
2782 // | teams |parallel sections| * |
2783 // | teams | task | + |
2784 // | teams | taskyield | + |
2785 // | teams | barrier | + |
2786 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002787 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002788 // | teams | flush | + |
2789 // | teams | ordered | + |
2790 // | teams | atomic | + |
2791 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002792 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002793 // | teams | target parallel | + |
2794 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002795 // | teams | target enter | + |
2796 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002797 // | teams | target exit | + |
2798 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002799 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002800 // | teams | cancellation | |
2801 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002802 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002803 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002804 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002805 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002806 // | teams | distribute | ! |
2807 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002808 // | teams | distribute | ! |
2809 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002810 // | teams | distribute simd | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002811 // +------------------+-----------------+------------------------------------+
2812 // | taskloop | parallel | * |
2813 // | taskloop | for | + |
2814 // | taskloop | for simd | + |
2815 // | taskloop | master | + |
2816 // | taskloop | critical | * |
2817 // | taskloop | simd | * |
2818 // | taskloop | sections | + |
2819 // | taskloop | section | + |
2820 // | taskloop | single | + |
2821 // | taskloop | parallel for | * |
2822 // | taskloop |parallel for simd| * |
2823 // | taskloop |parallel sections| * |
2824 // | taskloop | task | * |
2825 // | taskloop | taskyield | * |
2826 // | taskloop | barrier | + |
2827 // | taskloop | taskwait | * |
2828 // | taskloop | taskgroup | * |
2829 // | taskloop | flush | * |
2830 // | taskloop | ordered | + |
2831 // | taskloop | atomic | * |
2832 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002833 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002834 // | taskloop | target parallel | * |
2835 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002836 // | taskloop | target enter | * |
2837 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002838 // | taskloop | target exit | * |
2839 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002840 // | taskloop | teams | + |
2841 // | taskloop | cancellation | |
2842 // | | point | |
2843 // | taskloop | cancel | |
2844 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002845 // | taskloop | distribute | + |
2846 // | taskloop | distribute | + |
2847 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002848 // | taskloop | distribute | + |
2849 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002850 // | taskloop | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002851 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002852 // | taskloop simd | parallel | |
2853 // | taskloop simd | for | |
2854 // | taskloop simd | for simd | |
2855 // | taskloop simd | master | |
2856 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002857 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002858 // | taskloop simd | sections | |
2859 // | taskloop simd | section | |
2860 // | taskloop simd | single | |
2861 // | taskloop simd | parallel for | |
2862 // | taskloop simd |parallel for simd| |
2863 // | taskloop simd |parallel sections| |
2864 // | taskloop simd | task | |
2865 // | taskloop simd | taskyield | |
2866 // | taskloop simd | barrier | |
2867 // | taskloop simd | taskwait | |
2868 // | taskloop simd | taskgroup | |
2869 // | taskloop simd | flush | |
2870 // | taskloop simd | ordered | + (with simd clause) |
2871 // | taskloop simd | atomic | |
2872 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002873 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002874 // | taskloop simd | target parallel | |
2875 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002876 // | taskloop simd | target enter | |
2877 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002878 // | taskloop simd | target exit | |
2879 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002880 // | taskloop simd | teams | |
2881 // | taskloop simd | cancellation | |
2882 // | | point | |
2883 // | taskloop simd | cancel | |
2884 // | taskloop simd | taskloop | |
2885 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002886 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002887 // | taskloop simd | distribute | |
2888 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002889 // | taskloop simd | distribute | |
2890 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002891 // | taskloop simd | distribute simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002892 // +------------------+-----------------+------------------------------------+
2893 // | distribute | parallel | * |
2894 // | distribute | for | * |
2895 // | distribute | for simd | * |
2896 // | distribute | master | * |
2897 // | distribute | critical | * |
2898 // | distribute | simd | * |
2899 // | distribute | sections | * |
2900 // | distribute | section | * |
2901 // | distribute | single | * |
2902 // | distribute | parallel for | * |
2903 // | distribute |parallel for simd| * |
2904 // | distribute |parallel sections| * |
2905 // | distribute | task | * |
2906 // | distribute | taskyield | * |
2907 // | distribute | barrier | * |
2908 // | distribute | taskwait | * |
2909 // | distribute | taskgroup | * |
2910 // | distribute | flush | * |
2911 // | distribute | ordered | + |
2912 // | distribute | atomic | * |
2913 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002914 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002915 // | distribute | target parallel | |
2916 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002917 // | distribute | target enter | |
2918 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002919 // | distribute | target exit | |
2920 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002921 // | distribute | teams | |
2922 // | distribute | cancellation | + |
2923 // | | point | |
2924 // | distribute | cancel | + |
2925 // | distribute | taskloop | * |
2926 // | distribute | taskloop simd | * |
2927 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002928 // | distribute | distribute | |
2929 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002930 // | distribute | distribute | |
2931 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002932 // | distribute | distribute simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002933 // +------------------+-----------------+------------------------------------+
2934 // | distribute | parallel | * |
2935 // | parallel for | | |
2936 // | distribute | for | * |
2937 // | parallel for | | |
2938 // | distribute | for simd | * |
2939 // | parallel for | | |
2940 // | distribute | master | * |
2941 // | parallel for | | |
2942 // | distribute | critical | * |
2943 // | parallel for | | |
2944 // | distribute | simd | * |
2945 // | parallel for | | |
2946 // | distribute | sections | * |
2947 // | parallel for | | |
2948 // | distribute | section | * |
2949 // | parallel for | | |
2950 // | distribute | single | * |
2951 // | parallel for | | |
2952 // | distribute | parallel for | * |
2953 // | parallel for | | |
2954 // | distribute |parallel for simd| * |
2955 // | parallel for | | |
2956 // | distribute |parallel sections| * |
2957 // | parallel for | | |
2958 // | distribute | task | * |
2959 // | parallel for | | |
2960 // | parallel for | | |
2961 // | distribute | taskyield | * |
2962 // | parallel for | | |
2963 // | distribute | barrier | * |
2964 // | parallel for | | |
2965 // | distribute | taskwait | * |
2966 // | parallel for | | |
2967 // | distribute | taskgroup | * |
2968 // | parallel for | | |
2969 // | distribute | flush | * |
2970 // | parallel for | | |
2971 // | distribute | ordered | + |
2972 // | parallel for | | |
2973 // | distribute | atomic | * |
2974 // | parallel for | | |
2975 // | distribute | target | |
2976 // | parallel for | | |
2977 // | distribute | target parallel | |
2978 // | parallel for | | |
2979 // | distribute | target parallel | |
2980 // | parallel for | for | |
2981 // | distribute | target enter | |
2982 // | parallel for | data | |
2983 // | distribute | target exit | |
2984 // | parallel for | data | |
2985 // | distribute | teams | |
2986 // | parallel for | | |
2987 // | distribute | cancellation | + |
2988 // | parallel for | point | |
2989 // | distribute | cancel | + |
2990 // | parallel for | | |
2991 // | distribute | taskloop | * |
2992 // | parallel for | | |
2993 // | distribute | taskloop simd | * |
2994 // | parallel for | | |
2995 // | distribute | distribute | |
2996 // | parallel for | | |
2997 // | distribute | distribute | |
2998 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002999 // | distribute | distribute | |
3000 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003001 // | distribute | distribute simd | |
3002 // | parallel for | | |
Kelvin Li4a39add2016-07-05 05:00:15 +00003003 // +------------------+-----------------+------------------------------------+
3004 // | distribute | parallel | * |
3005 // | parallel for simd| | |
3006 // | distribute | for | * |
3007 // | parallel for simd| | |
3008 // | distribute | for simd | * |
3009 // | parallel for simd| | |
3010 // | distribute | master | * |
3011 // | parallel for simd| | |
3012 // | distribute | critical | * |
3013 // | parallel for simd| | |
3014 // | distribute | simd | * |
3015 // | parallel for simd| | |
3016 // | distribute | sections | * |
3017 // | parallel for simd| | |
3018 // | distribute | section | * |
3019 // | parallel for simd| | |
3020 // | distribute | single | * |
3021 // | parallel for simd| | |
3022 // | distribute | parallel for | * |
3023 // | parallel for simd| | |
3024 // | distribute |parallel for simd| * |
3025 // | parallel for simd| | |
3026 // | distribute |parallel sections| * |
3027 // | parallel for simd| | |
3028 // | distribute | task | * |
3029 // | parallel for simd| | |
3030 // | distribute | taskyield | * |
3031 // | parallel for simd| | |
3032 // | distribute | barrier | * |
3033 // | parallel for simd| | |
3034 // | distribute | taskwait | * |
3035 // | parallel for simd| | |
3036 // | distribute | taskgroup | * |
3037 // | parallel for simd| | |
3038 // | distribute | flush | * |
3039 // | parallel for simd| | |
3040 // | distribute | ordered | + |
3041 // | parallel for simd| | |
3042 // | distribute | atomic | * |
3043 // | parallel for simd| | |
3044 // | distribute | target | |
3045 // | parallel for simd| | |
3046 // | distribute | target parallel | |
3047 // | parallel for simd| | |
3048 // | distribute | target parallel | |
3049 // | parallel for simd| for | |
3050 // | distribute | target enter | |
3051 // | parallel for simd| data | |
3052 // | distribute | target exit | |
3053 // | parallel for simd| data | |
3054 // | distribute | teams | |
3055 // | parallel for simd| | |
3056 // | distribute | cancellation | + |
3057 // | parallel for simd| point | |
3058 // | distribute | cancel | + |
3059 // | parallel for simd| | |
3060 // | distribute | taskloop | * |
3061 // | parallel for simd| | |
3062 // | distribute | taskloop simd | * |
3063 // | parallel for simd| | |
3064 // | distribute | distribute | |
3065 // | parallel for simd| | |
3066 // | distribute | distribute | * |
3067 // | parallel for simd| parallel for | |
3068 // | distribute | distribute | * |
3069 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003070 // | distribute | distribute simd | * |
3071 // | parallel for simd| | |
3072 // +------------------+-----------------+------------------------------------+
3073 // | distribute simd | parallel | * |
3074 // | distribute simd | for | * |
3075 // | distribute simd | for simd | * |
3076 // | distribute simd | master | * |
3077 // | distribute simd | critical | * |
3078 // | distribute simd | simd | * |
3079 // | distribute simd | sections | * |
3080 // | distribute simd | section | * |
3081 // | distribute simd | single | * |
3082 // | distribute simd | parallel for | * |
3083 // | distribute simd |parallel for simd| * |
3084 // | distribute simd |parallel sections| * |
3085 // | distribute simd | task | * |
3086 // | distribute simd | taskyield | * |
3087 // | distribute simd | barrier | * |
3088 // | distribute simd | taskwait | * |
3089 // | distribute simd | taskgroup | * |
3090 // | distribute simd | flush | * |
3091 // | distribute simd | ordered | + |
3092 // | distribute simd | atomic | * |
3093 // | distribute simd | target | * |
3094 // | distribute simd | target parallel | * |
3095 // | distribute simd | target parallel | * |
3096 // | | for | |
3097 // | distribute simd | target enter | * |
3098 // | | data | |
3099 // | distribute simd | target exit | * |
3100 // | | data | |
3101 // | distribute simd | teams | * |
3102 // | distribute simd | cancellation | + |
3103 // | | point | |
3104 // | distribute simd | cancel | + |
3105 // | distribute simd | taskloop | * |
3106 // | distribute simd | taskloop simd | * |
3107 // | distribute simd | distribute | |
3108 // | distribute simd | distribute | * |
3109 // | | parallel for | |
3110 // | distribute simd | distribute | * |
3111 // | |parallel for simd| |
3112 // | distribute simd | distribute simd | * |
3113 // | | | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003114 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003115 if (Stack->getCurScope()) {
3116 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003117 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003118 bool NestingProhibited = false;
3119 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003120 enum {
3121 NoRecommend,
3122 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003123 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003124 ShouldBeInTargetRegion,
3125 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003126 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003127 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003128 // OpenMP [2.16, Nesting of Regions]
3129 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003130 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003131 // An ordered construct with the simd clause is the only OpenMP
3132 // construct that can appear in the simd region.
3133 // Allowing a SIMD consruct nested in another SIMD construct is an
3134 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3135 // message.
3136 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3137 ? diag::err_omp_prohibited_region_simd
3138 : diag::warn_omp_nesting_simd);
3139 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003140 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003141 if (ParentRegion == OMPD_atomic) {
3142 // OpenMP [2.16, Nesting of Regions]
3143 // OpenMP constructs may not be nested inside an atomic region.
3144 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3145 return true;
3146 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003147 if (CurrentRegion == OMPD_section) {
3148 // OpenMP [2.7.2, sections Construct, Restrictions]
3149 // Orphaned section directives are prohibited. That is, the section
3150 // directives must appear within the sections construct and must not be
3151 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003152 if (ParentRegion != OMPD_sections &&
3153 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003154 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3155 << (ParentRegion != OMPD_unknown)
3156 << getOpenMPDirectiveName(ParentRegion);
3157 return true;
3158 }
3159 return false;
3160 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003161 // Allow some constructs to be orphaned (they could be used in functions,
3162 // called from OpenMP regions with the required preconditions).
3163 if (ParentRegion == OMPD_unknown)
3164 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003165 if (CurrentRegion == OMPD_cancellation_point ||
3166 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003167 // OpenMP [2.16, Nesting of Regions]
3168 // A cancellation point construct for which construct-type-clause is
3169 // taskgroup must be nested inside a task construct. A cancellation
3170 // point construct for which construct-type-clause is not taskgroup must
3171 // be closely nested inside an OpenMP construct that matches the type
3172 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003173 // A cancel construct for which construct-type-clause is taskgroup must be
3174 // nested inside a task construct. A cancel construct for which
3175 // construct-type-clause is not taskgroup must be closely nested inside an
3176 // OpenMP construct that matches the type specified in
3177 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003178 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003179 !((CancelRegion == OMPD_parallel &&
3180 (ParentRegion == OMPD_parallel ||
3181 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003182 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003183 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3184 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003185 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3186 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003187 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3188 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003189 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003190 // OpenMP [2.16, Nesting of Regions]
3191 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003192 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003193 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003194 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003195 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3196 // OpenMP [2.16, Nesting of Regions]
3197 // A critical region may not be nested (closely or otherwise) inside a
3198 // critical region with the same name. Note that this restriction is not
3199 // sufficient to prevent deadlock.
3200 SourceLocation PreviousCriticalLoc;
3201 bool DeadLock =
3202 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3203 OpenMPDirectiveKind K,
3204 const DeclarationNameInfo &DNI,
3205 SourceLocation Loc)
3206 ->bool {
3207 if (K == OMPD_critical &&
3208 DNI.getName() == CurrentName.getName()) {
3209 PreviousCriticalLoc = Loc;
3210 return true;
3211 } else
3212 return false;
3213 },
3214 false /* skip top directive */);
3215 if (DeadLock) {
3216 SemaRef.Diag(StartLoc,
3217 diag::err_omp_prohibited_region_critical_same_name)
3218 << CurrentName.getName();
3219 if (PreviousCriticalLoc.isValid())
3220 SemaRef.Diag(PreviousCriticalLoc,
3221 diag::note_omp_previous_critical_region);
3222 return true;
3223 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003224 } else if (CurrentRegion == OMPD_barrier) {
3225 // OpenMP [2.16, Nesting of Regions]
3226 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003227 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003228 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3229 isOpenMPTaskingDirective(ParentRegion) ||
3230 ParentRegion == OMPD_master ||
3231 ParentRegion == OMPD_critical ||
3232 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003233 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003234 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003235 // OpenMP [2.16, Nesting of Regions]
3236 // A worksharing region may not be closely nested inside a worksharing,
3237 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003238 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3239 isOpenMPTaskingDirective(ParentRegion) ||
3240 ParentRegion == OMPD_master ||
3241 ParentRegion == OMPD_critical ||
3242 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003243 Recommend = ShouldBeInParallelRegion;
3244 } else if (CurrentRegion == OMPD_ordered) {
3245 // OpenMP [2.16, Nesting of Regions]
3246 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003247 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003248 // An ordered region must be closely nested inside a loop region (or
3249 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003250 // OpenMP [2.8.1,simd Construct, Restrictions]
3251 // An ordered construct with the simd clause is the only OpenMP construct
3252 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003253 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003254 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003255 !(isOpenMPSimdDirective(ParentRegion) ||
3256 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003257 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003258 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3259 // OpenMP [2.16, Nesting of Regions]
3260 // If specified, a teams construct must be contained within a target
3261 // construct.
3262 NestingProhibited = ParentRegion != OMPD_target;
3263 Recommend = ShouldBeInTargetRegion;
3264 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3265 }
3266 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3267 // OpenMP [2.16, Nesting of Regions]
3268 // distribute, parallel, parallel sections, parallel workshare, and the
3269 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3270 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003271 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3272 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003273 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003274 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003275 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3276 // OpenMP 4.5 [2.17 Nesting of Regions]
3277 // The region associated with the distribute construct must be strictly
3278 // nested inside a teams region
3279 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3280 Recommend = ShouldBeInTeamsRegion;
3281 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003282 if (!NestingProhibited &&
3283 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3284 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3285 // OpenMP 4.5 [2.17 Nesting of Regions]
3286 // If a target, target update, target data, target enter data, or
3287 // target exit data construct is encountered during execution of a
3288 // target region, the behavior is unspecified.
3289 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003290 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3291 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003292 if (isOpenMPTargetExecutionDirective(K)) {
3293 OffendingRegion = K;
3294 return true;
3295 } else
3296 return false;
3297 },
3298 false /* don't skip top directive */);
3299 CloseNesting = false;
3300 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003301 if (NestingProhibited) {
3302 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003303 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3304 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00003305 return true;
3306 }
3307 }
3308 return false;
3309}
3310
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003311static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3312 ArrayRef<OMPClause *> Clauses,
3313 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3314 bool ErrorFound = false;
3315 unsigned NamedModifiersNumber = 0;
3316 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3317 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003318 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003319 for (const auto *C : Clauses) {
3320 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3321 // At most one if clause without a directive-name-modifier can appear on
3322 // the directive.
3323 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3324 if (FoundNameModifiers[CurNM]) {
3325 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3326 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3327 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3328 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003329 } else if (CurNM != OMPD_unknown) {
3330 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003331 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003332 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003333 FoundNameModifiers[CurNM] = IC;
3334 if (CurNM == OMPD_unknown)
3335 continue;
3336 // Check if the specified name modifier is allowed for the current
3337 // directive.
3338 // At most one if clause with the particular directive-name-modifier can
3339 // appear on the directive.
3340 bool MatchFound = false;
3341 for (auto NM : AllowedNameModifiers) {
3342 if (CurNM == NM) {
3343 MatchFound = true;
3344 break;
3345 }
3346 }
3347 if (!MatchFound) {
3348 S.Diag(IC->getNameModifierLoc(),
3349 diag::err_omp_wrong_if_directive_name_modifier)
3350 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3351 ErrorFound = true;
3352 }
3353 }
3354 }
3355 // If any if clause on the directive includes a directive-name-modifier then
3356 // all if clauses on the directive must include a directive-name-modifier.
3357 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3358 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3359 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3360 diag::err_omp_no_more_if_clause);
3361 } else {
3362 std::string Values;
3363 std::string Sep(", ");
3364 unsigned AllowedCnt = 0;
3365 unsigned TotalAllowedNum =
3366 AllowedNameModifiers.size() - NamedModifiersNumber;
3367 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3368 ++Cnt) {
3369 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3370 if (!FoundNameModifiers[NM]) {
3371 Values += "'";
3372 Values += getOpenMPDirectiveName(NM);
3373 Values += "'";
3374 if (AllowedCnt + 2 == TotalAllowedNum)
3375 Values += " or ";
3376 else if (AllowedCnt + 1 != TotalAllowedNum)
3377 Values += Sep;
3378 ++AllowedCnt;
3379 }
3380 }
3381 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3382 diag::err_omp_unnamed_if_clause)
3383 << (TotalAllowedNum > 1) << Values;
3384 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003385 for (auto Loc : NameModifierLoc) {
3386 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3387 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003388 ErrorFound = true;
3389 }
3390 return ErrorFound;
3391}
3392
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003393StmtResult Sema::ActOnOpenMPExecutableDirective(
3394 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3395 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3396 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003397 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003398 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3399 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003400 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003401
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003402 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003403 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003404 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003405 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003406 if (AStmt) {
3407 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3408
3409 // Check default data sharing attributes for referenced variables.
3410 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3411 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3412 if (DSAChecker.isErrorFound())
3413 return StmtError();
3414 // Generate list of implicitly defined firstprivate variables.
3415 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003416
3417 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3418 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3419 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3420 SourceLocation(), SourceLocation())) {
3421 ClausesWithImplicit.push_back(Implicit);
3422 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3423 DSAChecker.getImplicitFirstprivate().size();
3424 } else
3425 ErrorFound = true;
3426 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003427 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003428
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003429 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003430 switch (Kind) {
3431 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003432 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3433 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003434 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003435 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003436 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003437 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3438 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003439 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003440 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003441 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3442 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003443 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003444 case OMPD_for_simd:
3445 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3446 EndLoc, VarsWithInheritedDSA);
3447 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003448 case OMPD_sections:
3449 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3450 EndLoc);
3451 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003452 case OMPD_section:
3453 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003454 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003455 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3456 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003457 case OMPD_single:
3458 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3459 EndLoc);
3460 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003461 case OMPD_master:
3462 assert(ClausesWithImplicit.empty() &&
3463 "No clauses are allowed for 'omp master' directive");
3464 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3465 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003466 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003467 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3468 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003469 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003470 case OMPD_parallel_for:
3471 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3472 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003473 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003474 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003475 case OMPD_parallel_for_simd:
3476 Res = ActOnOpenMPParallelForSimdDirective(
3477 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003478 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003479 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003480 case OMPD_parallel_sections:
3481 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3482 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003483 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003484 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003485 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003486 Res =
3487 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003488 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003489 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003490 case OMPD_taskyield:
3491 assert(ClausesWithImplicit.empty() &&
3492 "No clauses are allowed for 'omp taskyield' directive");
3493 assert(AStmt == nullptr &&
3494 "No associated statement allowed for 'omp taskyield' directive");
3495 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3496 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003497 case OMPD_barrier:
3498 assert(ClausesWithImplicit.empty() &&
3499 "No clauses are allowed for 'omp barrier' directive");
3500 assert(AStmt == nullptr &&
3501 "No associated statement allowed for 'omp barrier' directive");
3502 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3503 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003504 case OMPD_taskwait:
3505 assert(ClausesWithImplicit.empty() &&
3506 "No clauses are allowed for 'omp taskwait' directive");
3507 assert(AStmt == nullptr &&
3508 "No associated statement allowed for 'omp taskwait' directive");
3509 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3510 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003511 case OMPD_taskgroup:
3512 assert(ClausesWithImplicit.empty() &&
3513 "No clauses are allowed for 'omp taskgroup' directive");
3514 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3515 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003516 case OMPD_flush:
3517 assert(AStmt == nullptr &&
3518 "No associated statement allowed for 'omp flush' directive");
3519 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3520 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003521 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003522 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3523 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003524 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003525 case OMPD_atomic:
3526 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3527 EndLoc);
3528 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003529 case OMPD_teams:
3530 Res =
3531 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3532 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003533 case OMPD_target:
3534 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3535 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003536 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003537 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003538 case OMPD_target_parallel:
3539 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3540 StartLoc, EndLoc);
3541 AllowedNameModifiers.push_back(OMPD_target);
3542 AllowedNameModifiers.push_back(OMPD_parallel);
3543 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003544 case OMPD_target_parallel_for:
3545 Res = ActOnOpenMPTargetParallelForDirective(
3546 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3547 AllowedNameModifiers.push_back(OMPD_target);
3548 AllowedNameModifiers.push_back(OMPD_parallel);
3549 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003550 case OMPD_cancellation_point:
3551 assert(ClausesWithImplicit.empty() &&
3552 "No clauses are allowed for 'omp cancellation point' directive");
3553 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3554 "cancellation point' directive");
3555 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3556 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003557 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003558 assert(AStmt == nullptr &&
3559 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003560 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3561 CancelRegion);
3562 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003563 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003564 case OMPD_target_data:
3565 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3566 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003567 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003568 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003569 case OMPD_target_enter_data:
3570 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3571 EndLoc);
3572 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3573 break;
Samuel Antao72590762016-01-19 20:04:50 +00003574 case OMPD_target_exit_data:
3575 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3576 EndLoc);
3577 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3578 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003579 case OMPD_taskloop:
3580 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3581 EndLoc, VarsWithInheritedDSA);
3582 AllowedNameModifiers.push_back(OMPD_taskloop);
3583 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003584 case OMPD_taskloop_simd:
3585 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3586 EndLoc, VarsWithInheritedDSA);
3587 AllowedNameModifiers.push_back(OMPD_taskloop);
3588 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003589 case OMPD_distribute:
3590 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3591 EndLoc, VarsWithInheritedDSA);
3592 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003593 case OMPD_target_update:
3594 assert(!AStmt && "Statement is not allowed for target update");
3595 Res =
3596 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3597 AllowedNameModifiers.push_back(OMPD_target_update);
3598 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003599 case OMPD_distribute_parallel_for:
3600 Res = ActOnOpenMPDistributeParallelForDirective(
3601 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3602 AllowedNameModifiers.push_back(OMPD_parallel);
3603 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003604 case OMPD_distribute_parallel_for_simd:
3605 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3606 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3607 AllowedNameModifiers.push_back(OMPD_parallel);
3608 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003609 case OMPD_distribute_simd:
3610 Res = ActOnOpenMPDistributeSimdDirective(
3611 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3612 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003613 case OMPD_declare_target:
3614 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003615 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003616 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003617 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003618 llvm_unreachable("OpenMP Directive is not allowed");
3619 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003620 llvm_unreachable("Unknown OpenMP directive");
3621 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003622
Alexey Bataev4acb8592014-07-07 13:01:15 +00003623 for (auto P : VarsWithInheritedDSA) {
3624 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3625 << P.first << P.second->getSourceRange();
3626 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003627 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3628
3629 if (!AllowedNameModifiers.empty())
3630 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3631 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003632
Alexey Bataeved09d242014-05-28 05:53:51 +00003633 if (ErrorFound)
3634 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003635 return Res;
3636}
3637
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003638Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3639 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003640 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003641 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3642 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003643 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003644 assert(Linears.size() == LinModifiers.size());
3645 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003646 if (!DG || DG.get().isNull())
3647 return DeclGroupPtrTy();
3648
3649 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003650 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003651 return DG;
3652 }
3653 auto *ADecl = DG.get().getSingleDecl();
3654 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3655 ADecl = FTD->getTemplatedDecl();
3656
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003657 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3658 if (!FD) {
3659 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003660 return DeclGroupPtrTy();
3661 }
3662
Alexey Bataev2af33e32016-04-07 12:45:37 +00003663 // OpenMP [2.8.2, declare simd construct, Description]
3664 // The parameter of the simdlen clause must be a constant positive integer
3665 // expression.
3666 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003667 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003668 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003669 // OpenMP [2.8.2, declare simd construct, Description]
3670 // The special this pointer can be used as if was one of the arguments to the
3671 // function in any of the linear, aligned, or uniform clauses.
3672 // The uniform clause declares one or more arguments to have an invariant
3673 // value for all concurrent invocations of the function in the execution of a
3674 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003675 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3676 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003677 for (auto *E : Uniforms) {
3678 E = E->IgnoreParenImpCasts();
3679 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3680 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3681 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3682 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003683 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3684 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003685 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003686 }
3687 if (isa<CXXThisExpr>(E)) {
3688 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003689 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003690 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003691 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3692 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003693 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003694 // OpenMP [2.8.2, declare simd construct, Description]
3695 // The aligned clause declares that the object to which each list item points
3696 // is aligned to the number of bytes expressed in the optional parameter of
3697 // the aligned clause.
3698 // The special this pointer can be used as if was one of the arguments to the
3699 // function in any of the linear, aligned, or uniform clauses.
3700 // The type of list items appearing in the aligned clause must be array,
3701 // pointer, reference to array, or reference to pointer.
3702 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3703 Expr *AlignedThis = nullptr;
3704 for (auto *E : Aligneds) {
3705 E = E->IgnoreParenImpCasts();
3706 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3707 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3708 auto *CanonPVD = PVD->getCanonicalDecl();
3709 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3710 FD->getParamDecl(PVD->getFunctionScopeIndex())
3711 ->getCanonicalDecl() == CanonPVD) {
3712 // OpenMP [2.8.1, simd construct, Restrictions]
3713 // A list-item cannot appear in more than one aligned clause.
3714 if (AlignedArgs.count(CanonPVD) > 0) {
3715 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3716 << 1 << E->getSourceRange();
3717 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3718 diag::note_omp_explicit_dsa)
3719 << getOpenMPClauseName(OMPC_aligned);
3720 continue;
3721 }
3722 AlignedArgs[CanonPVD] = E;
3723 QualType QTy = PVD->getType()
3724 .getNonReferenceType()
3725 .getUnqualifiedType()
3726 .getCanonicalType();
3727 const Type *Ty = QTy.getTypePtrOrNull();
3728 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3729 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3730 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3731 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3732 }
3733 continue;
3734 }
3735 }
3736 if (isa<CXXThisExpr>(E)) {
3737 if (AlignedThis) {
3738 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3739 << 2 << E->getSourceRange();
3740 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3741 << getOpenMPClauseName(OMPC_aligned);
3742 }
3743 AlignedThis = E;
3744 continue;
3745 }
3746 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3747 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3748 }
3749 // The optional parameter of the aligned clause, alignment, must be a constant
3750 // positive integer expression. If no optional parameter is specified,
3751 // implementation-defined default alignments for SIMD instructions on the
3752 // target platforms are assumed.
3753 SmallVector<Expr *, 4> NewAligns;
3754 for (auto *E : Alignments) {
3755 ExprResult Align;
3756 if (E)
3757 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3758 NewAligns.push_back(Align.get());
3759 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003760 // OpenMP [2.8.2, declare simd construct, Description]
3761 // The linear clause declares one or more list items to be private to a SIMD
3762 // lane and to have a linear relationship with respect to the iteration space
3763 // of a loop.
3764 // The special this pointer can be used as if was one of the arguments to the
3765 // function in any of the linear, aligned, or uniform clauses.
3766 // When a linear-step expression is specified in a linear clause it must be
3767 // either a constant integer expression or an integer-typed parameter that is
3768 // specified in a uniform clause on the directive.
3769 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3770 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3771 auto MI = LinModifiers.begin();
3772 for (auto *E : Linears) {
3773 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3774 ++MI;
3775 E = E->IgnoreParenImpCasts();
3776 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3777 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3778 auto *CanonPVD = PVD->getCanonicalDecl();
3779 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3780 FD->getParamDecl(PVD->getFunctionScopeIndex())
3781 ->getCanonicalDecl() == CanonPVD) {
3782 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3783 // A list-item cannot appear in more than one linear clause.
3784 if (LinearArgs.count(CanonPVD) > 0) {
3785 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3786 << getOpenMPClauseName(OMPC_linear)
3787 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3788 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3789 diag::note_omp_explicit_dsa)
3790 << getOpenMPClauseName(OMPC_linear);
3791 continue;
3792 }
3793 // Each argument can appear in at most one uniform or linear clause.
3794 if (UniformedArgs.count(CanonPVD) > 0) {
3795 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3796 << getOpenMPClauseName(OMPC_linear)
3797 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3798 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3799 diag::note_omp_explicit_dsa)
3800 << getOpenMPClauseName(OMPC_uniform);
3801 continue;
3802 }
3803 LinearArgs[CanonPVD] = E;
3804 if (E->isValueDependent() || E->isTypeDependent() ||
3805 E->isInstantiationDependent() ||
3806 E->containsUnexpandedParameterPack())
3807 continue;
3808 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3809 PVD->getOriginalType());
3810 continue;
3811 }
3812 }
3813 if (isa<CXXThisExpr>(E)) {
3814 if (UniformedLinearThis) {
3815 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3816 << getOpenMPClauseName(OMPC_linear)
3817 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3818 << E->getSourceRange();
3819 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3820 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3821 : OMPC_linear);
3822 continue;
3823 }
3824 UniformedLinearThis = E;
3825 if (E->isValueDependent() || E->isTypeDependent() ||
3826 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3827 continue;
3828 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3829 E->getType());
3830 continue;
3831 }
3832 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3833 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3834 }
3835 Expr *Step = nullptr;
3836 Expr *NewStep = nullptr;
3837 SmallVector<Expr *, 4> NewSteps;
3838 for (auto *E : Steps) {
3839 // Skip the same step expression, it was checked already.
3840 if (Step == E || !E) {
3841 NewSteps.push_back(E ? NewStep : nullptr);
3842 continue;
3843 }
3844 Step = E;
3845 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3846 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3847 auto *CanonPVD = PVD->getCanonicalDecl();
3848 if (UniformedArgs.count(CanonPVD) == 0) {
3849 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3850 << Step->getSourceRange();
3851 } else if (E->isValueDependent() || E->isTypeDependent() ||
3852 E->isInstantiationDependent() ||
3853 E->containsUnexpandedParameterPack() ||
3854 CanonPVD->getType()->hasIntegerRepresentation())
3855 NewSteps.push_back(Step);
3856 else {
3857 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3858 << Step->getSourceRange();
3859 }
3860 continue;
3861 }
3862 NewStep = Step;
3863 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3864 !Step->isInstantiationDependent() &&
3865 !Step->containsUnexpandedParameterPack()) {
3866 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3867 .get();
3868 if (NewStep)
3869 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3870 }
3871 NewSteps.push_back(NewStep);
3872 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003873 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3874 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003875 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003876 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3877 const_cast<Expr **>(Linears.data()), Linears.size(),
3878 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3879 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003880 ADecl->addAttr(NewAttr);
3881 return ConvertDeclToDeclGroup(ADecl);
3882}
3883
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003884StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3885 Stmt *AStmt,
3886 SourceLocation StartLoc,
3887 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003888 if (!AStmt)
3889 return StmtError();
3890
Alexey Bataev9959db52014-05-06 10:08:46 +00003891 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3892 // 1.2.2 OpenMP Language Terminology
3893 // Structured block - An executable statement with a single entry at the
3894 // top and a single exit at the bottom.
3895 // The point of exit cannot be a branch out of the structured block.
3896 // longjmp() and throw() must not violate the entry/exit criteria.
3897 CS->getCapturedDecl()->setNothrow();
3898
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003899 getCurFunction()->setHasBranchProtectedScope();
3900
Alexey Bataev25e5b442015-09-15 12:52:43 +00003901 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3902 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003903}
3904
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003905namespace {
3906/// \brief Helper class for checking canonical form of the OpenMP loops and
3907/// extracting iteration space of each loop in the loop nest, that will be used
3908/// for IR generation.
3909class OpenMPIterationSpaceChecker {
3910 /// \brief Reference to Sema.
3911 Sema &SemaRef;
3912 /// \brief A location for diagnostics (when there is no some better location).
3913 SourceLocation DefaultLoc;
3914 /// \brief A location for diagnostics (when increment is not compatible).
3915 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003916 /// \brief A source location for referring to loop init later.
3917 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 /// \brief A source location for referring to condition later.
3919 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003920 /// \brief A source location for referring to increment later.
3921 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003922 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003923 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003924 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003925 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003926 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003927 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003928 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003929 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003931 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003932 /// \brief This flag is true when condition is one of:
3933 /// Var < UB
3934 /// Var <= UB
3935 /// UB > Var
3936 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003937 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003939 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003940 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003941 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003942
3943public:
3944 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003945 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003946 /// \brief Check init-expr for canonical loop form and save loop counter
3947 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003948 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003949 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3950 /// for less/greater and for strict/non-strict comparison.
3951 bool CheckCond(Expr *S);
3952 /// \brief Check incr-expr for canonical loop form and return true if it
3953 /// does not conform, otherwise save loop step (#Step).
3954 bool CheckInc(Expr *S);
3955 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003956 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003957 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003958 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003959 /// \brief Source range of the loop init.
3960 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3961 /// \brief Source range of the loop condition.
3962 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3963 /// \brief Source range of the loop increment.
3964 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3965 /// \brief True if the step should be subtracted.
3966 bool ShouldSubtractStep() const { return SubtractStep; }
3967 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003968 Expr *
3969 BuildNumIterations(Scope *S, const bool LimitedType,
3970 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003971 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003972 Expr *BuildPreCond(Scope *S, Expr *Cond,
3973 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003974 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003975 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3976 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003977 /// \brief Build reference expression to the private counter be used for
3978 /// codegen.
3979 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003980 /// \brief Build initization of the counter be used for codegen.
3981 Expr *BuildCounterInit() const;
3982 /// \brief Build step of the counter be used for codegen.
3983 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003984 /// \brief Return true if any expression is dependent.
3985 bool Dependent() const;
3986
3987private:
3988 /// \brief Check the right-hand side of an assignment in the increment
3989 /// expression.
3990 bool CheckIncRHS(Expr *RHS);
3991 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003992 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003993 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003994 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003995 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003996 /// \brief Helper to set loop increment.
3997 bool SetStep(Expr *NewStep, bool Subtract);
3998};
3999
4000bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004001 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004002 assert(!LB && !UB && !Step);
4003 return false;
4004 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004005 return LCDecl->getType()->isDependentType() ||
4006 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4007 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004008}
4009
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004010static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004011 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4012 E = ExprTemp->getSubExpr();
4013
4014 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4015 E = MTE->GetTemporaryExpr();
4016
4017 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4018 E = Binder->getSubExpr();
4019
4020 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4021 E = ICE->getSubExprAsWritten();
4022 return E->IgnoreParens();
4023}
4024
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004025bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4026 Expr *NewLCRefExpr,
4027 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004028 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004029 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004030 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004031 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004032 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004033 LCDecl = getCanonicalDecl(NewLCDecl);
4034 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004035 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4036 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004037 if ((Ctor->isCopyOrMoveConstructor() ||
4038 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4039 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004040 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004041 LB = NewLB;
4042 return false;
4043}
4044
4045bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004046 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004047 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004048 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4049 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004050 if (!NewUB)
4051 return true;
4052 UB = NewUB;
4053 TestIsLessOp = LessOp;
4054 TestIsStrictOp = StrictOp;
4055 ConditionSrcRange = SR;
4056 ConditionLoc = SL;
4057 return false;
4058}
4059
4060bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4061 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004062 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004063 if (!NewStep)
4064 return true;
4065 if (!NewStep->isValueDependent()) {
4066 // Check that the step is integer expression.
4067 SourceLocation StepLoc = NewStep->getLocStart();
4068 ExprResult Val =
4069 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4070 if (Val.isInvalid())
4071 return true;
4072 NewStep = Val.get();
4073
4074 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4075 // If test-expr is of form var relational-op b and relational-op is < or
4076 // <= then incr-expr must cause var to increase on each iteration of the
4077 // loop. If test-expr is of form var relational-op b and relational-op is
4078 // > or >= then incr-expr must cause var to decrease on each iteration of
4079 // the loop.
4080 // If test-expr is of form b relational-op var and relational-op is < or
4081 // <= then incr-expr must cause var to decrease on each iteration of the
4082 // loop. If test-expr is of form b relational-op var and relational-op is
4083 // > or >= then incr-expr must cause var to increase on each iteration of
4084 // the loop.
4085 llvm::APSInt Result;
4086 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4087 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4088 bool IsConstNeg =
4089 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004090 bool IsConstPos =
4091 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004092 bool IsConstZero = IsConstant && !Result.getBoolValue();
4093 if (UB && (IsConstZero ||
4094 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004095 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004096 SemaRef.Diag(NewStep->getExprLoc(),
4097 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004098 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004099 SemaRef.Diag(ConditionLoc,
4100 diag::note_omp_loop_cond_requres_compatible_incr)
4101 << TestIsLessOp << ConditionSrcRange;
4102 return true;
4103 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004104 if (TestIsLessOp == Subtract) {
4105 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4106 NewStep).get();
4107 Subtract = !Subtract;
4108 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004109 }
4110
4111 Step = NewStep;
4112 SubtractStep = Subtract;
4113 return false;
4114}
4115
Alexey Bataev9c821032015-04-30 04:23:23 +00004116bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004117 // Check init-expr for canonical loop form and save loop counter
4118 // variable - #Var and its initialization value - #LB.
4119 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4120 // var = lb
4121 // integer-type var = lb
4122 // random-access-iterator-type var = lb
4123 // pointer-type var = lb
4124 //
4125 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004126 if (EmitDiags) {
4127 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4128 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004129 return true;
4130 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004131 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4132 if (!ExprTemp->cleanupsHaveSideEffects())
4133 S = ExprTemp->getSubExpr();
4134
Alexander Musmana5f070a2014-10-01 06:03:56 +00004135 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004136 if (Expr *E = dyn_cast<Expr>(S))
4137 S = E->IgnoreParens();
4138 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004139 if (BO->getOpcode() == BO_Assign) {
4140 auto *LHS = BO->getLHS()->IgnoreParens();
4141 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4142 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4143 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4144 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4145 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4146 }
4147 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4148 if (ME->isArrow() &&
4149 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4150 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4151 }
4152 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004153 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4154 if (DS->isSingleDecl()) {
4155 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004156 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004157 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004158 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004159 SemaRef.Diag(S->getLocStart(),
4160 diag::ext_omp_loop_not_canonical_init)
4161 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004162 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004163 }
4164 }
4165 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004166 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4167 if (CE->getOperator() == OO_Equal) {
4168 auto *LHS = CE->getArg(0);
4169 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4170 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4171 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4172 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4173 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4174 }
4175 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4176 if (ME->isArrow() &&
4177 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4178 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4179 }
4180 }
4181 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004182
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004183 if (Dependent() || SemaRef.CurContext->isDependentContext())
4184 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004185 if (EmitDiags) {
4186 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4187 << S->getSourceRange();
4188 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004189 return true;
4190}
4191
Alexey Bataev23b69422014-06-18 07:08:49 +00004192/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004193/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004194static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004196 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004197 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004198 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4199 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004200 if ((Ctor->isCopyOrMoveConstructor() ||
4201 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4202 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004203 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004204 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4205 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4206 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4207 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4208 return getCanonicalDecl(ME->getMemberDecl());
4209 return getCanonicalDecl(VD);
4210 }
4211 }
4212 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4213 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4214 return getCanonicalDecl(ME->getMemberDecl());
4215 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004216}
4217
4218bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4219 // Check test-expr for canonical form, save upper-bound UB, flags for
4220 // less/greater and for strict/non-strict comparison.
4221 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4222 // var relational-op b
4223 // b relational-op var
4224 //
4225 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004226 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004227 return true;
4228 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004229 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004230 SourceLocation CondLoc = S->getLocStart();
4231 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4232 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004233 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004234 return SetUB(BO->getRHS(),
4235 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4236 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4237 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004238 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004239 return SetUB(BO->getLHS(),
4240 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4241 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4242 BO->getSourceRange(), BO->getOperatorLoc());
4243 }
4244 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4245 if (CE->getNumArgs() == 2) {
4246 auto Op = CE->getOperator();
4247 switch (Op) {
4248 case OO_Greater:
4249 case OO_GreaterEqual:
4250 case OO_Less:
4251 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004252 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004253 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4254 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4255 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004256 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004257 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4258 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4259 CE->getOperatorLoc());
4260 break;
4261 default:
4262 break;
4263 }
4264 }
4265 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004266 if (Dependent() || SemaRef.CurContext->isDependentContext())
4267 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004268 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004269 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004270 return true;
4271}
4272
4273bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4274 // RHS of canonical loop form increment can be:
4275 // var + incr
4276 // incr + var
4277 // var - incr
4278 //
4279 RHS = RHS->IgnoreParenImpCasts();
4280 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4281 if (BO->isAdditiveOp()) {
4282 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004283 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004284 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004285 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004286 return SetStep(BO->getLHS(), false);
4287 }
4288 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4289 bool IsAdd = CE->getOperator() == OO_Plus;
4290 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004291 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004292 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004293 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004294 return SetStep(CE->getArg(0), false);
4295 }
4296 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004297 if (Dependent() || SemaRef.CurContext->isDependentContext())
4298 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004299 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004300 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004301 return true;
4302}
4303
4304bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4305 // Check incr-expr for canonical loop form and return true if it
4306 // does not conform.
4307 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4308 // ++var
4309 // var++
4310 // --var
4311 // var--
4312 // var += incr
4313 // var -= incr
4314 // var = var + incr
4315 // var = incr + var
4316 // var = var - incr
4317 //
4318 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004319 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004320 return true;
4321 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004322 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4323 if (!ExprTemp->cleanupsHaveSideEffects())
4324 S = ExprTemp->getSubExpr();
4325
Alexander Musmana5f070a2014-10-01 06:03:56 +00004326 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004327 S = S->IgnoreParens();
4328 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004329 if (UO->isIncrementDecrementOp() &&
4330 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004331 return SetStep(
4332 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4333 (UO->isDecrementOp() ? -1 : 1)).get(),
4334 false);
4335 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4336 switch (BO->getOpcode()) {
4337 case BO_AddAssign:
4338 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004339 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004340 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4341 break;
4342 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004343 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004344 return CheckIncRHS(BO->getRHS());
4345 break;
4346 default:
4347 break;
4348 }
4349 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4350 switch (CE->getOperator()) {
4351 case OO_PlusPlus:
4352 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004353 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004354 return SetStep(
4355 SemaRef.ActOnIntegerConstant(
4356 CE->getLocStart(),
4357 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4358 false);
4359 break;
4360 case OO_PlusEqual:
4361 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004362 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004363 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4364 break;
4365 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004366 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004367 return CheckIncRHS(CE->getArg(1));
4368 break;
4369 default:
4370 break;
4371 }
4372 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004373 if (Dependent() || SemaRef.CurContext->isDependentContext())
4374 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004375 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004376 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004377 return true;
4378}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004379
Alexey Bataev5a3af132016-03-29 08:58:54 +00004380static ExprResult
4381tryBuildCapture(Sema &SemaRef, Expr *Capture,
4382 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004383 if (SemaRef.CurContext->isDependentContext())
4384 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004385 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4386 return SemaRef.PerformImplicitConversion(
4387 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4388 /*AllowExplicit=*/true);
4389 auto I = Captures.find(Capture);
4390 if (I != Captures.end())
4391 return buildCapture(SemaRef, Capture, I->second);
4392 DeclRefExpr *Ref = nullptr;
4393 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4394 Captures[Capture] = Ref;
4395 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004396}
4397
Alexander Musmana5f070a2014-10-01 06:03:56 +00004398/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004399Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4400 Scope *S, const bool LimitedType,
4401 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004402 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004403 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004404 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004405 SemaRef.getLangOpts().CPlusPlus) {
4406 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004407 auto *UBExpr = TestIsLessOp ? UB : LB;
4408 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004409 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4410 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004411 if (!Upper || !Lower)
4412 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004413
4414 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4415
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004416 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004417 // BuildBinOp already emitted error, this one is to point user to upper
4418 // and lower bound, and to tell what is passed to 'operator-'.
4419 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4420 << Upper->getSourceRange() << Lower->getSourceRange();
4421 return nullptr;
4422 }
4423 }
4424
4425 if (!Diff.isUsable())
4426 return nullptr;
4427
4428 // Upper - Lower [- 1]
4429 if (TestIsStrictOp)
4430 Diff = SemaRef.BuildBinOp(
4431 S, DefaultLoc, BO_Sub, Diff.get(),
4432 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4433 if (!Diff.isUsable())
4434 return nullptr;
4435
4436 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004437 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4438 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004439 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004440 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004441 if (!Diff.isUsable())
4442 return nullptr;
4443
4444 // Parentheses (for dumping/debugging purposes only).
4445 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4446 if (!Diff.isUsable())
4447 return nullptr;
4448
4449 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004450 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004451 if (!Diff.isUsable())
4452 return nullptr;
4453
Alexander Musman174b3ca2014-10-06 11:16:29 +00004454 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004455 QualType Type = Diff.get()->getType();
4456 auto &C = SemaRef.Context;
4457 bool UseVarType = VarType->hasIntegerRepresentation() &&
4458 C.getTypeSize(Type) > C.getTypeSize(VarType);
4459 if (!Type->isIntegerType() || UseVarType) {
4460 unsigned NewSize =
4461 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4462 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4463 : Type->hasSignedIntegerRepresentation();
4464 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004465 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4466 Diff = SemaRef.PerformImplicitConversion(
4467 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4468 if (!Diff.isUsable())
4469 return nullptr;
4470 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004471 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004472 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004473 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4474 if (NewSize != C.getTypeSize(Type)) {
4475 if (NewSize < C.getTypeSize(Type)) {
4476 assert(NewSize == 64 && "incorrect loop var size");
4477 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4478 << InitSrcRange << ConditionSrcRange;
4479 }
4480 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004481 NewSize, Type->hasSignedIntegerRepresentation() ||
4482 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004483 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4484 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4485 Sema::AA_Converting, true);
4486 if (!Diff.isUsable())
4487 return nullptr;
4488 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004489 }
4490 }
4491
Alexander Musmana5f070a2014-10-01 06:03:56 +00004492 return Diff.get();
4493}
4494
Alexey Bataev5a3af132016-03-29 08:58:54 +00004495Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4496 Scope *S, Expr *Cond,
4497 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004498 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4499 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4500 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004501
Alexey Bataev5a3af132016-03-29 08:58:54 +00004502 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4503 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4504 if (!NewLB.isUsable() || !NewUB.isUsable())
4505 return nullptr;
4506
Alexey Bataev62dbb972015-04-22 11:59:37 +00004507 auto CondExpr = SemaRef.BuildBinOp(
4508 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4509 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004510 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004511 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004512 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4513 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004514 CondExpr = SemaRef.PerformImplicitConversion(
4515 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4516 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004517 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004518 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4519 // Otherwise use original loop conditon and evaluate it in runtime.
4520 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4521}
4522
Alexander Musmana5f070a2014-10-01 06:03:56 +00004523/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004524DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004525 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004526 auto *VD = dyn_cast<VarDecl>(LCDecl);
4527 if (!VD) {
4528 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4529 auto *Ref = buildDeclRefExpr(
4530 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004531 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4532 // If the loop control decl is explicitly marked as private, do not mark it
4533 // as captured again.
4534 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4535 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004536 return Ref;
4537 }
4538 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004539 DefaultLoc);
4540}
4541
4542Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004543 if (LCDecl && !LCDecl->isInvalidDecl()) {
4544 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004545 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004546 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4547 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004548 if (PrivateVar->isInvalidDecl())
4549 return nullptr;
4550 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4551 }
4552 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004553}
4554
4555/// \brief Build initization of the counter be used for codegen.
4556Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4557
4558/// \brief Build step of the counter be used for codegen.
4559Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4560
4561/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004562struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004563 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004564 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004565 /// \brief This expression calculates the number of iterations in the loop.
4566 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004567 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004568 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004569 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004570 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004571 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004572 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004573 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004574 /// \brief This is step for the #CounterVar used to generate its update:
4575 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004576 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004577 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004578 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004579 /// \brief Source range of the loop init.
4580 SourceRange InitSrcRange;
4581 /// \brief Source range of the loop condition.
4582 SourceRange CondSrcRange;
4583 /// \brief Source range of the loop increment.
4584 SourceRange IncSrcRange;
4585};
4586
Alexey Bataev23b69422014-06-18 07:08:49 +00004587} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004588
Alexey Bataev9c821032015-04-30 04:23:23 +00004589void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4590 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4591 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004592 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4593 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004594 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4595 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004596 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4597 if (auto *D = ISC.GetLoopDecl()) {
4598 auto *VD = dyn_cast<VarDecl>(D);
4599 if (!VD) {
4600 if (auto *Private = IsOpenMPCapturedDecl(D))
4601 VD = Private;
4602 else {
4603 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4604 /*WithInit=*/false);
4605 VD = cast<VarDecl>(Ref->getDecl());
4606 }
4607 }
4608 DSAStack->addLoopControlVariable(D, VD);
4609 }
4610 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004611 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004612 }
4613}
4614
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004615/// \brief Called on a for stmt to check and extract its iteration space
4616/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004617static bool CheckOpenMPIterationSpace(
4618 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4619 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004620 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004621 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004622 LoopIterationSpace &ResultIterSpace,
4623 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004624 // OpenMP [2.6, Canonical Loop Form]
4625 // for (init-expr; test-expr; incr-expr) structured-block
4626 auto For = dyn_cast_or_null<ForStmt>(S);
4627 if (!For) {
4628 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004629 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4630 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4631 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4632 if (NestedLoopCount > 1) {
4633 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4634 SemaRef.Diag(DSA.getConstructLoc(),
4635 diag::note_omp_collapse_ordered_expr)
4636 << 2 << CollapseLoopCountExpr->getSourceRange()
4637 << OrderedLoopCountExpr->getSourceRange();
4638 else if (CollapseLoopCountExpr)
4639 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4640 diag::note_omp_collapse_ordered_expr)
4641 << 0 << CollapseLoopCountExpr->getSourceRange();
4642 else
4643 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4644 diag::note_omp_collapse_ordered_expr)
4645 << 1 << OrderedLoopCountExpr->getSourceRange();
4646 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004647 return true;
4648 }
4649 assert(For->getBody());
4650
4651 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4652
4653 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004654 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004655 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004656 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004657
4658 bool HasErrors = false;
4659
4660 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004661 if (auto *LCDecl = ISC.GetLoopDecl()) {
4662 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004663
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004664 // OpenMP [2.6, Canonical Loop Form]
4665 // Var is one of the following:
4666 // A variable of signed or unsigned integer type.
4667 // For C++, a variable of a random access iterator type.
4668 // For C, a variable of a pointer type.
4669 auto VarType = LCDecl->getType().getNonReferenceType();
4670 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4671 !VarType->isPointerType() &&
4672 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4673 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4674 << SemaRef.getLangOpts().CPlusPlus;
4675 HasErrors = true;
4676 }
4677
4678 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4679 // a Construct
4680 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4681 // parallel for construct is (are) private.
4682 // The loop iteration variable in the associated for-loop of a simd
4683 // construct with just one associated for-loop is linear with a
4684 // constant-linear-step that is the increment of the associated for-loop.
4685 // Exclude loop var from the list of variables with implicitly defined data
4686 // sharing attributes.
4687 VarsWithImplicitDSA.erase(LCDecl);
4688
4689 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4690 // in a Construct, C/C++].
4691 // The loop iteration variable in the associated for-loop of a simd
4692 // construct with just one associated for-loop may be listed in a linear
4693 // clause with a constant-linear-step that is the increment of the
4694 // associated for-loop.
4695 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4696 // parallel for construct may be listed in a private or lastprivate clause.
4697 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4698 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4699 // declared in the loop and it is predetermined as a private.
4700 auto PredeterminedCKind =
4701 isOpenMPSimdDirective(DKind)
4702 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4703 : OMPC_private;
4704 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4705 DVar.CKind != PredeterminedCKind) ||
4706 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4707 isOpenMPDistributeDirective(DKind)) &&
4708 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4709 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4710 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4711 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4712 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4713 << getOpenMPClauseName(PredeterminedCKind);
4714 if (DVar.RefExpr == nullptr)
4715 DVar.CKind = PredeterminedCKind;
4716 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4717 HasErrors = true;
4718 } else if (LoopDeclRefExpr != nullptr) {
4719 // Make the loop iteration variable private (for worksharing constructs),
4720 // linear (for simd directives with the only one associated loop) or
4721 // lastprivate (for simd directives with several collapsed or ordered
4722 // loops).
4723 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004724 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4725 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004726 /*FromParent=*/false);
4727 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4728 }
4729
4730 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4731
4732 // Check test-expr.
4733 HasErrors |= ISC.CheckCond(For->getCond());
4734
4735 // Check incr-expr.
4736 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004737 }
4738
Alexander Musmana5f070a2014-10-01 06:03:56 +00004739 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004740 return HasErrors;
4741
Alexander Musmana5f070a2014-10-01 06:03:56 +00004742 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004743 ResultIterSpace.PreCond =
4744 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004745 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004746 DSA.getCurScope(),
4747 (isOpenMPWorksharingDirective(DKind) ||
4748 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4749 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004750 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004751 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004752 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4753 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4754 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4755 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4756 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4757 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4758
Alexey Bataev62dbb972015-04-22 11:59:37 +00004759 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4760 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004761 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004762 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004763 ResultIterSpace.CounterInit == nullptr ||
4764 ResultIterSpace.CounterStep == nullptr);
4765
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004766 return HasErrors;
4767}
4768
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004769/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004770static ExprResult
4771BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4772 ExprResult Start,
4773 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004774 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004775 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4776 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004777 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004778 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004779 VarRef.get()->getType())) {
4780 NewStart = SemaRef.PerformImplicitConversion(
4781 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4782 /*AllowExplicit=*/true);
4783 if (!NewStart.isUsable())
4784 return ExprError();
4785 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004786
4787 auto Init =
4788 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4789 return Init;
4790}
4791
Alexander Musmana5f070a2014-10-01 06:03:56 +00004792/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004793static ExprResult
4794BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4795 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4796 ExprResult Step, bool Subtract,
4797 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004798 // Add parentheses (for debugging purposes only).
4799 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4800 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4801 !Step.isUsable())
4802 return ExprError();
4803
Alexey Bataev5a3af132016-03-29 08:58:54 +00004804 ExprResult NewStep = Step;
4805 if (Captures)
4806 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004807 if (NewStep.isInvalid())
4808 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004809 ExprResult Update =
4810 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004811 if (!Update.isUsable())
4812 return ExprError();
4813
Alexey Bataevc0214e02016-02-16 12:13:49 +00004814 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4815 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004816 ExprResult NewStart = Start;
4817 if (Captures)
4818 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004819 if (NewStart.isInvalid())
4820 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004821
Alexey Bataevc0214e02016-02-16 12:13:49 +00004822 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4823 ExprResult SavedUpdate = Update;
4824 ExprResult UpdateVal;
4825 if (VarRef.get()->getType()->isOverloadableType() ||
4826 NewStart.get()->getType()->isOverloadableType() ||
4827 Update.get()->getType()->isOverloadableType()) {
4828 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4829 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4830 Update =
4831 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4832 if (Update.isUsable()) {
4833 UpdateVal =
4834 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4835 VarRef.get(), SavedUpdate.get());
4836 if (UpdateVal.isUsable()) {
4837 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4838 UpdateVal.get());
4839 }
4840 }
4841 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4842 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004843
Alexey Bataevc0214e02016-02-16 12:13:49 +00004844 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4845 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4846 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4847 NewStart.get(), SavedUpdate.get());
4848 if (!Update.isUsable())
4849 return ExprError();
4850
Alexey Bataev11481f52016-02-17 10:29:05 +00004851 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4852 VarRef.get()->getType())) {
4853 Update = SemaRef.PerformImplicitConversion(
4854 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4855 if (!Update.isUsable())
4856 return ExprError();
4857 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004858
4859 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4860 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004861 return Update;
4862}
4863
4864/// \brief Convert integer expression \a E to make it have at least \a Bits
4865/// bits.
4866static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4867 Sema &SemaRef) {
4868 if (E == nullptr)
4869 return ExprError();
4870 auto &C = SemaRef.Context;
4871 QualType OldType = E->getType();
4872 unsigned HasBits = C.getTypeSize(OldType);
4873 if (HasBits >= Bits)
4874 return ExprResult(E);
4875 // OK to convert to signed, because new type has more bits than old.
4876 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4877 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4878 true);
4879}
4880
4881/// \brief Check if the given expression \a E is a constant integer that fits
4882/// into \a Bits bits.
4883static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4884 if (E == nullptr)
4885 return false;
4886 llvm::APSInt Result;
4887 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4888 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4889 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004890}
4891
Alexey Bataev5a3af132016-03-29 08:58:54 +00004892/// Build preinits statement for the given declarations.
4893static Stmt *buildPreInits(ASTContext &Context,
4894 SmallVectorImpl<Decl *> &PreInits) {
4895 if (!PreInits.empty()) {
4896 return new (Context) DeclStmt(
4897 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4898 SourceLocation(), SourceLocation());
4899 }
4900 return nullptr;
4901}
4902
4903/// Build preinits statement for the given declarations.
4904static Stmt *buildPreInits(ASTContext &Context,
4905 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4906 if (!Captures.empty()) {
4907 SmallVector<Decl *, 16> PreInits;
4908 for (auto &Pair : Captures)
4909 PreInits.push_back(Pair.second->getDecl());
4910 return buildPreInits(Context, PreInits);
4911 }
4912 return nullptr;
4913}
4914
4915/// Build postupdate expression for the given list of postupdates expressions.
4916static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4917 Expr *PostUpdate = nullptr;
4918 if (!PostUpdates.empty()) {
4919 for (auto *E : PostUpdates) {
4920 Expr *ConvE = S.BuildCStyleCastExpr(
4921 E->getExprLoc(),
4922 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4923 E->getExprLoc(), E)
4924 .get();
4925 PostUpdate = PostUpdate
4926 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4927 PostUpdate, ConvE)
4928 .get()
4929 : ConvE;
4930 }
4931 }
4932 return PostUpdate;
4933}
4934
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004935/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004936/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4937/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004938static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004939CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4940 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4941 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004942 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004943 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004944 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004945 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004946 // Found 'collapse' clause - calculate collapse number.
4947 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004948 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004949 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004950 }
4951 if (OrderedLoopCountExpr) {
4952 // Found 'ordered' clause - calculate collapse number.
4953 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004954 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4955 if (Result.getLimitedValue() < NestedLoopCount) {
4956 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4957 diag::err_omp_wrong_ordered_loop_count)
4958 << OrderedLoopCountExpr->getSourceRange();
4959 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4960 diag::note_collapse_loop_count)
4961 << CollapseLoopCountExpr->getSourceRange();
4962 }
4963 NestedLoopCount = Result.getLimitedValue();
4964 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004965 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004966 // This is helper routine for loop directives (e.g., 'for', 'simd',
4967 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004968 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004969 SmallVector<LoopIterationSpace, 4> IterSpaces;
4970 IterSpaces.resize(NestedLoopCount);
4971 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004972 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004973 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004974 NestedLoopCount, CollapseLoopCountExpr,
4975 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004976 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004977 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004978 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004979 // OpenMP [2.8.1, simd construct, Restrictions]
4980 // All loops associated with the construct must be perfectly nested; that
4981 // is, there must be no intervening code nor any OpenMP directive between
4982 // any two loops.
4983 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004984 }
4985
Alexander Musmana5f070a2014-10-01 06:03:56 +00004986 Built.clear(/* size */ NestedLoopCount);
4987
4988 if (SemaRef.CurContext->isDependentContext())
4989 return NestedLoopCount;
4990
4991 // An example of what is generated for the following code:
4992 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004993 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004994 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004995 // for (k = 0; k < NK; ++k)
4996 // for (j = J0; j < NJ; j+=2) {
4997 // <loop body>
4998 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004999 //
5000 // We generate the code below.
5001 // Note: the loop body may be outlined in CodeGen.
5002 // Note: some counters may be C++ classes, operator- is used to find number of
5003 // iterations and operator+= to calculate counter value.
5004 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5005 // or i64 is currently supported).
5006 //
5007 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5008 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5009 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5010 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5011 // // similar updates for vars in clauses (e.g. 'linear')
5012 // <loop body (using local i and j)>
5013 // }
5014 // i = NI; // assign final values of counters
5015 // j = NJ;
5016 //
5017
5018 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5019 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005020 // Precondition tests if there is at least one iteration (all conditions are
5021 // true).
5022 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005023 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005024 ExprResult LastIteration32 = WidenIterationCount(
5025 32 /* Bits */, SemaRef.PerformImplicitConversion(
5026 N0->IgnoreImpCasts(), N0->getType(),
5027 Sema::AA_Converting, /*AllowExplicit=*/true)
5028 .get(),
5029 SemaRef);
5030 ExprResult LastIteration64 = WidenIterationCount(
5031 64 /* Bits */, SemaRef.PerformImplicitConversion(
5032 N0->IgnoreImpCasts(), N0->getType(),
5033 Sema::AA_Converting, /*AllowExplicit=*/true)
5034 .get(),
5035 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005036
5037 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5038 return NestedLoopCount;
5039
5040 auto &C = SemaRef.Context;
5041 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5042
5043 Scope *CurScope = DSA.getCurScope();
5044 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005045 if (PreCond.isUsable()) {
5046 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5047 PreCond.get(), IterSpaces[Cnt].PreCond);
5048 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005049 auto N = IterSpaces[Cnt].NumIterations;
5050 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5051 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005052 LastIteration32 = SemaRef.BuildBinOp(
5053 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5054 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5055 Sema::AA_Converting,
5056 /*AllowExplicit=*/true)
5057 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005058 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005059 LastIteration64 = SemaRef.BuildBinOp(
5060 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5061 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5062 Sema::AA_Converting,
5063 /*AllowExplicit=*/true)
5064 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005065 }
5066
5067 // Choose either the 32-bit or 64-bit version.
5068 ExprResult LastIteration = LastIteration64;
5069 if (LastIteration32.isUsable() &&
5070 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5071 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5072 FitsInto(
5073 32 /* Bits */,
5074 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5075 LastIteration64.get(), SemaRef)))
5076 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005077 QualType VType = LastIteration.get()->getType();
5078 QualType RealVType = VType;
5079 QualType StrideVType = VType;
5080 if (isOpenMPTaskLoopDirective(DKind)) {
5081 VType =
5082 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5083 StrideVType =
5084 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5085 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005086
5087 if (!LastIteration.isUsable())
5088 return 0;
5089
5090 // Save the number of iterations.
5091 ExprResult NumIterations = LastIteration;
5092 {
5093 LastIteration = SemaRef.BuildBinOp(
5094 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5095 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5096 if (!LastIteration.isUsable())
5097 return 0;
5098 }
5099
5100 // Calculate the last iteration number beforehand instead of doing this on
5101 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5102 llvm::APSInt Result;
5103 bool IsConstant =
5104 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5105 ExprResult CalcLastIteration;
5106 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005107 ExprResult SaveRef =
5108 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005109 LastIteration = SaveRef;
5110
5111 // Prepare SaveRef + 1.
5112 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005113 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005114 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5115 if (!NumIterations.isUsable())
5116 return 0;
5117 }
5118
5119 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5120
Alexander Musmanc6388682014-12-15 07:07:06 +00005121 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005122 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005123 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5124 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005125 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005126 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5127 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005128 SemaRef.AddInitializerToDecl(
5129 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5130 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5131
5132 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005133 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5134 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005135 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5136 /*DirectInit*/ false,
5137 /*TypeMayContainAuto*/ false);
5138
5139 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5140 // This will be used to implement clause 'lastprivate'.
5141 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005142 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5143 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005144 SemaRef.AddInitializerToDecl(
5145 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5146 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5147
5148 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005149 VarDecl *STDecl =
5150 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5151 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005152 SemaRef.AddInitializerToDecl(
5153 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5154 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5155
5156 // Build expression: UB = min(UB, LastIteration)
5157 // It is nesessary for CodeGen of directives with static scheduling.
5158 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5159 UB.get(), LastIteration.get());
5160 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5161 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5162 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5163 CondOp.get());
5164 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005165
5166 // If we have a combined directive that combines 'distribute', 'for' or
5167 // 'simd' we need to be able to access the bounds of the schedule of the
5168 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5169 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5170 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5171 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5172
5173 // We expect to have at least 2 more parameters than the 'parallel'
5174 // directive does - the lower and upper bounds of the previous schedule.
5175 assert(CD->getNumParams() >= 4 &&
5176 "Unexpected number of parameters in loop combined directive");
5177
5178 // Set the proper type for the bounds given what we learned from the
5179 // enclosed loops.
5180 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5181 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5182
5183 // Previous lower and upper bounds are obtained from the region
5184 // parameters.
5185 PrevLB =
5186 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5187 PrevUB =
5188 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5189 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005190 }
5191
5192 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005193 ExprResult IV;
5194 ExprResult Init;
5195 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005196 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5197 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005198 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005199 isOpenMPTaskLoopDirective(DKind) ||
5200 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005201 ? LB.get()
5202 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5203 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5204 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005205 }
5206
Alexander Musmanc6388682014-12-15 07:07:06 +00005207 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005208 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005209 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005210 (isOpenMPWorksharingDirective(DKind) ||
5211 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005212 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5213 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5214 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005215
5216 // Loop increment (IV = IV + 1)
5217 SourceLocation IncLoc;
5218 ExprResult Inc =
5219 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5220 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5221 if (!Inc.isUsable())
5222 return 0;
5223 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005224 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5225 if (!Inc.isUsable())
5226 return 0;
5227
5228 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5229 // Used for directives with static scheduling.
5230 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005231 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5232 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005233 // LB + ST
5234 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5235 if (!NextLB.isUsable())
5236 return 0;
5237 // LB = LB + ST
5238 NextLB =
5239 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5240 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5241 if (!NextLB.isUsable())
5242 return 0;
5243 // UB + ST
5244 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5245 if (!NextUB.isUsable())
5246 return 0;
5247 // UB = UB + ST
5248 NextUB =
5249 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5250 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5251 if (!NextUB.isUsable())
5252 return 0;
5253 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005254
5255 // Build updates and final values of the loop counters.
5256 bool HasErrors = false;
5257 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005258 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005259 Built.Updates.resize(NestedLoopCount);
5260 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005261 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005262 {
5263 ExprResult Div;
5264 // Go from inner nested loop to outer.
5265 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5266 LoopIterationSpace &IS = IterSpaces[Cnt];
5267 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5268 // Build: Iter = (IV / Div) % IS.NumIters
5269 // where Div is product of previous iterations' IS.NumIters.
5270 ExprResult Iter;
5271 if (Div.isUsable()) {
5272 Iter =
5273 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5274 } else {
5275 Iter = IV;
5276 assert((Cnt == (int)NestedLoopCount - 1) &&
5277 "unusable div expected on first iteration only");
5278 }
5279
5280 if (Cnt != 0 && Iter.isUsable())
5281 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5282 IS.NumIterations);
5283 if (!Iter.isUsable()) {
5284 HasErrors = true;
5285 break;
5286 }
5287
Alexey Bataev39f915b82015-05-08 10:41:21 +00005288 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005289 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5290 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5291 IS.CounterVar->getExprLoc(),
5292 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005293 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005294 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005295 if (!Init.isUsable()) {
5296 HasErrors = true;
5297 break;
5298 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005299 ExprResult Update = BuildCounterUpdate(
5300 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5301 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005302 if (!Update.isUsable()) {
5303 HasErrors = true;
5304 break;
5305 }
5306
5307 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5308 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005309 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005310 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005311 if (!Final.isUsable()) {
5312 HasErrors = true;
5313 break;
5314 }
5315
5316 // Build Div for the next iteration: Div <- Div * IS.NumIters
5317 if (Cnt != 0) {
5318 if (Div.isUnset())
5319 Div = IS.NumIterations;
5320 else
5321 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5322 IS.NumIterations);
5323
5324 // Add parentheses (for debugging purposes only).
5325 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005326 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005327 if (!Div.isUsable()) {
5328 HasErrors = true;
5329 break;
5330 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005331 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005332 }
5333 if (!Update.isUsable() || !Final.isUsable()) {
5334 HasErrors = true;
5335 break;
5336 }
5337 // Save results
5338 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005339 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005340 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005341 Built.Updates[Cnt] = Update.get();
5342 Built.Finals[Cnt] = Final.get();
5343 }
5344 }
5345
5346 if (HasErrors)
5347 return 0;
5348
5349 // Save results
5350 Built.IterationVarRef = IV.get();
5351 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005352 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005353 Built.CalcLastIteration =
5354 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005355 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005356 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005357 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005358 Built.Init = Init.get();
5359 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005360 Built.LB = LB.get();
5361 Built.UB = UB.get();
5362 Built.IL = IL.get();
5363 Built.ST = ST.get();
5364 Built.EUB = EUB.get();
5365 Built.NLB = NextLB.get();
5366 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005367 Built.PrevLB = PrevLB.get();
5368 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005369
Alexey Bataev8b427062016-05-25 12:36:08 +00005370 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5371 // Fill data for doacross depend clauses.
5372 for (auto Pair : DSA.getDoacrossDependClauses()) {
5373 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5374 Pair.first->setCounterValue(CounterVal);
5375 else {
5376 if (NestedLoopCount != Pair.second.size() ||
5377 NestedLoopCount != LoopMultipliers.size() + 1) {
5378 // Erroneous case - clause has some problems.
5379 Pair.first->setCounterValue(CounterVal);
5380 continue;
5381 }
5382 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5383 auto I = Pair.second.rbegin();
5384 auto IS = IterSpaces.rbegin();
5385 auto ILM = LoopMultipliers.rbegin();
5386 Expr *UpCounterVal = CounterVal;
5387 Expr *Multiplier = nullptr;
5388 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5389 if (I->first) {
5390 assert(IS->CounterStep);
5391 Expr *NormalizedOffset =
5392 SemaRef
5393 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5394 I->first, IS->CounterStep)
5395 .get();
5396 if (Multiplier) {
5397 NormalizedOffset =
5398 SemaRef
5399 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5400 NormalizedOffset, Multiplier)
5401 .get();
5402 }
5403 assert(I->second == OO_Plus || I->second == OO_Minus);
5404 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5405 UpCounterVal =
5406 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5407 UpCounterVal, NormalizedOffset).get();
5408 }
5409 Multiplier = *ILM;
5410 ++I;
5411 ++IS;
5412 ++ILM;
5413 }
5414 Pair.first->setCounterValue(UpCounterVal);
5415 }
5416 }
5417
Alexey Bataevabfc0692014-06-25 06:52:00 +00005418 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005419}
5420
Alexey Bataev10e775f2015-07-30 11:36:16 +00005421static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005422 auto CollapseClauses =
5423 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5424 if (CollapseClauses.begin() != CollapseClauses.end())
5425 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005426 return nullptr;
5427}
5428
Alexey Bataev10e775f2015-07-30 11:36:16 +00005429static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005430 auto OrderedClauses =
5431 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5432 if (OrderedClauses.begin() != OrderedClauses.end())
5433 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005434 return nullptr;
5435}
5436
Alexey Bataev66b15b52015-08-21 11:14:16 +00005437static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
5438 const Expr *Safelen) {
5439 llvm::APSInt SimdlenRes, SafelenRes;
5440 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
5441 Simdlen->isInstantiationDependent() ||
5442 Simdlen->containsUnexpandedParameterPack())
5443 return false;
5444 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
5445 Safelen->isInstantiationDependent() ||
5446 Safelen->containsUnexpandedParameterPack())
5447 return false;
5448 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
5449 Safelen->EvaluateAsInt(SafelenRes, S.Context);
5450 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5451 // If both simdlen and safelen clauses are specified, the value of the simdlen
5452 // parameter must be less than or equal to the value of the safelen parameter.
5453 if (SimdlenRes > SafelenRes) {
5454 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
5455 << Simdlen->getSourceRange() << Safelen->getSourceRange();
5456 return true;
5457 }
5458 return false;
5459}
5460
Alexey Bataev4acb8592014-07-07 13:01:15 +00005461StmtResult Sema::ActOnOpenMPSimdDirective(
5462 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5463 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005464 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005465 if (!AStmt)
5466 return StmtError();
5467
5468 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005469 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005470 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5471 // define the nested loops number.
5472 unsigned NestedLoopCount = CheckOpenMPLoop(
5473 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5474 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005475 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005476 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005477
Alexander Musmana5f070a2014-10-01 06:03:56 +00005478 assert((CurContext->isDependentContext() || B.builtAll()) &&
5479 "omp simd loop exprs were not built");
5480
Alexander Musman3276a272015-03-21 10:12:56 +00005481 if (!CurContext->isDependentContext()) {
5482 // Finalize the clauses that need pre-built expressions for CodeGen.
5483 for (auto C : Clauses) {
5484 if (auto LC = dyn_cast<OMPLinearClause>(C))
5485 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005486 B.NumIterations, *this, CurScope,
5487 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005488 return StmtError();
5489 }
5490 }
5491
Alexey Bataev66b15b52015-08-21 11:14:16 +00005492 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5493 // If both simdlen and safelen clauses are specified, the value of the simdlen
5494 // parameter must be less than or equal to the value of the safelen parameter.
5495 OMPSafelenClause *Safelen = nullptr;
5496 OMPSimdlenClause *Simdlen = nullptr;
5497 for (auto *Clause : Clauses) {
5498 if (Clause->getClauseKind() == OMPC_safelen)
5499 Safelen = cast<OMPSafelenClause>(Clause);
5500 else if (Clause->getClauseKind() == OMPC_simdlen)
5501 Simdlen = cast<OMPSimdlenClause>(Clause);
5502 if (Safelen && Simdlen)
5503 break;
5504 }
5505 if (Simdlen && Safelen &&
5506 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5507 Safelen->getSafelen()))
5508 return StmtError();
5509
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005510 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005511 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5512 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005513}
5514
Alexey Bataev4acb8592014-07-07 13:01:15 +00005515StmtResult Sema::ActOnOpenMPForDirective(
5516 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5517 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005518 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005519 if (!AStmt)
5520 return StmtError();
5521
5522 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005523 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005524 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5525 // define the nested loops number.
5526 unsigned NestedLoopCount = CheckOpenMPLoop(
5527 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5528 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005529 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005530 return StmtError();
5531
Alexander Musmana5f070a2014-10-01 06:03:56 +00005532 assert((CurContext->isDependentContext() || B.builtAll()) &&
5533 "omp for loop exprs were not built");
5534
Alexey Bataev54acd402015-08-04 11:18:19 +00005535 if (!CurContext->isDependentContext()) {
5536 // Finalize the clauses that need pre-built expressions for CodeGen.
5537 for (auto C : Clauses) {
5538 if (auto LC = dyn_cast<OMPLinearClause>(C))
5539 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005540 B.NumIterations, *this, CurScope,
5541 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005542 return StmtError();
5543 }
5544 }
5545
Alexey Bataevf29276e2014-06-18 04:14:57 +00005546 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005547 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005548 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005549}
5550
Alexander Musmanf82886e2014-09-18 05:12:34 +00005551StmtResult Sema::ActOnOpenMPForSimdDirective(
5552 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5553 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005554 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005555 if (!AStmt)
5556 return StmtError();
5557
5558 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005559 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005560 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5561 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005562 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005563 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5564 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5565 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005566 if (NestedLoopCount == 0)
5567 return StmtError();
5568
Alexander Musmanc6388682014-12-15 07:07:06 +00005569 assert((CurContext->isDependentContext() || B.builtAll()) &&
5570 "omp for simd loop exprs were not built");
5571
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005572 if (!CurContext->isDependentContext()) {
5573 // Finalize the clauses that need pre-built expressions for CodeGen.
5574 for (auto C : Clauses) {
5575 if (auto LC = dyn_cast<OMPLinearClause>(C))
5576 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005577 B.NumIterations, *this, CurScope,
5578 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005579 return StmtError();
5580 }
5581 }
5582
Alexey Bataev66b15b52015-08-21 11:14:16 +00005583 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5584 // If both simdlen and safelen clauses are specified, the value of the simdlen
5585 // parameter must be less than or equal to the value of the safelen parameter.
5586 OMPSafelenClause *Safelen = nullptr;
5587 OMPSimdlenClause *Simdlen = nullptr;
5588 for (auto *Clause : Clauses) {
5589 if (Clause->getClauseKind() == OMPC_safelen)
5590 Safelen = cast<OMPSafelenClause>(Clause);
5591 else if (Clause->getClauseKind() == OMPC_simdlen)
5592 Simdlen = cast<OMPSimdlenClause>(Clause);
5593 if (Safelen && Simdlen)
5594 break;
5595 }
5596 if (Simdlen && Safelen &&
5597 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5598 Safelen->getSafelen()))
5599 return StmtError();
5600
Alexander Musmanf82886e2014-09-18 05:12:34 +00005601 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005602 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5603 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005604}
5605
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005606StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5607 Stmt *AStmt,
5608 SourceLocation StartLoc,
5609 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005610 if (!AStmt)
5611 return StmtError();
5612
5613 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005614 auto BaseStmt = AStmt;
5615 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5616 BaseStmt = CS->getCapturedStmt();
5617 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5618 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005619 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005620 return StmtError();
5621 // All associated statements must be '#pragma omp section' except for
5622 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005623 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005624 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5625 if (SectionStmt)
5626 Diag(SectionStmt->getLocStart(),
5627 diag::err_omp_sections_substmt_not_section);
5628 return StmtError();
5629 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005630 cast<OMPSectionDirective>(SectionStmt)
5631 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005632 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005633 } else {
5634 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5635 return StmtError();
5636 }
5637
5638 getCurFunction()->setHasBranchProtectedScope();
5639
Alexey Bataev25e5b442015-09-15 12:52:43 +00005640 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5641 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005642}
5643
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005644StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5645 SourceLocation StartLoc,
5646 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005647 if (!AStmt)
5648 return StmtError();
5649
5650 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005651
5652 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005653 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005654
Alexey Bataev25e5b442015-09-15 12:52:43 +00005655 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5656 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005657}
5658
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005659StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5660 Stmt *AStmt,
5661 SourceLocation StartLoc,
5662 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005663 if (!AStmt)
5664 return StmtError();
5665
5666 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005667
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005668 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005669
Alexey Bataev3255bf32015-01-19 05:20:46 +00005670 // OpenMP [2.7.3, single Construct, Restrictions]
5671 // The copyprivate clause must not be used with the nowait clause.
5672 OMPClause *Nowait = nullptr;
5673 OMPClause *Copyprivate = nullptr;
5674 for (auto *Clause : Clauses) {
5675 if (Clause->getClauseKind() == OMPC_nowait)
5676 Nowait = Clause;
5677 else if (Clause->getClauseKind() == OMPC_copyprivate)
5678 Copyprivate = Clause;
5679 if (Copyprivate && Nowait) {
5680 Diag(Copyprivate->getLocStart(),
5681 diag::err_omp_single_copyprivate_with_nowait);
5682 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5683 return StmtError();
5684 }
5685 }
5686
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005687 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5688}
5689
Alexander Musman80c22892014-07-17 08:54:58 +00005690StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5691 SourceLocation StartLoc,
5692 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005693 if (!AStmt)
5694 return StmtError();
5695
5696 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005697
5698 getCurFunction()->setHasBranchProtectedScope();
5699
5700 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5701}
5702
Alexey Bataev28c75412015-12-15 08:19:24 +00005703StmtResult Sema::ActOnOpenMPCriticalDirective(
5704 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5705 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005706 if (!AStmt)
5707 return StmtError();
5708
5709 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005710
Alexey Bataev28c75412015-12-15 08:19:24 +00005711 bool ErrorFound = false;
5712 llvm::APSInt Hint;
5713 SourceLocation HintLoc;
5714 bool DependentHint = false;
5715 for (auto *C : Clauses) {
5716 if (C->getClauseKind() == OMPC_hint) {
5717 if (!DirName.getName()) {
5718 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5719 ErrorFound = true;
5720 }
5721 Expr *E = cast<OMPHintClause>(C)->getHint();
5722 if (E->isTypeDependent() || E->isValueDependent() ||
5723 E->isInstantiationDependent())
5724 DependentHint = true;
5725 else {
5726 Hint = E->EvaluateKnownConstInt(Context);
5727 HintLoc = C->getLocStart();
5728 }
5729 }
5730 }
5731 if (ErrorFound)
5732 return StmtError();
5733 auto Pair = DSAStack->getCriticalWithHint(DirName);
5734 if (Pair.first && DirName.getName() && !DependentHint) {
5735 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5736 Diag(StartLoc, diag::err_omp_critical_with_hint);
5737 if (HintLoc.isValid()) {
5738 Diag(HintLoc, diag::note_omp_critical_hint_here)
5739 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5740 } else
5741 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5742 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5743 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5744 << 1
5745 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5746 /*Radix=*/10, /*Signed=*/false);
5747 } else
5748 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5749 }
5750 }
5751
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005752 getCurFunction()->setHasBranchProtectedScope();
5753
Alexey Bataev28c75412015-12-15 08:19:24 +00005754 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5755 Clauses, AStmt);
5756 if (!Pair.first && DirName.getName() && !DependentHint)
5757 DSAStack->addCriticalWithHint(Dir, Hint);
5758 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005759}
5760
Alexey Bataev4acb8592014-07-07 13:01:15 +00005761StmtResult Sema::ActOnOpenMPParallelForDirective(
5762 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5763 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005764 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005765 if (!AStmt)
5766 return StmtError();
5767
Alexey Bataev4acb8592014-07-07 13:01:15 +00005768 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5769 // 1.2.2 OpenMP Language Terminology
5770 // Structured block - An executable statement with a single entry at the
5771 // top and a single exit at the bottom.
5772 // The point of exit cannot be a branch out of the structured block.
5773 // longjmp() and throw() must not violate the entry/exit criteria.
5774 CS->getCapturedDecl()->setNothrow();
5775
Alexander Musmanc6388682014-12-15 07:07:06 +00005776 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005777 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5778 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005779 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005780 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5781 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5782 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005783 if (NestedLoopCount == 0)
5784 return StmtError();
5785
Alexander Musmana5f070a2014-10-01 06:03:56 +00005786 assert((CurContext->isDependentContext() || B.builtAll()) &&
5787 "omp parallel for loop exprs were not built");
5788
Alexey Bataev54acd402015-08-04 11:18:19 +00005789 if (!CurContext->isDependentContext()) {
5790 // Finalize the clauses that need pre-built expressions for CodeGen.
5791 for (auto C : Clauses) {
5792 if (auto LC = dyn_cast<OMPLinearClause>(C))
5793 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005794 B.NumIterations, *this, CurScope,
5795 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005796 return StmtError();
5797 }
5798 }
5799
Alexey Bataev4acb8592014-07-07 13:01:15 +00005800 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005801 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005802 NestedLoopCount, Clauses, AStmt, B,
5803 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005804}
5805
Alexander Musmane4e893b2014-09-23 09:33:00 +00005806StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5807 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5808 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005809 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005810 if (!AStmt)
5811 return StmtError();
5812
Alexander Musmane4e893b2014-09-23 09:33:00 +00005813 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5814 // 1.2.2 OpenMP Language Terminology
5815 // Structured block - An executable statement with a single entry at the
5816 // top and a single exit at the bottom.
5817 // The point of exit cannot be a branch out of the structured block.
5818 // longjmp() and throw() must not violate the entry/exit criteria.
5819 CS->getCapturedDecl()->setNothrow();
5820
Alexander Musmanc6388682014-12-15 07:07:06 +00005821 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005822 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5823 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005824 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005825 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5826 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5827 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005828 if (NestedLoopCount == 0)
5829 return StmtError();
5830
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005831 if (!CurContext->isDependentContext()) {
5832 // Finalize the clauses that need pre-built expressions for CodeGen.
5833 for (auto C : Clauses) {
5834 if (auto LC = dyn_cast<OMPLinearClause>(C))
5835 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005836 B.NumIterations, *this, CurScope,
5837 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005838 return StmtError();
5839 }
5840 }
5841
Alexey Bataev66b15b52015-08-21 11:14:16 +00005842 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5843 // If both simdlen and safelen clauses are specified, the value of the simdlen
5844 // parameter must be less than or equal to the value of the safelen parameter.
5845 OMPSafelenClause *Safelen = nullptr;
5846 OMPSimdlenClause *Simdlen = nullptr;
5847 for (auto *Clause : Clauses) {
5848 if (Clause->getClauseKind() == OMPC_safelen)
5849 Safelen = cast<OMPSafelenClause>(Clause);
5850 else if (Clause->getClauseKind() == OMPC_simdlen)
5851 Simdlen = cast<OMPSimdlenClause>(Clause);
5852 if (Safelen && Simdlen)
5853 break;
5854 }
5855 if (Simdlen && Safelen &&
5856 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5857 Safelen->getSafelen()))
5858 return StmtError();
5859
Alexander Musmane4e893b2014-09-23 09:33:00 +00005860 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005861 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005862 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005863}
5864
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005865StmtResult
5866Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5867 Stmt *AStmt, SourceLocation StartLoc,
5868 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005869 if (!AStmt)
5870 return StmtError();
5871
5872 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005873 auto BaseStmt = AStmt;
5874 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5875 BaseStmt = CS->getCapturedStmt();
5876 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5877 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005878 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005879 return StmtError();
5880 // All associated statements must be '#pragma omp section' except for
5881 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005882 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005883 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5884 if (SectionStmt)
5885 Diag(SectionStmt->getLocStart(),
5886 diag::err_omp_parallel_sections_substmt_not_section);
5887 return StmtError();
5888 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005889 cast<OMPSectionDirective>(SectionStmt)
5890 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005891 }
5892 } else {
5893 Diag(AStmt->getLocStart(),
5894 diag::err_omp_parallel_sections_not_compound_stmt);
5895 return StmtError();
5896 }
5897
5898 getCurFunction()->setHasBranchProtectedScope();
5899
Alexey Bataev25e5b442015-09-15 12:52:43 +00005900 return OMPParallelSectionsDirective::Create(
5901 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005902}
5903
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005904StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5905 Stmt *AStmt, SourceLocation StartLoc,
5906 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005907 if (!AStmt)
5908 return StmtError();
5909
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005910 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5911 // 1.2.2 OpenMP Language Terminology
5912 // Structured block - An executable statement with a single entry at the
5913 // top and a single exit at the bottom.
5914 // The point of exit cannot be a branch out of the structured block.
5915 // longjmp() and throw() must not violate the entry/exit criteria.
5916 CS->getCapturedDecl()->setNothrow();
5917
5918 getCurFunction()->setHasBranchProtectedScope();
5919
Alexey Bataev25e5b442015-09-15 12:52:43 +00005920 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5921 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005922}
5923
Alexey Bataev68446b72014-07-18 07:47:19 +00005924StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5925 SourceLocation EndLoc) {
5926 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5927}
5928
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005929StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5930 SourceLocation EndLoc) {
5931 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5932}
5933
Alexey Bataev2df347a2014-07-18 10:17:07 +00005934StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5935 SourceLocation EndLoc) {
5936 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5937}
5938
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005939StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5940 SourceLocation StartLoc,
5941 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005942 if (!AStmt)
5943 return StmtError();
5944
5945 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005946
5947 getCurFunction()->setHasBranchProtectedScope();
5948
5949 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5950}
5951
Alexey Bataev6125da92014-07-21 11:26:11 +00005952StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5953 SourceLocation StartLoc,
5954 SourceLocation EndLoc) {
5955 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5956 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5957}
5958
Alexey Bataev346265e2015-09-25 10:37:12 +00005959StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5960 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005961 SourceLocation StartLoc,
5962 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005963 OMPClause *DependFound = nullptr;
5964 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005965 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005966 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005967 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005968 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005969 for (auto *C : Clauses) {
5970 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5971 DependFound = C;
5972 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5973 if (DependSourceClause) {
5974 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5975 << getOpenMPDirectiveName(OMPD_ordered)
5976 << getOpenMPClauseName(OMPC_depend) << 2;
5977 ErrorFound = true;
5978 } else
5979 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005980 if (DependSinkClause) {
5981 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5982 << 0;
5983 ErrorFound = true;
5984 }
5985 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5986 if (DependSourceClause) {
5987 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5988 << 1;
5989 ErrorFound = true;
5990 }
5991 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005992 }
5993 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005994 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005995 else if (C->getClauseKind() == OMPC_simd)
5996 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005997 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005998 if (!ErrorFound && !SC &&
5999 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006000 // OpenMP [2.8.1,simd Construct, Restrictions]
6001 // An ordered construct with the simd clause is the only OpenMP construct
6002 // that can appear in the simd region.
6003 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006004 ErrorFound = true;
6005 } else if (DependFound && (TC || SC)) {
6006 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6007 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6008 ErrorFound = true;
6009 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6010 Diag(DependFound->getLocStart(),
6011 diag::err_omp_ordered_directive_without_param);
6012 ErrorFound = true;
6013 } else if (TC || Clauses.empty()) {
6014 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6015 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6016 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6017 << (TC != nullptr);
6018 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6019 ErrorFound = true;
6020 }
6021 }
6022 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006023 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006024
6025 if (AStmt) {
6026 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6027
6028 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006029 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006030
6031 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006032}
6033
Alexey Bataev1d160b12015-03-13 12:27:31 +00006034namespace {
6035/// \brief Helper class for checking expression in 'omp atomic [update]'
6036/// construct.
6037class OpenMPAtomicUpdateChecker {
6038 /// \brief Error results for atomic update expressions.
6039 enum ExprAnalysisErrorCode {
6040 /// \brief A statement is not an expression statement.
6041 NotAnExpression,
6042 /// \brief Expression is not builtin binary or unary operation.
6043 NotABinaryOrUnaryExpression,
6044 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6045 NotAnUnaryIncDecExpression,
6046 /// \brief An expression is not of scalar type.
6047 NotAScalarType,
6048 /// \brief A binary operation is not an assignment operation.
6049 NotAnAssignmentOp,
6050 /// \brief RHS part of the binary operation is not a binary expression.
6051 NotABinaryExpression,
6052 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6053 /// expression.
6054 NotABinaryOperator,
6055 /// \brief RHS binary operation does not have reference to the updated LHS
6056 /// part.
6057 NotAnUpdateExpression,
6058 /// \brief No errors is found.
6059 NoError
6060 };
6061 /// \brief Reference to Sema.
6062 Sema &SemaRef;
6063 /// \brief A location for note diagnostics (when error is found).
6064 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006065 /// \brief 'x' lvalue part of the source atomic expression.
6066 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006067 /// \brief 'expr' rvalue part of the source atomic expression.
6068 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006069 /// \brief Helper expression of the form
6070 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6071 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6072 Expr *UpdateExpr;
6073 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6074 /// important for non-associative operations.
6075 bool IsXLHSInRHSPart;
6076 BinaryOperatorKind Op;
6077 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006078 /// \brief true if the source expression is a postfix unary operation, false
6079 /// if it is a prefix unary operation.
6080 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006081
6082public:
6083 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006084 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006085 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006086 /// \brief Check specified statement that it is suitable for 'atomic update'
6087 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006088 /// expression. If DiagId and NoteId == 0, then only check is performed
6089 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006090 /// \param DiagId Diagnostic which should be emitted if error is found.
6091 /// \param NoteId Diagnostic note for the main error message.
6092 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006093 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006094 /// \brief Return the 'x' lvalue part of the source atomic expression.
6095 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006096 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6097 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006098 /// \brief Return the update expression used in calculation of the updated
6099 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6100 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6101 Expr *getUpdateExpr() const { return UpdateExpr; }
6102 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6103 /// false otherwise.
6104 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6105
Alexey Bataevb78ca832015-04-01 03:33:17 +00006106 /// \brief true if the source expression is a postfix unary operation, false
6107 /// if it is a prefix unary operation.
6108 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6109
Alexey Bataev1d160b12015-03-13 12:27:31 +00006110private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006111 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6112 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006113};
6114} // namespace
6115
6116bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6117 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6118 ExprAnalysisErrorCode ErrorFound = NoError;
6119 SourceLocation ErrorLoc, NoteLoc;
6120 SourceRange ErrorRange, NoteRange;
6121 // Allowed constructs are:
6122 // x = x binop expr;
6123 // x = expr binop x;
6124 if (AtomicBinOp->getOpcode() == BO_Assign) {
6125 X = AtomicBinOp->getLHS();
6126 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6127 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6128 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6129 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6130 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006131 Op = AtomicInnerBinOp->getOpcode();
6132 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006133 auto *LHS = AtomicInnerBinOp->getLHS();
6134 auto *RHS = AtomicInnerBinOp->getRHS();
6135 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6136 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6137 /*Canonical=*/true);
6138 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6139 /*Canonical=*/true);
6140 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6141 /*Canonical=*/true);
6142 if (XId == LHSId) {
6143 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006144 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006145 } else if (XId == RHSId) {
6146 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006147 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006148 } else {
6149 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6150 ErrorRange = AtomicInnerBinOp->getSourceRange();
6151 NoteLoc = X->getExprLoc();
6152 NoteRange = X->getSourceRange();
6153 ErrorFound = NotAnUpdateExpression;
6154 }
6155 } else {
6156 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6157 ErrorRange = AtomicInnerBinOp->getSourceRange();
6158 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6159 NoteRange = SourceRange(NoteLoc, NoteLoc);
6160 ErrorFound = NotABinaryOperator;
6161 }
6162 } else {
6163 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6164 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6165 ErrorFound = NotABinaryExpression;
6166 }
6167 } else {
6168 ErrorLoc = AtomicBinOp->getExprLoc();
6169 ErrorRange = AtomicBinOp->getSourceRange();
6170 NoteLoc = AtomicBinOp->getOperatorLoc();
6171 NoteRange = SourceRange(NoteLoc, NoteLoc);
6172 ErrorFound = NotAnAssignmentOp;
6173 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006174 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006175 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6176 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6177 return true;
6178 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006179 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006180 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006181}
6182
6183bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6184 unsigned NoteId) {
6185 ExprAnalysisErrorCode ErrorFound = NoError;
6186 SourceLocation ErrorLoc, NoteLoc;
6187 SourceRange ErrorRange, NoteRange;
6188 // Allowed constructs are:
6189 // x++;
6190 // x--;
6191 // ++x;
6192 // --x;
6193 // x binop= expr;
6194 // x = x binop expr;
6195 // x = expr binop x;
6196 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6197 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6198 if (AtomicBody->getType()->isScalarType() ||
6199 AtomicBody->isInstantiationDependent()) {
6200 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6201 AtomicBody->IgnoreParenImpCasts())) {
6202 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006203 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006204 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006205 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006206 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006207 X = AtomicCompAssignOp->getLHS();
6208 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006209 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6210 AtomicBody->IgnoreParenImpCasts())) {
6211 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006212 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6213 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006214 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006215 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6216 // Check for Unary Operation
6217 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006218 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006219 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6220 OpLoc = AtomicUnaryOp->getOperatorLoc();
6221 X = AtomicUnaryOp->getSubExpr();
6222 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6223 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006224 } else {
6225 ErrorFound = NotAnUnaryIncDecExpression;
6226 ErrorLoc = AtomicUnaryOp->getExprLoc();
6227 ErrorRange = AtomicUnaryOp->getSourceRange();
6228 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6229 NoteRange = SourceRange(NoteLoc, NoteLoc);
6230 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006231 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006232 ErrorFound = NotABinaryOrUnaryExpression;
6233 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6234 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6235 }
6236 } else {
6237 ErrorFound = NotAScalarType;
6238 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6239 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6240 }
6241 } else {
6242 ErrorFound = NotAnExpression;
6243 NoteLoc = ErrorLoc = S->getLocStart();
6244 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6245 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006246 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006247 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6248 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6249 return true;
6250 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006251 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006252 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006253 // Build an update expression of form 'OpaqueValueExpr(x) binop
6254 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6255 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6256 auto *OVEX = new (SemaRef.getASTContext())
6257 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6258 auto *OVEExpr = new (SemaRef.getASTContext())
6259 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6260 auto Update =
6261 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6262 IsXLHSInRHSPart ? OVEExpr : OVEX);
6263 if (Update.isInvalid())
6264 return true;
6265 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6266 Sema::AA_Casting);
6267 if (Update.isInvalid())
6268 return true;
6269 UpdateExpr = Update.get();
6270 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006271 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006272}
6273
Alexey Bataev0162e452014-07-22 10:10:35 +00006274StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6275 Stmt *AStmt,
6276 SourceLocation StartLoc,
6277 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006278 if (!AStmt)
6279 return StmtError();
6280
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006281 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006282 // 1.2.2 OpenMP Language Terminology
6283 // Structured block - An executable statement with a single entry at the
6284 // top and a single exit at the bottom.
6285 // The point of exit cannot be a branch out of the structured block.
6286 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006287 OpenMPClauseKind AtomicKind = OMPC_unknown;
6288 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006289 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006290 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006291 C->getClauseKind() == OMPC_update ||
6292 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006293 if (AtomicKind != OMPC_unknown) {
6294 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6295 << SourceRange(C->getLocStart(), C->getLocEnd());
6296 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6297 << getOpenMPClauseName(AtomicKind);
6298 } else {
6299 AtomicKind = C->getClauseKind();
6300 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006301 }
6302 }
6303 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006304
Alexey Bataev459dec02014-07-24 06:46:57 +00006305 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006306 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6307 Body = EWC->getSubExpr();
6308
Alexey Bataev62cec442014-11-18 10:14:22 +00006309 Expr *X = nullptr;
6310 Expr *V = nullptr;
6311 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006312 Expr *UE = nullptr;
6313 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006314 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006315 // OpenMP [2.12.6, atomic Construct]
6316 // In the next expressions:
6317 // * x and v (as applicable) are both l-value expressions with scalar type.
6318 // * During the execution of an atomic region, multiple syntactic
6319 // occurrences of x must designate the same storage location.
6320 // * Neither of v and expr (as applicable) may access the storage location
6321 // designated by x.
6322 // * Neither of x and expr (as applicable) may access the storage location
6323 // designated by v.
6324 // * expr is an expression with scalar type.
6325 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6326 // * binop, binop=, ++, and -- are not overloaded operators.
6327 // * The expression x binop expr must be numerically equivalent to x binop
6328 // (expr). This requirement is satisfied if the operators in expr have
6329 // precedence greater than binop, or by using parentheses around expr or
6330 // subexpressions of expr.
6331 // * The expression expr binop x must be numerically equivalent to (expr)
6332 // binop x. This requirement is satisfied if the operators in expr have
6333 // precedence equal to or greater than binop, or by using parentheses around
6334 // expr or subexpressions of expr.
6335 // * For forms that allow multiple occurrences of x, the number of times
6336 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006337 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006338 enum {
6339 NotAnExpression,
6340 NotAnAssignmentOp,
6341 NotAScalarType,
6342 NotAnLValue,
6343 NoError
6344 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006345 SourceLocation ErrorLoc, NoteLoc;
6346 SourceRange ErrorRange, NoteRange;
6347 // If clause is read:
6348 // v = x;
6349 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6350 auto AtomicBinOp =
6351 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6352 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6353 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6354 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6355 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6356 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6357 if (!X->isLValue() || !V->isLValue()) {
6358 auto NotLValueExpr = X->isLValue() ? V : X;
6359 ErrorFound = NotAnLValue;
6360 ErrorLoc = AtomicBinOp->getExprLoc();
6361 ErrorRange = AtomicBinOp->getSourceRange();
6362 NoteLoc = NotLValueExpr->getExprLoc();
6363 NoteRange = NotLValueExpr->getSourceRange();
6364 }
6365 } else if (!X->isInstantiationDependent() ||
6366 !V->isInstantiationDependent()) {
6367 auto NotScalarExpr =
6368 (X->isInstantiationDependent() || X->getType()->isScalarType())
6369 ? V
6370 : X;
6371 ErrorFound = NotAScalarType;
6372 ErrorLoc = AtomicBinOp->getExprLoc();
6373 ErrorRange = AtomicBinOp->getSourceRange();
6374 NoteLoc = NotScalarExpr->getExprLoc();
6375 NoteRange = NotScalarExpr->getSourceRange();
6376 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006377 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006378 ErrorFound = NotAnAssignmentOp;
6379 ErrorLoc = AtomicBody->getExprLoc();
6380 ErrorRange = AtomicBody->getSourceRange();
6381 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6382 : AtomicBody->getExprLoc();
6383 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6384 : AtomicBody->getSourceRange();
6385 }
6386 } else {
6387 ErrorFound = NotAnExpression;
6388 NoteLoc = ErrorLoc = Body->getLocStart();
6389 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006390 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006391 if (ErrorFound != NoError) {
6392 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6393 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006394 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6395 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006396 return StmtError();
6397 } else if (CurContext->isDependentContext())
6398 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006399 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006400 enum {
6401 NotAnExpression,
6402 NotAnAssignmentOp,
6403 NotAScalarType,
6404 NotAnLValue,
6405 NoError
6406 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006407 SourceLocation ErrorLoc, NoteLoc;
6408 SourceRange ErrorRange, NoteRange;
6409 // If clause is write:
6410 // x = expr;
6411 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6412 auto AtomicBinOp =
6413 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6414 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006415 X = AtomicBinOp->getLHS();
6416 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006417 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6418 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6419 if (!X->isLValue()) {
6420 ErrorFound = NotAnLValue;
6421 ErrorLoc = AtomicBinOp->getExprLoc();
6422 ErrorRange = AtomicBinOp->getSourceRange();
6423 NoteLoc = X->getExprLoc();
6424 NoteRange = X->getSourceRange();
6425 }
6426 } else if (!X->isInstantiationDependent() ||
6427 !E->isInstantiationDependent()) {
6428 auto NotScalarExpr =
6429 (X->isInstantiationDependent() || X->getType()->isScalarType())
6430 ? E
6431 : X;
6432 ErrorFound = NotAScalarType;
6433 ErrorLoc = AtomicBinOp->getExprLoc();
6434 ErrorRange = AtomicBinOp->getSourceRange();
6435 NoteLoc = NotScalarExpr->getExprLoc();
6436 NoteRange = NotScalarExpr->getSourceRange();
6437 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006438 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006439 ErrorFound = NotAnAssignmentOp;
6440 ErrorLoc = AtomicBody->getExprLoc();
6441 ErrorRange = AtomicBody->getSourceRange();
6442 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6443 : AtomicBody->getExprLoc();
6444 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6445 : AtomicBody->getSourceRange();
6446 }
6447 } else {
6448 ErrorFound = NotAnExpression;
6449 NoteLoc = ErrorLoc = Body->getLocStart();
6450 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006451 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006452 if (ErrorFound != NoError) {
6453 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6454 << ErrorRange;
6455 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6456 << NoteRange;
6457 return StmtError();
6458 } else if (CurContext->isDependentContext())
6459 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006460 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006461 // If clause is update:
6462 // x++;
6463 // x--;
6464 // ++x;
6465 // --x;
6466 // x binop= expr;
6467 // x = x binop expr;
6468 // x = expr binop x;
6469 OpenMPAtomicUpdateChecker Checker(*this);
6470 if (Checker.checkStatement(
6471 Body, (AtomicKind == OMPC_update)
6472 ? diag::err_omp_atomic_update_not_expression_statement
6473 : diag::err_omp_atomic_not_expression_statement,
6474 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006475 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006476 if (!CurContext->isDependentContext()) {
6477 E = Checker.getExpr();
6478 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006479 UE = Checker.getUpdateExpr();
6480 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006481 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006482 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006483 enum {
6484 NotAnAssignmentOp,
6485 NotACompoundStatement,
6486 NotTwoSubstatements,
6487 NotASpecificExpression,
6488 NoError
6489 } ErrorFound = NoError;
6490 SourceLocation ErrorLoc, NoteLoc;
6491 SourceRange ErrorRange, NoteRange;
6492 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6493 // If clause is a capture:
6494 // v = x++;
6495 // v = x--;
6496 // v = ++x;
6497 // v = --x;
6498 // v = x binop= expr;
6499 // v = x = x binop expr;
6500 // v = x = expr binop x;
6501 auto *AtomicBinOp =
6502 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6503 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6504 V = AtomicBinOp->getLHS();
6505 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6506 OpenMPAtomicUpdateChecker Checker(*this);
6507 if (Checker.checkStatement(
6508 Body, diag::err_omp_atomic_capture_not_expression_statement,
6509 diag::note_omp_atomic_update))
6510 return StmtError();
6511 E = Checker.getExpr();
6512 X = Checker.getX();
6513 UE = Checker.getUpdateExpr();
6514 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6515 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006516 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006517 ErrorLoc = AtomicBody->getExprLoc();
6518 ErrorRange = AtomicBody->getSourceRange();
6519 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6520 : AtomicBody->getExprLoc();
6521 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6522 : AtomicBody->getSourceRange();
6523 ErrorFound = NotAnAssignmentOp;
6524 }
6525 if (ErrorFound != NoError) {
6526 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6527 << ErrorRange;
6528 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6529 return StmtError();
6530 } else if (CurContext->isDependentContext()) {
6531 UE = V = E = X = nullptr;
6532 }
6533 } else {
6534 // If clause is a capture:
6535 // { v = x; x = expr; }
6536 // { v = x; x++; }
6537 // { v = x; x--; }
6538 // { v = x; ++x; }
6539 // { v = x; --x; }
6540 // { v = x; x binop= expr; }
6541 // { v = x; x = x binop expr; }
6542 // { v = x; x = expr binop x; }
6543 // { x++; v = x; }
6544 // { x--; v = x; }
6545 // { ++x; v = x; }
6546 // { --x; v = x; }
6547 // { x binop= expr; v = x; }
6548 // { x = x binop expr; v = x; }
6549 // { x = expr binop x; v = x; }
6550 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6551 // Check that this is { expr1; expr2; }
6552 if (CS->size() == 2) {
6553 auto *First = CS->body_front();
6554 auto *Second = CS->body_back();
6555 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6556 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6557 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6558 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6559 // Need to find what subexpression is 'v' and what is 'x'.
6560 OpenMPAtomicUpdateChecker Checker(*this);
6561 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6562 BinaryOperator *BinOp = nullptr;
6563 if (IsUpdateExprFound) {
6564 BinOp = dyn_cast<BinaryOperator>(First);
6565 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6566 }
6567 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6568 // { v = x; x++; }
6569 // { v = x; x--; }
6570 // { v = x; ++x; }
6571 // { v = x; --x; }
6572 // { v = x; x binop= expr; }
6573 // { v = x; x = x binop expr; }
6574 // { v = x; x = expr binop x; }
6575 // Check that the first expression has form v = x.
6576 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6577 llvm::FoldingSetNodeID XId, PossibleXId;
6578 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6579 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6580 IsUpdateExprFound = XId == PossibleXId;
6581 if (IsUpdateExprFound) {
6582 V = BinOp->getLHS();
6583 X = Checker.getX();
6584 E = Checker.getExpr();
6585 UE = Checker.getUpdateExpr();
6586 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006587 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006588 }
6589 }
6590 if (!IsUpdateExprFound) {
6591 IsUpdateExprFound = !Checker.checkStatement(First);
6592 BinOp = nullptr;
6593 if (IsUpdateExprFound) {
6594 BinOp = dyn_cast<BinaryOperator>(Second);
6595 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6596 }
6597 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6598 // { x++; v = x; }
6599 // { x--; v = x; }
6600 // { ++x; v = x; }
6601 // { --x; v = x; }
6602 // { x binop= expr; v = x; }
6603 // { x = x binop expr; v = x; }
6604 // { x = expr binop x; v = x; }
6605 // Check that the second expression has form v = x.
6606 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6607 llvm::FoldingSetNodeID XId, PossibleXId;
6608 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6609 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6610 IsUpdateExprFound = XId == PossibleXId;
6611 if (IsUpdateExprFound) {
6612 V = BinOp->getLHS();
6613 X = Checker.getX();
6614 E = Checker.getExpr();
6615 UE = Checker.getUpdateExpr();
6616 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006617 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006618 }
6619 }
6620 }
6621 if (!IsUpdateExprFound) {
6622 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006623 auto *FirstExpr = dyn_cast<Expr>(First);
6624 auto *SecondExpr = dyn_cast<Expr>(Second);
6625 if (!FirstExpr || !SecondExpr ||
6626 !(FirstExpr->isInstantiationDependent() ||
6627 SecondExpr->isInstantiationDependent())) {
6628 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6629 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006630 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006631 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6632 : First->getLocStart();
6633 NoteRange = ErrorRange = FirstBinOp
6634 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006635 : SourceRange(ErrorLoc, ErrorLoc);
6636 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006637 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6638 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6639 ErrorFound = NotAnAssignmentOp;
6640 NoteLoc = ErrorLoc = SecondBinOp
6641 ? SecondBinOp->getOperatorLoc()
6642 : Second->getLocStart();
6643 NoteRange = ErrorRange =
6644 SecondBinOp ? SecondBinOp->getSourceRange()
6645 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006646 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006647 auto *PossibleXRHSInFirst =
6648 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6649 auto *PossibleXLHSInSecond =
6650 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6651 llvm::FoldingSetNodeID X1Id, X2Id;
6652 PossibleXRHSInFirst->Profile(X1Id, Context,
6653 /*Canonical=*/true);
6654 PossibleXLHSInSecond->Profile(X2Id, Context,
6655 /*Canonical=*/true);
6656 IsUpdateExprFound = X1Id == X2Id;
6657 if (IsUpdateExprFound) {
6658 V = FirstBinOp->getLHS();
6659 X = SecondBinOp->getLHS();
6660 E = SecondBinOp->getRHS();
6661 UE = nullptr;
6662 IsXLHSInRHSPart = false;
6663 IsPostfixUpdate = true;
6664 } else {
6665 ErrorFound = NotASpecificExpression;
6666 ErrorLoc = FirstBinOp->getExprLoc();
6667 ErrorRange = FirstBinOp->getSourceRange();
6668 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6669 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6670 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006671 }
6672 }
6673 }
6674 }
6675 } else {
6676 NoteLoc = ErrorLoc = Body->getLocStart();
6677 NoteRange = ErrorRange =
6678 SourceRange(Body->getLocStart(), Body->getLocStart());
6679 ErrorFound = NotTwoSubstatements;
6680 }
6681 } else {
6682 NoteLoc = ErrorLoc = Body->getLocStart();
6683 NoteRange = ErrorRange =
6684 SourceRange(Body->getLocStart(), Body->getLocStart());
6685 ErrorFound = NotACompoundStatement;
6686 }
6687 if (ErrorFound != NoError) {
6688 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6689 << ErrorRange;
6690 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6691 return StmtError();
6692 } else if (CurContext->isDependentContext()) {
6693 UE = V = E = X = nullptr;
6694 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006695 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006696 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006697
6698 getCurFunction()->setHasBranchProtectedScope();
6699
Alexey Bataev62cec442014-11-18 10:14:22 +00006700 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006701 X, V, E, UE, IsXLHSInRHSPart,
6702 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006703}
6704
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006705StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6706 Stmt *AStmt,
6707 SourceLocation StartLoc,
6708 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006709 if (!AStmt)
6710 return StmtError();
6711
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006712 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6713 // 1.2.2 OpenMP Language Terminology
6714 // Structured block - An executable statement with a single entry at the
6715 // top and a single exit at the bottom.
6716 // The point of exit cannot be a branch out of the structured block.
6717 // longjmp() and throw() must not violate the entry/exit criteria.
6718 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006719
Alexey Bataev13314bf2014-10-09 04:18:56 +00006720 // OpenMP [2.16, Nesting of Regions]
6721 // If specified, a teams construct must be contained within a target
6722 // construct. That target construct must contain no statements or directives
6723 // outside of the teams construct.
6724 if (DSAStack->hasInnerTeamsRegion()) {
6725 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6726 bool OMPTeamsFound = true;
6727 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6728 auto I = CS->body_begin();
6729 while (I != CS->body_end()) {
6730 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6731 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6732 OMPTeamsFound = false;
6733 break;
6734 }
6735 ++I;
6736 }
6737 assert(I != CS->body_end() && "Not found statement");
6738 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006739 } else {
6740 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6741 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006742 }
6743 if (!OMPTeamsFound) {
6744 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6745 Diag(DSAStack->getInnerTeamsRegionLoc(),
6746 diag::note_omp_nested_teams_construct_here);
6747 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6748 << isa<OMPExecutableDirective>(S);
6749 return StmtError();
6750 }
6751 }
6752
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006753 getCurFunction()->setHasBranchProtectedScope();
6754
6755 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6756}
6757
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006758StmtResult
6759Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6760 Stmt *AStmt, SourceLocation StartLoc,
6761 SourceLocation EndLoc) {
6762 if (!AStmt)
6763 return StmtError();
6764
6765 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6766 // 1.2.2 OpenMP Language Terminology
6767 // Structured block - An executable statement with a single entry at the
6768 // top and a single exit at the bottom.
6769 // The point of exit cannot be a branch out of the structured block.
6770 // longjmp() and throw() must not violate the entry/exit criteria.
6771 CS->getCapturedDecl()->setNothrow();
6772
6773 getCurFunction()->setHasBranchProtectedScope();
6774
6775 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6776 AStmt);
6777}
6778
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006779StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6780 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6781 SourceLocation EndLoc,
6782 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6783 if (!AStmt)
6784 return StmtError();
6785
6786 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6787 // 1.2.2 OpenMP Language Terminology
6788 // Structured block - An executable statement with a single entry at the
6789 // top and a single exit at the bottom.
6790 // The point of exit cannot be a branch out of the structured block.
6791 // longjmp() and throw() must not violate the entry/exit criteria.
6792 CS->getCapturedDecl()->setNothrow();
6793
6794 OMPLoopDirective::HelperExprs B;
6795 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6796 // define the nested loops number.
6797 unsigned NestedLoopCount =
6798 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6799 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6800 VarsWithImplicitDSA, B);
6801 if (NestedLoopCount == 0)
6802 return StmtError();
6803
6804 assert((CurContext->isDependentContext() || B.builtAll()) &&
6805 "omp target parallel for loop exprs were not built");
6806
6807 if (!CurContext->isDependentContext()) {
6808 // Finalize the clauses that need pre-built expressions for CodeGen.
6809 for (auto C : Clauses) {
6810 if (auto LC = dyn_cast<OMPLinearClause>(C))
6811 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006812 B.NumIterations, *this, CurScope,
6813 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006814 return StmtError();
6815 }
6816 }
6817
6818 getCurFunction()->setHasBranchProtectedScope();
6819 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6820 NestedLoopCount, Clauses, AStmt,
6821 B, DSAStack->isCancelRegion());
6822}
6823
Samuel Antaodf67fc42016-01-19 19:15:56 +00006824/// \brief Check for existence of a map clause in the list of clauses.
6825static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6826 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6827 I != E; ++I) {
6828 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6829 return true;
6830 }
6831 }
6832
6833 return false;
6834}
6835
Michael Wong65f367f2015-07-21 13:44:28 +00006836StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6837 Stmt *AStmt,
6838 SourceLocation StartLoc,
6839 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006840 if (!AStmt)
6841 return StmtError();
6842
6843 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6844
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006845 // OpenMP [2.10.1, Restrictions, p. 97]
6846 // At least one map clause must appear on the directive.
6847 if (!HasMapClause(Clauses)) {
6848 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6849 getOpenMPDirectiveName(OMPD_target_data);
6850 return StmtError();
6851 }
6852
Michael Wong65f367f2015-07-21 13:44:28 +00006853 getCurFunction()->setHasBranchProtectedScope();
6854
6855 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6856 AStmt);
6857}
6858
Samuel Antaodf67fc42016-01-19 19:15:56 +00006859StmtResult
6860Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6861 SourceLocation StartLoc,
6862 SourceLocation EndLoc) {
6863 // OpenMP [2.10.2, Restrictions, p. 99]
6864 // At least one map clause must appear on the directive.
6865 if (!HasMapClause(Clauses)) {
6866 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6867 << getOpenMPDirectiveName(OMPD_target_enter_data);
6868 return StmtError();
6869 }
6870
6871 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6872 Clauses);
6873}
6874
Samuel Antao72590762016-01-19 20:04:50 +00006875StmtResult
6876Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6877 SourceLocation StartLoc,
6878 SourceLocation EndLoc) {
6879 // OpenMP [2.10.3, Restrictions, p. 102]
6880 // At least one map clause must appear on the directive.
6881 if (!HasMapClause(Clauses)) {
6882 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6883 << getOpenMPDirectiveName(OMPD_target_exit_data);
6884 return StmtError();
6885 }
6886
6887 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6888}
6889
Samuel Antao686c70c2016-05-26 17:30:50 +00006890StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6891 SourceLocation StartLoc,
6892 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006893 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00006894 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00006895 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00006896 seenMotionClause = true;
6897 }
Samuel Antao686c70c2016-05-26 17:30:50 +00006898 if (!seenMotionClause) {
6899 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6900 return StmtError();
6901 }
6902 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6903}
6904
Alexey Bataev13314bf2014-10-09 04:18:56 +00006905StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6906 Stmt *AStmt, SourceLocation StartLoc,
6907 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006908 if (!AStmt)
6909 return StmtError();
6910
Alexey Bataev13314bf2014-10-09 04:18:56 +00006911 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6912 // 1.2.2 OpenMP Language Terminology
6913 // Structured block - An executable statement with a single entry at the
6914 // top and a single exit at the bottom.
6915 // The point of exit cannot be a branch out of the structured block.
6916 // longjmp() and throw() must not violate the entry/exit criteria.
6917 CS->getCapturedDecl()->setNothrow();
6918
6919 getCurFunction()->setHasBranchProtectedScope();
6920
6921 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6922}
6923
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006924StmtResult
6925Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6926 SourceLocation EndLoc,
6927 OpenMPDirectiveKind CancelRegion) {
6928 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6929 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6930 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6931 << getOpenMPDirectiveName(CancelRegion);
6932 return StmtError();
6933 }
6934 if (DSAStack->isParentNowaitRegion()) {
6935 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6936 return StmtError();
6937 }
6938 if (DSAStack->isParentOrderedRegion()) {
6939 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6940 return StmtError();
6941 }
6942 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6943 CancelRegion);
6944}
6945
Alexey Bataev87933c72015-09-18 08:07:34 +00006946StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6947 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006948 SourceLocation EndLoc,
6949 OpenMPDirectiveKind CancelRegion) {
6950 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6951 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6952 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6953 << getOpenMPDirectiveName(CancelRegion);
6954 return StmtError();
6955 }
6956 if (DSAStack->isParentNowaitRegion()) {
6957 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6958 return StmtError();
6959 }
6960 if (DSAStack->isParentOrderedRegion()) {
6961 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6962 return StmtError();
6963 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006964 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006965 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6966 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006967}
6968
Alexey Bataev382967a2015-12-08 12:06:20 +00006969static bool checkGrainsizeNumTasksClauses(Sema &S,
6970 ArrayRef<OMPClause *> Clauses) {
6971 OMPClause *PrevClause = nullptr;
6972 bool ErrorFound = false;
6973 for (auto *C : Clauses) {
6974 if (C->getClauseKind() == OMPC_grainsize ||
6975 C->getClauseKind() == OMPC_num_tasks) {
6976 if (!PrevClause)
6977 PrevClause = C;
6978 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6979 S.Diag(C->getLocStart(),
6980 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6981 << getOpenMPClauseName(C->getClauseKind())
6982 << getOpenMPClauseName(PrevClause->getClauseKind());
6983 S.Diag(PrevClause->getLocStart(),
6984 diag::note_omp_previous_grainsize_num_tasks)
6985 << getOpenMPClauseName(PrevClause->getClauseKind());
6986 ErrorFound = true;
6987 }
6988 }
6989 }
6990 return ErrorFound;
6991}
6992
Alexey Bataev49f6e782015-12-01 04:18:41 +00006993StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6994 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6995 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006996 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006997 if (!AStmt)
6998 return StmtError();
6999
7000 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7001 OMPLoopDirective::HelperExprs B;
7002 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7003 // define the nested loops number.
7004 unsigned NestedLoopCount =
7005 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007006 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007007 VarsWithImplicitDSA, B);
7008 if (NestedLoopCount == 0)
7009 return StmtError();
7010
7011 assert((CurContext->isDependentContext() || B.builtAll()) &&
7012 "omp for loop exprs were not built");
7013
Alexey Bataev382967a2015-12-08 12:06:20 +00007014 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7015 // The grainsize clause and num_tasks clause are mutually exclusive and may
7016 // not appear on the same taskloop directive.
7017 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7018 return StmtError();
7019
Alexey Bataev49f6e782015-12-01 04:18:41 +00007020 getCurFunction()->setHasBranchProtectedScope();
7021 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7022 NestedLoopCount, Clauses, AStmt, B);
7023}
7024
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007025StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7026 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7027 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007028 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007029 if (!AStmt)
7030 return StmtError();
7031
7032 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7033 OMPLoopDirective::HelperExprs B;
7034 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7035 // define the nested loops number.
7036 unsigned NestedLoopCount =
7037 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7038 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7039 VarsWithImplicitDSA, B);
7040 if (NestedLoopCount == 0)
7041 return StmtError();
7042
7043 assert((CurContext->isDependentContext() || B.builtAll()) &&
7044 "omp for loop exprs were not built");
7045
Alexey Bataev5a3af132016-03-29 08:58:54 +00007046 if (!CurContext->isDependentContext()) {
7047 // Finalize the clauses that need pre-built expressions for CodeGen.
7048 for (auto C : Clauses) {
7049 if (auto LC = dyn_cast<OMPLinearClause>(C))
7050 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007051 B.NumIterations, *this, CurScope,
7052 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007053 return StmtError();
7054 }
7055 }
7056
Alexey Bataev382967a2015-12-08 12:06:20 +00007057 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7058 // The grainsize clause and num_tasks clause are mutually exclusive and may
7059 // not appear on the same taskloop directive.
7060 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7061 return StmtError();
7062
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007063 getCurFunction()->setHasBranchProtectedScope();
7064 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7065 NestedLoopCount, Clauses, AStmt, B);
7066}
7067
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007068StmtResult Sema::ActOnOpenMPDistributeDirective(
7069 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7070 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007071 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007072 if (!AStmt)
7073 return StmtError();
7074
7075 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7076 OMPLoopDirective::HelperExprs B;
7077 // In presence of clause 'collapse' with number of loops, it will
7078 // define the nested loops number.
7079 unsigned NestedLoopCount =
7080 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7081 nullptr /*ordered not a clause on distribute*/, AStmt,
7082 *this, *DSAStack, VarsWithImplicitDSA, B);
7083 if (NestedLoopCount == 0)
7084 return StmtError();
7085
7086 assert((CurContext->isDependentContext() || B.builtAll()) &&
7087 "omp for loop exprs were not built");
7088
7089 getCurFunction()->setHasBranchProtectedScope();
7090 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7091 NestedLoopCount, Clauses, AStmt, B);
7092}
7093
Carlo Bertolli9925f152016-06-27 14:55:37 +00007094StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7095 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7096 SourceLocation EndLoc,
7097 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7098 if (!AStmt)
7099 return StmtError();
7100
7101 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7102 // 1.2.2 OpenMP Language Terminology
7103 // Structured block - An executable statement with a single entry at the
7104 // top and a single exit at the bottom.
7105 // The point of exit cannot be a branch out of the structured block.
7106 // longjmp() and throw() must not violate the entry/exit criteria.
7107 CS->getCapturedDecl()->setNothrow();
7108
7109 OMPLoopDirective::HelperExprs B;
7110 // In presence of clause 'collapse' with number of loops, it will
7111 // define the nested loops number.
7112 unsigned NestedLoopCount = CheckOpenMPLoop(
7113 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7114 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7115 VarsWithImplicitDSA, B);
7116 if (NestedLoopCount == 0)
7117 return StmtError();
7118
7119 assert((CurContext->isDependentContext() || B.builtAll()) &&
7120 "omp for loop exprs were not built");
7121
7122 getCurFunction()->setHasBranchProtectedScope();
7123 return OMPDistributeParallelForDirective::Create(
7124 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7125}
7126
Kelvin Li4a39add2016-07-05 05:00:15 +00007127StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7128 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7129 SourceLocation EndLoc,
7130 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7131 if (!AStmt)
7132 return StmtError();
7133
7134 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7135 // 1.2.2 OpenMP Language Terminology
7136 // Structured block - An executable statement with a single entry at the
7137 // top and a single exit at the bottom.
7138 // The point of exit cannot be a branch out of the structured block.
7139 // longjmp() and throw() must not violate the entry/exit criteria.
7140 CS->getCapturedDecl()->setNothrow();
7141
7142 OMPLoopDirective::HelperExprs B;
7143 // In presence of clause 'collapse' with number of loops, it will
7144 // define the nested loops number.
7145 unsigned NestedLoopCount = CheckOpenMPLoop(
7146 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7147 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7148 VarsWithImplicitDSA, B);
7149 if (NestedLoopCount == 0)
7150 return StmtError();
7151
7152 assert((CurContext->isDependentContext() || B.builtAll()) &&
7153 "omp for loop exprs were not built");
7154
7155 getCurFunction()->setHasBranchProtectedScope();
7156 return OMPDistributeParallelForSimdDirective::Create(
7157 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7158}
7159
Kelvin Li787f3fc2016-07-06 04:45:38 +00007160StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7161 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7162 SourceLocation EndLoc,
7163 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7164 if (!AStmt)
7165 return StmtError();
7166
7167 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7168 // 1.2.2 OpenMP Language Terminology
7169 // Structured block - An executable statement with a single entry at the
7170 // top and a single exit at the bottom.
7171 // The point of exit cannot be a branch out of the structured block.
7172 // longjmp() and throw() must not violate the entry/exit criteria.
7173 CS->getCapturedDecl()->setNothrow();
7174
7175 OMPLoopDirective::HelperExprs B;
7176 // In presence of clause 'collapse' with number of loops, it will
7177 // define the nested loops number.
7178 unsigned NestedLoopCount =
7179 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7180 nullptr /*ordered not a clause on distribute*/, AStmt,
7181 *this, *DSAStack, VarsWithImplicitDSA, B);
7182 if (NestedLoopCount == 0)
7183 return StmtError();
7184
7185 assert((CurContext->isDependentContext() || B.builtAll()) &&
7186 "omp for loop exprs were not built");
7187
7188 getCurFunction()->setHasBranchProtectedScope();
7189 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7190 NestedLoopCount, Clauses, AStmt, B);
7191}
7192
Alexey Bataeved09d242014-05-28 05:53:51 +00007193OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007194 SourceLocation StartLoc,
7195 SourceLocation LParenLoc,
7196 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007197 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007198 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007199 case OMPC_final:
7200 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7201 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007202 case OMPC_num_threads:
7203 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7204 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007205 case OMPC_safelen:
7206 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7207 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007208 case OMPC_simdlen:
7209 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7210 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007211 case OMPC_collapse:
7212 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7213 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007214 case OMPC_ordered:
7215 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7216 break;
Michael Wonge710d542015-08-07 16:16:36 +00007217 case OMPC_device:
7218 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7219 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007220 case OMPC_num_teams:
7221 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7222 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007223 case OMPC_thread_limit:
7224 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7225 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007226 case OMPC_priority:
7227 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7228 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007229 case OMPC_grainsize:
7230 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7231 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007232 case OMPC_num_tasks:
7233 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7234 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007235 case OMPC_hint:
7236 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7237 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007238 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007239 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007240 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007241 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007242 case OMPC_private:
7243 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007244 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007245 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007246 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007247 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007248 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007249 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007250 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007251 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007252 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007253 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007254 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007255 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007256 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007257 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007258 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007259 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007260 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007261 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007262 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007263 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007264 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007265 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007266 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007267 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007268 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007269 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007270 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007271 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007272 case OMPC_use_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007273 llvm_unreachable("Clause is not allowed.");
7274 }
7275 return Res;
7276}
7277
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007278OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7279 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007280 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007281 SourceLocation NameModifierLoc,
7282 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007283 SourceLocation EndLoc) {
7284 Expr *ValExpr = Condition;
7285 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7286 !Condition->isInstantiationDependent() &&
7287 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007288 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007289 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007290 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007291
Richard Smith03a4aa32016-06-23 19:02:52 +00007292 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007293 }
7294
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007295 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7296 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007297}
7298
Alexey Bataev3778b602014-07-17 07:32:53 +00007299OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7300 SourceLocation StartLoc,
7301 SourceLocation LParenLoc,
7302 SourceLocation EndLoc) {
7303 Expr *ValExpr = Condition;
7304 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7305 !Condition->isInstantiationDependent() &&
7306 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007307 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007308 if (Val.isInvalid())
7309 return nullptr;
7310
Richard Smith03a4aa32016-06-23 19:02:52 +00007311 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007312 }
7313
7314 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7315}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007316ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7317 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007318 if (!Op)
7319 return ExprError();
7320
7321 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7322 public:
7323 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007324 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007325 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7326 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007327 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7328 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007329 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7330 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007331 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7332 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007333 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7334 QualType T,
7335 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007336 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7337 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007338 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7339 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007340 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007341 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007342 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007343 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7344 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007345 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7346 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007347 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7348 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007349 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007350 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007351 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007352 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7353 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007354 llvm_unreachable("conversion functions are permitted");
7355 }
7356 } ConvertDiagnoser;
7357 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7358}
7359
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007360static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007361 OpenMPClauseKind CKind,
7362 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007363 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7364 !ValExpr->isInstantiationDependent()) {
7365 SourceLocation Loc = ValExpr->getExprLoc();
7366 ExprResult Value =
7367 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7368 if (Value.isInvalid())
7369 return false;
7370
7371 ValExpr = Value.get();
7372 // The expression must evaluate to a non-negative integer value.
7373 llvm::APSInt Result;
7374 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007375 Result.isSigned() &&
7376 !((!StrictlyPositive && Result.isNonNegative()) ||
7377 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007378 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007379 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7380 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007381 return false;
7382 }
7383 }
7384 return true;
7385}
7386
Alexey Bataev568a8332014-03-06 06:15:19 +00007387OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7388 SourceLocation StartLoc,
7389 SourceLocation LParenLoc,
7390 SourceLocation EndLoc) {
7391 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007392
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007393 // OpenMP [2.5, Restrictions]
7394 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007395 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7396 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007397 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007398
Alexey Bataeved09d242014-05-28 05:53:51 +00007399 return new (Context)
7400 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007401}
7402
Alexey Bataev62c87d22014-03-21 04:51:18 +00007403ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007404 OpenMPClauseKind CKind,
7405 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007406 if (!E)
7407 return ExprError();
7408 if (E->isValueDependent() || E->isTypeDependent() ||
7409 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007410 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007411 llvm::APSInt Result;
7412 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7413 if (ICE.isInvalid())
7414 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007415 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7416 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007417 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007418 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7419 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007420 return ExprError();
7421 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007422 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7423 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7424 << E->getSourceRange();
7425 return ExprError();
7426 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007427 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7428 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007429 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007430 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007431 return ICE;
7432}
7433
7434OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7435 SourceLocation LParenLoc,
7436 SourceLocation EndLoc) {
7437 // OpenMP [2.8.1, simd construct, Description]
7438 // The parameter of the safelen clause must be a constant
7439 // positive integer expression.
7440 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7441 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007442 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007443 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007444 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007445}
7446
Alexey Bataev66b15b52015-08-21 11:14:16 +00007447OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7448 SourceLocation LParenLoc,
7449 SourceLocation EndLoc) {
7450 // OpenMP [2.8.1, simd construct, Description]
7451 // The parameter of the simdlen clause must be a constant
7452 // positive integer expression.
7453 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7454 if (Simdlen.isInvalid())
7455 return nullptr;
7456 return new (Context)
7457 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7458}
7459
Alexander Musman64d33f12014-06-04 07:53:32 +00007460OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7461 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007462 SourceLocation LParenLoc,
7463 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007464 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007465 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007466 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007467 // The parameter of the collapse clause must be a constant
7468 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007469 ExprResult NumForLoopsResult =
7470 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7471 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007472 return nullptr;
7473 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007474 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007475}
7476
Alexey Bataev10e775f2015-07-30 11:36:16 +00007477OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7478 SourceLocation EndLoc,
7479 SourceLocation LParenLoc,
7480 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007481 // OpenMP [2.7.1, loop construct, Description]
7482 // OpenMP [2.8.1, simd construct, Description]
7483 // OpenMP [2.9.6, distribute construct, Description]
7484 // The parameter of the ordered clause must be a constant
7485 // positive integer expression if any.
7486 if (NumForLoops && LParenLoc.isValid()) {
7487 ExprResult NumForLoopsResult =
7488 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7489 if (NumForLoopsResult.isInvalid())
7490 return nullptr;
7491 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007492 } else
7493 NumForLoops = nullptr;
7494 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007495 return new (Context)
7496 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7497}
7498
Alexey Bataeved09d242014-05-28 05:53:51 +00007499OMPClause *Sema::ActOnOpenMPSimpleClause(
7500 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7501 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007502 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007503 switch (Kind) {
7504 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007505 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007506 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7507 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007508 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007509 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007510 Res = ActOnOpenMPProcBindClause(
7511 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7512 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007513 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007514 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007515 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007516 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007517 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007518 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007519 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007520 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007521 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007522 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007523 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007524 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007525 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007526 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007527 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007528 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007529 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007530 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007531 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007532 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007533 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007534 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007535 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007536 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007537 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007538 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007539 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007540 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007541 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007542 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007543 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007544 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007545 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007546 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007547 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007548 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007549 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007550 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007551 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007552 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007553 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007554 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007555 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007556 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007557 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007558 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007559 case OMPC_use_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007560 llvm_unreachable("Clause is not allowed.");
7561 }
7562 return Res;
7563}
7564
Alexey Bataev6402bca2015-12-28 07:25:51 +00007565static std::string
7566getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7567 ArrayRef<unsigned> Exclude = llvm::None) {
7568 std::string Values;
7569 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7570 unsigned Skipped = Exclude.size();
7571 auto S = Exclude.begin(), E = Exclude.end();
7572 for (unsigned i = First; i < Last; ++i) {
7573 if (std::find(S, E, i) != E) {
7574 --Skipped;
7575 continue;
7576 }
7577 Values += "'";
7578 Values += getOpenMPSimpleClauseTypeName(K, i);
7579 Values += "'";
7580 if (i == Bound - Skipped)
7581 Values += " or ";
7582 else if (i != Bound + 1 - Skipped)
7583 Values += ", ";
7584 }
7585 return Values;
7586}
7587
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007588OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7589 SourceLocation KindKwLoc,
7590 SourceLocation StartLoc,
7591 SourceLocation LParenLoc,
7592 SourceLocation EndLoc) {
7593 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007594 static_assert(OMPC_DEFAULT_unknown > 0,
7595 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007596 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007597 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7598 /*Last=*/OMPC_DEFAULT_unknown)
7599 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007600 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007601 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007602 switch (Kind) {
7603 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007604 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007605 break;
7606 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007607 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007608 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007609 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007610 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007611 break;
7612 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007613 return new (Context)
7614 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007615}
7616
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007617OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7618 SourceLocation KindKwLoc,
7619 SourceLocation StartLoc,
7620 SourceLocation LParenLoc,
7621 SourceLocation EndLoc) {
7622 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007623 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007624 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7625 /*Last=*/OMPC_PROC_BIND_unknown)
7626 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007627 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007628 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007629 return new (Context)
7630 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007631}
7632
Alexey Bataev56dafe82014-06-20 07:16:17 +00007633OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007634 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007635 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007636 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007637 SourceLocation EndLoc) {
7638 OMPClause *Res = nullptr;
7639 switch (Kind) {
7640 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007641 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7642 assert(Argument.size() == NumberOfElements &&
7643 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007644 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007645 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7646 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7647 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7648 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7649 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007650 break;
7651 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007652 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7653 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7654 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7655 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007656 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007657 case OMPC_dist_schedule:
7658 Res = ActOnOpenMPDistScheduleClause(
7659 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7660 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7661 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007662 case OMPC_defaultmap:
7663 enum { Modifier, DefaultmapKind };
7664 Res = ActOnOpenMPDefaultmapClause(
7665 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7666 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7667 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7668 ArgumentLoc[DefaultmapKind], EndLoc);
7669 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007670 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007671 case OMPC_num_threads:
7672 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007673 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007674 case OMPC_collapse:
7675 case OMPC_default:
7676 case OMPC_proc_bind:
7677 case OMPC_private:
7678 case OMPC_firstprivate:
7679 case OMPC_lastprivate:
7680 case OMPC_shared:
7681 case OMPC_reduction:
7682 case OMPC_linear:
7683 case OMPC_aligned:
7684 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007685 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007686 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007687 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007688 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007689 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007690 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007691 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007692 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007693 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007694 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007695 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007696 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007697 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007698 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007699 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007700 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007701 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007702 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007703 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007704 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007705 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007706 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007707 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007708 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007709 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007710 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007711 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007712 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007713 case OMPC_use_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007714 llvm_unreachable("Clause is not allowed.");
7715 }
7716 return Res;
7717}
7718
Alexey Bataev6402bca2015-12-28 07:25:51 +00007719static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7720 OpenMPScheduleClauseModifier M2,
7721 SourceLocation M1Loc, SourceLocation M2Loc) {
7722 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7723 SmallVector<unsigned, 2> Excluded;
7724 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7725 Excluded.push_back(M2);
7726 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7727 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7728 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7729 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7730 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7731 << getListOfPossibleValues(OMPC_schedule,
7732 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7733 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7734 Excluded)
7735 << getOpenMPClauseName(OMPC_schedule);
7736 return true;
7737 }
7738 return false;
7739}
7740
Alexey Bataev56dafe82014-06-20 07:16:17 +00007741OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007742 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007743 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007744 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7745 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7746 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7747 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7748 return nullptr;
7749 // OpenMP, 2.7.1, Loop Construct, Restrictions
7750 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7751 // but not both.
7752 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7753 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7754 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7755 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7756 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7757 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7758 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7759 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7760 return nullptr;
7761 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007762 if (Kind == OMPC_SCHEDULE_unknown) {
7763 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007764 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7765 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7766 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7767 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7768 Exclude);
7769 } else {
7770 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7771 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007772 }
7773 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7774 << Values << getOpenMPClauseName(OMPC_schedule);
7775 return nullptr;
7776 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007777 // OpenMP, 2.7.1, Loop Construct, Restrictions
7778 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7779 // schedule(guided).
7780 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7781 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7782 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7783 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7784 diag::err_omp_schedule_nonmonotonic_static);
7785 return nullptr;
7786 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007787 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007788 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007789 if (ChunkSize) {
7790 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7791 !ChunkSize->isInstantiationDependent() &&
7792 !ChunkSize->containsUnexpandedParameterPack()) {
7793 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7794 ExprResult Val =
7795 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7796 if (Val.isInvalid())
7797 return nullptr;
7798
7799 ValExpr = Val.get();
7800
7801 // OpenMP [2.7.1, Restrictions]
7802 // chunk_size must be a loop invariant integer expression with a positive
7803 // value.
7804 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007805 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7806 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7807 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007808 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007809 return nullptr;
7810 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007811 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7812 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007813 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7814 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7815 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007816 }
7817 }
7818 }
7819
Alexey Bataev6402bca2015-12-28 07:25:51 +00007820 return new (Context)
7821 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007822 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007823}
7824
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007825OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7826 SourceLocation StartLoc,
7827 SourceLocation EndLoc) {
7828 OMPClause *Res = nullptr;
7829 switch (Kind) {
7830 case OMPC_ordered:
7831 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7832 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007833 case OMPC_nowait:
7834 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7835 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007836 case OMPC_untied:
7837 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7838 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007839 case OMPC_mergeable:
7840 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7841 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007842 case OMPC_read:
7843 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7844 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007845 case OMPC_write:
7846 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7847 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007848 case OMPC_update:
7849 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7850 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007851 case OMPC_capture:
7852 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7853 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007854 case OMPC_seq_cst:
7855 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7856 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007857 case OMPC_threads:
7858 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7859 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007860 case OMPC_simd:
7861 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7862 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007863 case OMPC_nogroup:
7864 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7865 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007866 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007867 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007868 case OMPC_num_threads:
7869 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007870 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007871 case OMPC_collapse:
7872 case OMPC_schedule:
7873 case OMPC_private:
7874 case OMPC_firstprivate:
7875 case OMPC_lastprivate:
7876 case OMPC_shared:
7877 case OMPC_reduction:
7878 case OMPC_linear:
7879 case OMPC_aligned:
7880 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007881 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007882 case OMPC_default:
7883 case OMPC_proc_bind:
7884 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007885 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007886 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007887 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007888 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007889 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007890 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007891 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007892 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007893 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007894 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007895 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007896 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007897 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007898 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007899 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007900 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007901 case OMPC_use_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007902 llvm_unreachable("Clause is not allowed.");
7903 }
7904 return Res;
7905}
7906
Alexey Bataev236070f2014-06-20 11:19:47 +00007907OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7908 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007909 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007910 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7911}
7912
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007913OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7914 SourceLocation EndLoc) {
7915 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7916}
7917
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007918OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7919 SourceLocation EndLoc) {
7920 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7921}
7922
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007923OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7924 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007925 return new (Context) OMPReadClause(StartLoc, EndLoc);
7926}
7927
Alexey Bataevdea47612014-07-23 07:46:59 +00007928OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7929 SourceLocation EndLoc) {
7930 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7931}
7932
Alexey Bataev67a4f222014-07-23 10:25:33 +00007933OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7934 SourceLocation EndLoc) {
7935 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7936}
7937
Alexey Bataev459dec02014-07-24 06:46:57 +00007938OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7939 SourceLocation EndLoc) {
7940 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7941}
7942
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007943OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7944 SourceLocation EndLoc) {
7945 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7946}
7947
Alexey Bataev346265e2015-09-25 10:37:12 +00007948OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7949 SourceLocation EndLoc) {
7950 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7951}
7952
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007953OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7954 SourceLocation EndLoc) {
7955 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7956}
7957
Alexey Bataevb825de12015-12-07 10:51:44 +00007958OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7959 SourceLocation EndLoc) {
7960 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7961}
7962
Alexey Bataevc5e02582014-06-16 07:08:35 +00007963OMPClause *Sema::ActOnOpenMPVarListClause(
7964 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7965 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7966 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007967 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007968 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7969 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7970 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007971 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007972 switch (Kind) {
7973 case OMPC_private:
7974 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7975 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007976 case OMPC_firstprivate:
7977 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7978 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007979 case OMPC_lastprivate:
7980 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7981 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007982 case OMPC_shared:
7983 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7984 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007985 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007986 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7987 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007988 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007989 case OMPC_linear:
7990 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007991 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007992 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007993 case OMPC_aligned:
7994 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7995 ColonLoc, EndLoc);
7996 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007997 case OMPC_copyin:
7998 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7999 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008000 case OMPC_copyprivate:
8001 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8002 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008003 case OMPC_flush:
8004 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8005 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008006 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008007 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8008 StartLoc, LParenLoc, EndLoc);
8009 break;
8010 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008011 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8012 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8013 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008014 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008015 case OMPC_to:
8016 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8017 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008018 case OMPC_from:
8019 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8020 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008021 case OMPC_use_device_ptr:
8022 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8023 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008024 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008025 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008026 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008027 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008028 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008029 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008030 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008031 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008032 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008033 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008034 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008035 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008036 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008037 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008038 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008039 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008040 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008041 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008042 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008043 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008044 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008045 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008046 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008047 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008048 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008049 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008050 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008051 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008052 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008053 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008054 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008055 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008056 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008057 llvm_unreachable("Clause is not allowed.");
8058 }
8059 return Res;
8060}
8061
Alexey Bataev90c228f2016-02-08 09:29:13 +00008062ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008063 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008064 ExprResult Res = BuildDeclRefExpr(
8065 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8066 if (!Res.isUsable())
8067 return ExprError();
8068 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8069 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8070 if (!Res.isUsable())
8071 return ExprError();
8072 }
8073 if (VK != VK_LValue && Res.get()->isGLValue()) {
8074 Res = DefaultLvalueConversion(Res.get());
8075 if (!Res.isUsable())
8076 return ExprError();
8077 }
8078 return Res;
8079}
8080
Alexey Bataev60da77e2016-02-29 05:54:20 +00008081static std::pair<ValueDecl *, bool>
8082getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8083 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008084 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8085 RefExpr->containsUnexpandedParameterPack())
8086 return std::make_pair(nullptr, true);
8087
Alexey Bataevd985eda2016-02-10 11:29:16 +00008088 // OpenMP [3.1, C/C++]
8089 // A list item is a variable name.
8090 // OpenMP [2.9.3.3, Restrictions, p.1]
8091 // A variable that is part of another variable (as an array or
8092 // structure element) cannot appear in a private clause.
8093 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008094 enum {
8095 NoArrayExpr = -1,
8096 ArraySubscript = 0,
8097 OMPArraySection = 1
8098 } IsArrayExpr = NoArrayExpr;
8099 if (AllowArraySection) {
8100 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8101 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8102 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8103 Base = TempASE->getBase()->IgnoreParenImpCasts();
8104 RefExpr = Base;
8105 IsArrayExpr = ArraySubscript;
8106 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8107 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8108 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8109 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8110 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8111 Base = TempASE->getBase()->IgnoreParenImpCasts();
8112 RefExpr = Base;
8113 IsArrayExpr = OMPArraySection;
8114 }
8115 }
8116 ELoc = RefExpr->getExprLoc();
8117 ERange = RefExpr->getSourceRange();
8118 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008119 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8120 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8121 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8122 (S.getCurrentThisType().isNull() || !ME ||
8123 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8124 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008125 if (IsArrayExpr != NoArrayExpr)
8126 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8127 << ERange;
8128 else {
8129 S.Diag(ELoc,
8130 AllowArraySection
8131 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8132 : diag::err_omp_expected_var_name_member_expr)
8133 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8134 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008135 return std::make_pair(nullptr, false);
8136 }
8137 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8138}
8139
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008140OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8141 SourceLocation StartLoc,
8142 SourceLocation LParenLoc,
8143 SourceLocation EndLoc) {
8144 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008145 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008146 for (auto &RefExpr : VarList) {
8147 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008148 SourceLocation ELoc;
8149 SourceRange ERange;
8150 Expr *SimpleRefExpr = RefExpr;
8151 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008152 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008153 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008154 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008155 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008156 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008157 ValueDecl *D = Res.first;
8158 if (!D)
8159 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008160
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008161 QualType Type = D->getType();
8162 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008163
8164 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8165 // A variable that appears in a private clause must not have an incomplete
8166 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008167 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008168 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008169 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008170
Alexey Bataev758e55e2013-09-06 18:03:48 +00008171 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8172 // in a Construct]
8173 // Variables with the predetermined data-sharing attributes may not be
8174 // listed in data-sharing attributes clauses, except for the cases
8175 // listed below. For these exceptions only, listing a predetermined
8176 // variable in a data-sharing attribute clause is allowed and overrides
8177 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008178 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008179 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008180 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8181 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008182 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008183 continue;
8184 }
8185
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008186 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008187 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008188 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008189 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8190 << getOpenMPClauseName(OMPC_private) << Type
8191 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8192 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008193 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008194 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008195 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008196 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008197 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008198 continue;
8199 }
8200
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008201 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8202 // A list item cannot appear in both a map clause and a data-sharing
8203 // attribute clause on the same construct
8204 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008205 if (DSAStack->checkMappableExprComponentListsForDecl(
8206 VD, /* CurrentRegionOnly = */ true,
8207 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8208 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008209 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8210 << getOpenMPClauseName(OMPC_private)
8211 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8212 ReportOriginalDSA(*this, DSAStack, D, DVar);
8213 continue;
8214 }
8215 }
8216
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008217 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8218 // A variable of class type (or array thereof) that appears in a private
8219 // clause requires an accessible, unambiguous default constructor for the
8220 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008221 // Generate helper private variable and initialize it with the default
8222 // value. The address of the original variable is replaced by the address of
8223 // the new private variable in CodeGen. This new variable is not added to
8224 // IdResolver, so the code in the OpenMP region uses original variable for
8225 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008226 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008227 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8228 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008229 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008230 if (VDPrivate->isInvalidDecl())
8231 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008232 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008233 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008234
Alexey Bataev90c228f2016-02-08 09:29:13 +00008235 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008236 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008237 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008238 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008239 Vars.push_back((VD || CurContext->isDependentContext())
8240 ? RefExpr->IgnoreParens()
8241 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008242 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008243 }
8244
Alexey Bataeved09d242014-05-28 05:53:51 +00008245 if (Vars.empty())
8246 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008247
Alexey Bataev03b340a2014-10-21 03:16:40 +00008248 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8249 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008250}
8251
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008252namespace {
8253class DiagsUninitializedSeveretyRAII {
8254private:
8255 DiagnosticsEngine &Diags;
8256 SourceLocation SavedLoc;
8257 bool IsIgnored;
8258
8259public:
8260 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8261 bool IsIgnored)
8262 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8263 if (!IsIgnored) {
8264 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8265 /*Map*/ diag::Severity::Ignored, Loc);
8266 }
8267 }
8268 ~DiagsUninitializedSeveretyRAII() {
8269 if (!IsIgnored)
8270 Diags.popMappings(SavedLoc);
8271 }
8272};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008273}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008274
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008275OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8276 SourceLocation StartLoc,
8277 SourceLocation LParenLoc,
8278 SourceLocation EndLoc) {
8279 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008280 SmallVector<Expr *, 8> PrivateCopies;
8281 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008282 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008283 bool IsImplicitClause =
8284 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8285 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8286
Alexey Bataeved09d242014-05-28 05:53:51 +00008287 for (auto &RefExpr : VarList) {
8288 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008289 SourceLocation ELoc;
8290 SourceRange ERange;
8291 Expr *SimpleRefExpr = RefExpr;
8292 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008293 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008294 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008295 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008296 PrivateCopies.push_back(nullptr);
8297 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008298 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008299 ValueDecl *D = Res.first;
8300 if (!D)
8301 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008302
Alexey Bataev60da77e2016-02-29 05:54:20 +00008303 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008304 QualType Type = D->getType();
8305 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008306
8307 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8308 // A variable that appears in a private clause must not have an incomplete
8309 // type or a reference type.
8310 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008311 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008312 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008313 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008314
8315 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8316 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008317 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008318 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008319 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008320
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008321 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008322 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008323 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008324 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008325 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008326 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008327 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8328 // A list item that specifies a given variable may not appear in more
8329 // than one clause on the same directive, except that a variable may be
8330 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008331 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008332 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008333 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008334 << getOpenMPClauseName(DVar.CKind)
8335 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008336 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008337 continue;
8338 }
8339
8340 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8341 // in a Construct]
8342 // Variables with the predetermined data-sharing attributes may not be
8343 // listed in data-sharing attributes clauses, except for the cases
8344 // listed below. For these exceptions only, listing a predetermined
8345 // variable in a data-sharing attribute clause is allowed and overrides
8346 // the variable's predetermined data-sharing attributes.
8347 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8348 // in a Construct, C/C++, p.2]
8349 // Variables with const-qualified type having no mutable member may be
8350 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008351 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008352 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8353 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008354 << getOpenMPClauseName(DVar.CKind)
8355 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008356 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008357 continue;
8358 }
8359
Alexey Bataevf29276e2014-06-18 04:14:57 +00008360 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008361 // OpenMP [2.9.3.4, Restrictions, p.2]
8362 // A list item that is private within a parallel region must not appear
8363 // in a firstprivate clause on a worksharing construct if any of the
8364 // worksharing regions arising from the worksharing construct ever bind
8365 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008366 if (isOpenMPWorksharingDirective(CurrDir) &&
8367 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008368 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008369 if (DVar.CKind != OMPC_shared &&
8370 (isOpenMPParallelDirective(DVar.DKind) ||
8371 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008372 Diag(ELoc, diag::err_omp_required_access)
8373 << getOpenMPClauseName(OMPC_firstprivate)
8374 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008375 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008376 continue;
8377 }
8378 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008379 // OpenMP [2.9.3.4, Restrictions, p.3]
8380 // A list item that appears in a reduction clause of a parallel construct
8381 // must not appear in a firstprivate clause on a worksharing or task
8382 // construct if any of the worksharing or task regions arising from the
8383 // worksharing or task construct ever bind to any of the parallel regions
8384 // arising from the parallel construct.
8385 // OpenMP [2.9.3.4, Restrictions, p.4]
8386 // A list item that appears in a reduction clause in worksharing
8387 // construct must not appear in a firstprivate clause in a task construct
8388 // encountered during execution of any of the worksharing regions arising
8389 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008390 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008391 DVar = DSAStack->hasInnermostDSA(
8392 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8393 [](OpenMPDirectiveKind K) -> bool {
8394 return isOpenMPParallelDirective(K) ||
8395 isOpenMPWorksharingDirective(K);
8396 },
8397 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008398 if (DVar.CKind == OMPC_reduction &&
8399 (isOpenMPParallelDirective(DVar.DKind) ||
8400 isOpenMPWorksharingDirective(DVar.DKind))) {
8401 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8402 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008403 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008404 continue;
8405 }
8406 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008407
8408 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8409 // A list item that is private within a teams region must not appear in a
8410 // firstprivate clause on a distribute construct if any of the distribute
8411 // regions arising from the distribute construct ever bind to any of the
8412 // teams regions arising from the teams construct.
8413 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8414 // A list item that appears in a reduction clause of a teams construct
8415 // must not appear in a firstprivate clause on a distribute construct if
8416 // any of the distribute regions arising from the distribute construct
8417 // ever bind to any of the teams regions arising from the teams construct.
8418 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8419 // A list item may appear in a firstprivate or lastprivate clause but not
8420 // both.
8421 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008422 DVar = DSAStack->hasInnermostDSA(
8423 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8424 [](OpenMPDirectiveKind K) -> bool {
8425 return isOpenMPTeamsDirective(K);
8426 },
8427 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008428 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8429 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008430 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008431 continue;
8432 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008433 DVar = DSAStack->hasInnermostDSA(
8434 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8435 [](OpenMPDirectiveKind K) -> bool {
8436 return isOpenMPTeamsDirective(K);
8437 },
8438 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008439 if (DVar.CKind == OMPC_reduction &&
8440 isOpenMPTeamsDirective(DVar.DKind)) {
8441 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008442 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008443 continue;
8444 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008445 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008446 if (DVar.CKind == OMPC_lastprivate) {
8447 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008448 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008449 continue;
8450 }
8451 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008452 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8453 // A list item cannot appear in both a map clause and a data-sharing
8454 // attribute clause on the same construct
8455 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008456 if (DSAStack->checkMappableExprComponentListsForDecl(
8457 VD, /* CurrentRegionOnly = */ true,
8458 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8459 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008460 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8461 << getOpenMPClauseName(OMPC_firstprivate)
8462 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8463 ReportOriginalDSA(*this, DSAStack, D, DVar);
8464 continue;
8465 }
8466 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008467 }
8468
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008469 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008470 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008471 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008472 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8473 << getOpenMPClauseName(OMPC_firstprivate) << Type
8474 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8475 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008476 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008477 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008478 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008479 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008480 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008481 continue;
8482 }
8483
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008484 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008485 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8486 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008487 // Generate helper private variable and initialize it with the value of the
8488 // original variable. The address of the original variable is replaced by
8489 // the address of the new private variable in the CodeGen. This new variable
8490 // is not added to IdResolver, so the code in the OpenMP region uses
8491 // original variable for proper diagnostics and variable capturing.
8492 Expr *VDInitRefExpr = nullptr;
8493 // For arrays generate initializer for single element and replace it by the
8494 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008495 if (Type->isArrayType()) {
8496 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008497 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008498 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008499 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008500 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008501 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008502 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008503 InitializedEntity Entity =
8504 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008505 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8506
8507 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8508 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8509 if (Result.isInvalid())
8510 VDPrivate->setInvalidDecl();
8511 else
8512 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008513 // Remove temp variable declaration.
8514 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008515 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008516 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8517 ".firstprivate.temp");
8518 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8519 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008520 AddInitializerToDecl(VDPrivate,
8521 DefaultLvalueConversion(VDInitRefExpr).get(),
8522 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008523 }
8524 if (VDPrivate->isInvalidDecl()) {
8525 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008526 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008527 diag::note_omp_task_predetermined_firstprivate_here);
8528 }
8529 continue;
8530 }
8531 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008532 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008533 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8534 RefExpr->getExprLoc());
8535 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008536 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008537 if (TopDVar.CKind == OMPC_lastprivate)
8538 Ref = TopDVar.PrivateCopy;
8539 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008540 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008541 if (!IsOpenMPCapturedDecl(D))
8542 ExprCaptures.push_back(Ref->getDecl());
8543 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008544 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008545 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008546 Vars.push_back((VD || CurContext->isDependentContext())
8547 ? RefExpr->IgnoreParens()
8548 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008549 PrivateCopies.push_back(VDPrivateRefExpr);
8550 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008551 }
8552
Alexey Bataeved09d242014-05-28 05:53:51 +00008553 if (Vars.empty())
8554 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008555
8556 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008557 Vars, PrivateCopies, Inits,
8558 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008559}
8560
Alexander Musman1bb328c2014-06-04 13:06:39 +00008561OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8562 SourceLocation StartLoc,
8563 SourceLocation LParenLoc,
8564 SourceLocation EndLoc) {
8565 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008566 SmallVector<Expr *, 8> SrcExprs;
8567 SmallVector<Expr *, 8> DstExprs;
8568 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008569 SmallVector<Decl *, 4> ExprCaptures;
8570 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008571 for (auto &RefExpr : VarList) {
8572 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008573 SourceLocation ELoc;
8574 SourceRange ERange;
8575 Expr *SimpleRefExpr = RefExpr;
8576 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008577 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008578 // It will be analyzed later.
8579 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008580 SrcExprs.push_back(nullptr);
8581 DstExprs.push_back(nullptr);
8582 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008583 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008584 ValueDecl *D = Res.first;
8585 if (!D)
8586 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008587
Alexey Bataev74caaf22016-02-20 04:09:36 +00008588 QualType Type = D->getType();
8589 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008590
8591 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8592 // A variable that appears in a lastprivate clause must not have an
8593 // incomplete type or a reference type.
8594 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008595 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008596 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008597 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008598
8599 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8600 // in a Construct]
8601 // Variables with the predetermined data-sharing attributes may not be
8602 // listed in data-sharing attributes clauses, except for the cases
8603 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008604 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008605 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8606 DVar.CKind != OMPC_firstprivate &&
8607 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8608 Diag(ELoc, diag::err_omp_wrong_dsa)
8609 << getOpenMPClauseName(DVar.CKind)
8610 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008611 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008612 continue;
8613 }
8614
Alexey Bataevf29276e2014-06-18 04:14:57 +00008615 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8616 // OpenMP [2.14.3.5, Restrictions, p.2]
8617 // A list item that is private within a parallel region, or that appears in
8618 // the reduction clause of a parallel construct, must not appear in a
8619 // lastprivate clause on a worksharing construct if any of the corresponding
8620 // worksharing regions ever binds to any of the corresponding parallel
8621 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008622 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008623 if (isOpenMPWorksharingDirective(CurrDir) &&
8624 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008625 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008626 if (DVar.CKind != OMPC_shared) {
8627 Diag(ELoc, diag::err_omp_required_access)
8628 << getOpenMPClauseName(OMPC_lastprivate)
8629 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008630 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008631 continue;
8632 }
8633 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008634
8635 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8636 // A list item may appear in a firstprivate or lastprivate clause but not
8637 // both.
8638 if (CurrDir == OMPD_distribute) {
8639 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8640 if (DVar.CKind == OMPC_firstprivate) {
8641 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8642 ReportOriginalDSA(*this, DSAStack, D, DVar);
8643 continue;
8644 }
8645 }
8646
Alexander Musman1bb328c2014-06-04 13:06:39 +00008647 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008648 // A variable of class type (or array thereof) that appears in a
8649 // lastprivate clause requires an accessible, unambiguous default
8650 // constructor for the class type, unless the list item is also specified
8651 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008652 // A variable of class type (or array thereof) that appears in a
8653 // lastprivate clause requires an accessible, unambiguous copy assignment
8654 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008655 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008656 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008657 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008658 D->hasAttrs() ? &D->getAttrs() : nullptr);
8659 auto *PseudoSrcExpr =
8660 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008661 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008662 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008663 D->hasAttrs() ? &D->getAttrs() : nullptr);
8664 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008665 // For arrays generate assignment operation for single element and replace
8666 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008667 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008668 PseudoDstExpr, PseudoSrcExpr);
8669 if (AssignmentOp.isInvalid())
8670 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008671 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008672 /*DiscardedValue=*/true);
8673 if (AssignmentOp.isInvalid())
8674 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008675
Alexey Bataev74caaf22016-02-20 04:09:36 +00008676 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008677 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008678 if (TopDVar.CKind == OMPC_firstprivate)
8679 Ref = TopDVar.PrivateCopy;
8680 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008681 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008682 if (!IsOpenMPCapturedDecl(D))
8683 ExprCaptures.push_back(Ref->getDecl());
8684 }
8685 if (TopDVar.CKind == OMPC_firstprivate ||
8686 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008687 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008688 ExprResult RefRes = DefaultLvalueConversion(Ref);
8689 if (!RefRes.isUsable())
8690 continue;
8691 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008692 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8693 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008694 if (!PostUpdateRes.isUsable())
8695 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008696 ExprPostUpdates.push_back(
8697 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008698 }
8699 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008700 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008701 Vars.push_back((VD || CurContext->isDependentContext())
8702 ? RefExpr->IgnoreParens()
8703 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008704 SrcExprs.push_back(PseudoSrcExpr);
8705 DstExprs.push_back(PseudoDstExpr);
8706 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008707 }
8708
8709 if (Vars.empty())
8710 return nullptr;
8711
8712 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008713 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008714 buildPreInits(Context, ExprCaptures),
8715 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008716}
8717
Alexey Bataev758e55e2013-09-06 18:03:48 +00008718OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8719 SourceLocation StartLoc,
8720 SourceLocation LParenLoc,
8721 SourceLocation EndLoc) {
8722 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008723 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008724 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008725 SourceLocation ELoc;
8726 SourceRange ERange;
8727 Expr *SimpleRefExpr = RefExpr;
8728 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008729 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008730 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008731 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008732 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008733 ValueDecl *D = Res.first;
8734 if (!D)
8735 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008736
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008737 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008738 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8739 // in a Construct]
8740 // Variables with the predetermined data-sharing attributes may not be
8741 // listed in data-sharing attributes clauses, except for the cases
8742 // listed below. For these exceptions only, listing a predetermined
8743 // variable in a data-sharing attribute clause is allowed and overrides
8744 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008745 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008746 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8747 DVar.RefExpr) {
8748 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8749 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008750 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008751 continue;
8752 }
8753
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008754 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008755 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008756 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008757 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008758 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8759 ? RefExpr->IgnoreParens()
8760 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008761 }
8762
Alexey Bataeved09d242014-05-28 05:53:51 +00008763 if (Vars.empty())
8764 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008765
8766 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8767}
8768
Alexey Bataevc5e02582014-06-16 07:08:35 +00008769namespace {
8770class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8771 DSAStackTy *Stack;
8772
8773public:
8774 bool VisitDeclRefExpr(DeclRefExpr *E) {
8775 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008776 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008777 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8778 return false;
8779 if (DVar.CKind != OMPC_unknown)
8780 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008781 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8782 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8783 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008784 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008785 return true;
8786 return false;
8787 }
8788 return false;
8789 }
8790 bool VisitStmt(Stmt *S) {
8791 for (auto Child : S->children()) {
8792 if (Child && Visit(Child))
8793 return true;
8794 }
8795 return false;
8796 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008797 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008798};
Alexey Bataev23b69422014-06-18 07:08:49 +00008799} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008800
Alexey Bataev60da77e2016-02-29 05:54:20 +00008801namespace {
8802// Transform MemberExpression for specified FieldDecl of current class to
8803// DeclRefExpr to specified OMPCapturedExprDecl.
8804class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8805 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8806 ValueDecl *Field;
8807 DeclRefExpr *CapturedExpr;
8808
8809public:
8810 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8811 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8812
8813 ExprResult TransformMemberExpr(MemberExpr *E) {
8814 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8815 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008816 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008817 return CapturedExpr;
8818 }
8819 return BaseTransform::TransformMemberExpr(E);
8820 }
8821 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8822};
8823} // namespace
8824
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008825template <typename T>
8826static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8827 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8828 for (auto &Set : Lookups) {
8829 for (auto *D : Set) {
8830 if (auto Res = Gen(cast<ValueDecl>(D)))
8831 return Res;
8832 }
8833 }
8834 return T();
8835}
8836
8837static ExprResult
8838buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8839 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8840 const DeclarationNameInfo &ReductionId, QualType Ty,
8841 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8842 if (ReductionIdScopeSpec.isInvalid())
8843 return ExprError();
8844 SmallVector<UnresolvedSet<8>, 4> Lookups;
8845 if (S) {
8846 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8847 Lookup.suppressDiagnostics();
8848 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8849 auto *D = Lookup.getRepresentativeDecl();
8850 do {
8851 S = S->getParent();
8852 } while (S && !S->isDeclScope(D));
8853 if (S)
8854 S = S->getParent();
8855 Lookups.push_back(UnresolvedSet<8>());
8856 Lookups.back().append(Lookup.begin(), Lookup.end());
8857 Lookup.clear();
8858 }
8859 } else if (auto *ULE =
8860 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8861 Lookups.push_back(UnresolvedSet<8>());
8862 Decl *PrevD = nullptr;
8863 for(auto *D : ULE->decls()) {
8864 if (D == PrevD)
8865 Lookups.push_back(UnresolvedSet<8>());
8866 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8867 Lookups.back().addDecl(DRD);
8868 PrevD = D;
8869 }
8870 }
8871 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8872 Ty->containsUnexpandedParameterPack() ||
8873 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8874 return !D->isInvalidDecl() &&
8875 (D->getType()->isDependentType() ||
8876 D->getType()->isInstantiationDependentType() ||
8877 D->getType()->containsUnexpandedParameterPack());
8878 })) {
8879 UnresolvedSet<8> ResSet;
8880 for (auto &Set : Lookups) {
8881 ResSet.append(Set.begin(), Set.end());
8882 // The last item marks the end of all declarations at the specified scope.
8883 ResSet.addDecl(Set[Set.size() - 1]);
8884 }
8885 return UnresolvedLookupExpr::Create(
8886 SemaRef.Context, /*NamingClass=*/nullptr,
8887 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8888 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8889 }
8890 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8891 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8892 if (!D->isInvalidDecl() &&
8893 SemaRef.Context.hasSameType(D->getType(), Ty))
8894 return D;
8895 return nullptr;
8896 }))
8897 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8898 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8899 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8900 if (!D->isInvalidDecl() &&
8901 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8902 !Ty.isMoreQualifiedThan(D->getType()))
8903 return D;
8904 return nullptr;
8905 })) {
8906 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8907 /*DetectVirtual=*/false);
8908 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8909 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8910 VD->getType().getUnqualifiedType()))) {
8911 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8912 /*DiagID=*/0) !=
8913 Sema::AR_inaccessible) {
8914 SemaRef.BuildBasePathArray(Paths, BasePath);
8915 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8916 }
8917 }
8918 }
8919 }
8920 if (ReductionIdScopeSpec.isSet()) {
8921 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8922 return ExprError();
8923 }
8924 return ExprEmpty();
8925}
8926
Alexey Bataevc5e02582014-06-16 07:08:35 +00008927OMPClause *Sema::ActOnOpenMPReductionClause(
8928 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8929 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008930 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8931 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008932 auto DN = ReductionId.getName();
8933 auto OOK = DN.getCXXOverloadedOperator();
8934 BinaryOperatorKind BOK = BO_Comma;
8935
8936 // OpenMP [2.14.3.6, reduction clause]
8937 // C
8938 // reduction-identifier is either an identifier or one of the following
8939 // operators: +, -, *, &, |, ^, && and ||
8940 // C++
8941 // reduction-identifier is either an id-expression or one of the following
8942 // operators: +, -, *, &, |, ^, && and ||
8943 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8944 switch (OOK) {
8945 case OO_Plus:
8946 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008947 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008948 break;
8949 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008950 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008951 break;
8952 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008953 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008954 break;
8955 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008956 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008957 break;
8958 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008959 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008960 break;
8961 case OO_AmpAmp:
8962 BOK = BO_LAnd;
8963 break;
8964 case OO_PipePipe:
8965 BOK = BO_LOr;
8966 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008967 case OO_New:
8968 case OO_Delete:
8969 case OO_Array_New:
8970 case OO_Array_Delete:
8971 case OO_Slash:
8972 case OO_Percent:
8973 case OO_Tilde:
8974 case OO_Exclaim:
8975 case OO_Equal:
8976 case OO_Less:
8977 case OO_Greater:
8978 case OO_LessEqual:
8979 case OO_GreaterEqual:
8980 case OO_PlusEqual:
8981 case OO_MinusEqual:
8982 case OO_StarEqual:
8983 case OO_SlashEqual:
8984 case OO_PercentEqual:
8985 case OO_CaretEqual:
8986 case OO_AmpEqual:
8987 case OO_PipeEqual:
8988 case OO_LessLess:
8989 case OO_GreaterGreater:
8990 case OO_LessLessEqual:
8991 case OO_GreaterGreaterEqual:
8992 case OO_EqualEqual:
8993 case OO_ExclaimEqual:
8994 case OO_PlusPlus:
8995 case OO_MinusMinus:
8996 case OO_Comma:
8997 case OO_ArrowStar:
8998 case OO_Arrow:
8999 case OO_Call:
9000 case OO_Subscript:
9001 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009002 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009003 case NUM_OVERLOADED_OPERATORS:
9004 llvm_unreachable("Unexpected reduction identifier");
9005 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009006 if (auto II = DN.getAsIdentifierInfo()) {
9007 if (II->isStr("max"))
9008 BOK = BO_GT;
9009 else if (II->isStr("min"))
9010 BOK = BO_LT;
9011 }
9012 break;
9013 }
9014 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009015 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009016 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009017 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009018
9019 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009020 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009021 SmallVector<Expr *, 8> LHSs;
9022 SmallVector<Expr *, 8> RHSs;
9023 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009024 SmallVector<Decl *, 4> ExprCaptures;
9025 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009026 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9027 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009028 for (auto RefExpr : VarList) {
9029 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009030 // OpenMP [2.1, C/C++]
9031 // A list item is a variable or array section, subject to the restrictions
9032 // specified in Section 2.4 on page 42 and in each of the sections
9033 // describing clauses and directives for which a list appears.
9034 // OpenMP [2.14.3.3, Restrictions, p.1]
9035 // A variable that is part of another variable (as an array or
9036 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009037 if (!FirstIter && IR != ER)
9038 ++IR;
9039 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009040 SourceLocation ELoc;
9041 SourceRange ERange;
9042 Expr *SimpleRefExpr = RefExpr;
9043 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9044 /*AllowArraySection=*/true);
9045 if (Res.second) {
9046 // It will be analyzed later.
9047 Vars.push_back(RefExpr);
9048 Privates.push_back(nullptr);
9049 LHSs.push_back(nullptr);
9050 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009051 // Try to find 'declare reduction' corresponding construct before using
9052 // builtin/overloaded operators.
9053 QualType Type = Context.DependentTy;
9054 CXXCastPath BasePath;
9055 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9056 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9057 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9058 if (CurContext->isDependentContext() &&
9059 (DeclareReductionRef.isUnset() ||
9060 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9061 ReductionOps.push_back(DeclareReductionRef.get());
9062 else
9063 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009064 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009065 ValueDecl *D = Res.first;
9066 if (!D)
9067 continue;
9068
Alexey Bataeva1764212015-09-30 09:22:36 +00009069 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009070 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9071 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9072 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009073 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009074 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009075 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9076 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9077 Type = ATy->getElementType();
9078 else
9079 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009080 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009081 } else
9082 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9083 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009084
Alexey Bataevc5e02582014-06-16 07:08:35 +00009085 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9086 // A variable that appears in a private clause must not have an incomplete
9087 // type or a reference type.
9088 if (RequireCompleteType(ELoc, Type,
9089 diag::err_omp_reduction_incomplete_type))
9090 continue;
9091 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009092 // A list item that appears in a reduction clause must not be
9093 // const-qualified.
9094 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009095 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009096 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009097 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009098 bool IsDecl = !VD ||
9099 VD->isThisDeclarationADefinition(Context) ==
9100 VarDecl::DeclarationOnly;
9101 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009102 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009103 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009104 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009105 continue;
9106 }
9107 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9108 // If a list-item is a reference type then it must bind to the same object
9109 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009110 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009111 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009112 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009113 DSARefChecker Check(DSAStack);
9114 if (Check.Visit(VDDef->getInit())) {
9115 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9116 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9117 continue;
9118 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009119 }
9120 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009121
Alexey Bataevc5e02582014-06-16 07:08:35 +00009122 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9123 // in a Construct]
9124 // Variables with the predetermined data-sharing attributes may not be
9125 // listed in data-sharing attributes clauses, except for the cases
9126 // listed below. For these exceptions only, listing a predetermined
9127 // variable in a data-sharing attribute clause is allowed and overrides
9128 // the variable's predetermined data-sharing attributes.
9129 // OpenMP [2.14.3.6, Restrictions, p.3]
9130 // Any number of reduction clauses can be specified on the directive,
9131 // but a list item can appear only once in the reduction clauses for that
9132 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009133 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009134 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009135 if (DVar.CKind == OMPC_reduction) {
9136 Diag(ELoc, diag::err_omp_once_referenced)
9137 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009138 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009139 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009140 } else if (DVar.CKind != OMPC_unknown) {
9141 Diag(ELoc, diag::err_omp_wrong_dsa)
9142 << getOpenMPClauseName(DVar.CKind)
9143 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009144 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009145 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009146 }
9147
9148 // OpenMP [2.14.3.6, Restrictions, p.1]
9149 // A list item that appears in a reduction clause of a worksharing
9150 // construct must be shared in the parallel regions to which any of the
9151 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009152 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9153 if (isOpenMPWorksharingDirective(CurrDir) &&
9154 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009155 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009156 if (DVar.CKind != OMPC_shared) {
9157 Diag(ELoc, diag::err_omp_required_access)
9158 << getOpenMPClauseName(OMPC_reduction)
9159 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009160 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009161 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009162 }
9163 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009164
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009165 // Try to find 'declare reduction' corresponding construct before using
9166 // builtin/overloaded operators.
9167 CXXCastPath BasePath;
9168 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9169 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9170 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9171 if (DeclareReductionRef.isInvalid())
9172 continue;
9173 if (CurContext->isDependentContext() &&
9174 (DeclareReductionRef.isUnset() ||
9175 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9176 Vars.push_back(RefExpr);
9177 Privates.push_back(nullptr);
9178 LHSs.push_back(nullptr);
9179 RHSs.push_back(nullptr);
9180 ReductionOps.push_back(DeclareReductionRef.get());
9181 continue;
9182 }
9183 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9184 // Not allowed reduction identifier is found.
9185 Diag(ReductionId.getLocStart(),
9186 diag::err_omp_unknown_reduction_identifier)
9187 << Type << ReductionIdRange;
9188 continue;
9189 }
9190
9191 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9192 // The type of a list item that appears in a reduction clause must be valid
9193 // for the reduction-identifier. For a max or min reduction in C, the type
9194 // of the list item must be an allowed arithmetic data type: char, int,
9195 // float, double, or _Bool, possibly modified with long, short, signed, or
9196 // unsigned. For a max or min reduction in C++, the type of the list item
9197 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9198 // double, or bool, possibly modified with long, short, signed, or unsigned.
9199 if (DeclareReductionRef.isUnset()) {
9200 if ((BOK == BO_GT || BOK == BO_LT) &&
9201 !(Type->isScalarType() ||
9202 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9203 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9204 << getLangOpts().CPlusPlus;
9205 if (!ASE && !OASE) {
9206 bool IsDecl = !VD ||
9207 VD->isThisDeclarationADefinition(Context) ==
9208 VarDecl::DeclarationOnly;
9209 Diag(D->getLocation(),
9210 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9211 << D;
9212 }
9213 continue;
9214 }
9215 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9216 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9217 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9218 if (!ASE && !OASE) {
9219 bool IsDecl = !VD ||
9220 VD->isThisDeclarationADefinition(Context) ==
9221 VarDecl::DeclarationOnly;
9222 Diag(D->getLocation(),
9223 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9224 << D;
9225 }
9226 continue;
9227 }
9228 }
9229
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009230 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009231 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009232 D->hasAttrs() ? &D->getAttrs() : nullptr);
9233 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9234 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009235 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009236 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009237 (!ASE &&
9238 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009239 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009240 // Create pseudo array type for private copy. The size for this array will
9241 // be generated during codegen.
9242 // For array subscripts or single variables Private Ty is the same as Type
9243 // (type of the variable or single array element).
9244 PrivateTy = Context.getVariableArrayType(
9245 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9246 Context.getSizeType(), VK_RValue),
9247 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009248 } else if (!ASE && !OASE &&
9249 Context.getAsArrayType(D->getType().getNonReferenceType()))
9250 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009251 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009252 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9253 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009254 // Add initializer for private variable.
9255 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009256 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9257 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9258 if (DeclareReductionRef.isUsable()) {
9259 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9260 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9261 if (DRD->getInitializer()) {
9262 Init = DRDRef;
9263 RHSVD->setInit(DRDRef);
9264 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009265 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009266 } else {
9267 switch (BOK) {
9268 case BO_Add:
9269 case BO_Xor:
9270 case BO_Or:
9271 case BO_LOr:
9272 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9273 if (Type->isScalarType() || Type->isAnyComplexType())
9274 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9275 break;
9276 case BO_Mul:
9277 case BO_LAnd:
9278 if (Type->isScalarType() || Type->isAnyComplexType()) {
9279 // '*' and '&&' reduction ops - initializer is '1'.
9280 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009281 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009282 break;
9283 case BO_And: {
9284 // '&' reduction op - initializer is '~0'.
9285 QualType OrigType = Type;
9286 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9287 Type = ComplexTy->getElementType();
9288 if (Type->isRealFloatingType()) {
9289 llvm::APFloat InitValue =
9290 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9291 /*isIEEE=*/true);
9292 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9293 Type, ELoc);
9294 } else if (Type->isScalarType()) {
9295 auto Size = Context.getTypeSize(Type);
9296 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9297 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9298 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9299 }
9300 if (Init && OrigType->isAnyComplexType()) {
9301 // Init = 0xFFFF + 0xFFFFi;
9302 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9303 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9304 }
9305 Type = OrigType;
9306 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009307 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009308 case BO_LT:
9309 case BO_GT: {
9310 // 'min' reduction op - initializer is 'Largest representable number in
9311 // the reduction list item type'.
9312 // 'max' reduction op - initializer is 'Least representable number in
9313 // the reduction list item type'.
9314 if (Type->isIntegerType() || Type->isPointerType()) {
9315 bool IsSigned = Type->hasSignedIntegerRepresentation();
9316 auto Size = Context.getTypeSize(Type);
9317 QualType IntTy =
9318 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9319 llvm::APInt InitValue =
9320 (BOK != BO_LT)
9321 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9322 : llvm::APInt::getMinValue(Size)
9323 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9324 : llvm::APInt::getMaxValue(Size);
9325 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9326 if (Type->isPointerType()) {
9327 // Cast to pointer type.
9328 auto CastExpr = BuildCStyleCastExpr(
9329 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9330 SourceLocation(), Init);
9331 if (CastExpr.isInvalid())
9332 continue;
9333 Init = CastExpr.get();
9334 }
9335 } else if (Type->isRealFloatingType()) {
9336 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9337 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9338 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9339 Type, ELoc);
9340 }
9341 break;
9342 }
9343 case BO_PtrMemD:
9344 case BO_PtrMemI:
9345 case BO_MulAssign:
9346 case BO_Div:
9347 case BO_Rem:
9348 case BO_Sub:
9349 case BO_Shl:
9350 case BO_Shr:
9351 case BO_LE:
9352 case BO_GE:
9353 case BO_EQ:
9354 case BO_NE:
9355 case BO_AndAssign:
9356 case BO_XorAssign:
9357 case BO_OrAssign:
9358 case BO_Assign:
9359 case BO_AddAssign:
9360 case BO_SubAssign:
9361 case BO_DivAssign:
9362 case BO_RemAssign:
9363 case BO_ShlAssign:
9364 case BO_ShrAssign:
9365 case BO_Comma:
9366 llvm_unreachable("Unexpected reduction operation");
9367 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009368 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009369 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009370 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9371 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009372 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009373 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009374 if (RHSVD->isInvalidDecl())
9375 continue;
9376 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009377 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9378 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009379 bool IsDecl =
9380 !VD ||
9381 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9382 Diag(D->getLocation(),
9383 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9384 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009385 continue;
9386 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009387 // Store initializer for single element in private copy. Will be used during
9388 // codegen.
9389 PrivateVD->setInit(RHSVD->getInit());
9390 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009391 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009392 ExprResult ReductionOp;
9393 if (DeclareReductionRef.isUsable()) {
9394 QualType RedTy = DeclareReductionRef.get()->getType();
9395 QualType PtrRedTy = Context.getPointerType(RedTy);
9396 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9397 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9398 if (!BasePath.empty()) {
9399 LHS = DefaultLvalueConversion(LHS.get());
9400 RHS = DefaultLvalueConversion(RHS.get());
9401 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9402 CK_UncheckedDerivedToBase, LHS.get(),
9403 &BasePath, LHS.get()->getValueKind());
9404 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9405 CK_UncheckedDerivedToBase, RHS.get(),
9406 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009407 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009408 FunctionProtoType::ExtProtoInfo EPI;
9409 QualType Params[] = {PtrRedTy, PtrRedTy};
9410 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9411 auto *OVE = new (Context) OpaqueValueExpr(
9412 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9413 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9414 Expr *Args[] = {LHS.get(), RHS.get()};
9415 ReductionOp = new (Context)
9416 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9417 } else {
9418 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9419 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9420 if (ReductionOp.isUsable()) {
9421 if (BOK != BO_LT && BOK != BO_GT) {
9422 ReductionOp =
9423 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9424 BO_Assign, LHSDRE, ReductionOp.get());
9425 } else {
9426 auto *ConditionalOp = new (Context) ConditionalOperator(
9427 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9428 RHSDRE, Type, VK_LValue, OK_Ordinary);
9429 ReductionOp =
9430 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9431 BO_Assign, LHSDRE, ConditionalOp);
9432 }
9433 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9434 }
9435 if (ReductionOp.isInvalid())
9436 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009437 }
9438
Alexey Bataev60da77e2016-02-29 05:54:20 +00009439 DeclRefExpr *Ref = nullptr;
9440 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009441 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009442 if (ASE || OASE) {
9443 TransformExprToCaptures RebuildToCapture(*this, D);
9444 VarsExpr =
9445 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9446 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009447 } else {
9448 VarsExpr = Ref =
9449 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009450 }
9451 if (!IsOpenMPCapturedDecl(D)) {
9452 ExprCaptures.push_back(Ref->getDecl());
9453 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9454 ExprResult RefRes = DefaultLvalueConversion(Ref);
9455 if (!RefRes.isUsable())
9456 continue;
9457 ExprResult PostUpdateRes =
9458 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9459 SimpleRefExpr, RefRes.get());
9460 if (!PostUpdateRes.isUsable())
9461 continue;
9462 ExprPostUpdates.push_back(
9463 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009464 }
9465 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009466 }
9467 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9468 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009469 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009470 LHSs.push_back(LHSDRE);
9471 RHSs.push_back(RHSDRE);
9472 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009473 }
9474
9475 if (Vars.empty())
9476 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009477
Alexey Bataevc5e02582014-06-16 07:08:35 +00009478 return OMPReductionClause::Create(
9479 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009480 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009481 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9482 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009483}
9484
Alexey Bataevecba70f2016-04-12 11:02:11 +00009485bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9486 SourceLocation LinLoc) {
9487 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9488 LinKind == OMPC_LINEAR_unknown) {
9489 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9490 return true;
9491 }
9492 return false;
9493}
9494
9495bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9496 OpenMPLinearClauseKind LinKind,
9497 QualType Type) {
9498 auto *VD = dyn_cast_or_null<VarDecl>(D);
9499 // A variable must not have an incomplete type or a reference type.
9500 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9501 return true;
9502 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9503 !Type->isReferenceType()) {
9504 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9505 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9506 return true;
9507 }
9508 Type = Type.getNonReferenceType();
9509
9510 // A list item must not be const-qualified.
9511 if (Type.isConstant(Context)) {
9512 Diag(ELoc, diag::err_omp_const_variable)
9513 << getOpenMPClauseName(OMPC_linear);
9514 if (D) {
9515 bool IsDecl =
9516 !VD ||
9517 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9518 Diag(D->getLocation(),
9519 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9520 << D;
9521 }
9522 return true;
9523 }
9524
9525 // A list item must be of integral or pointer type.
9526 Type = Type.getUnqualifiedType().getCanonicalType();
9527 const auto *Ty = Type.getTypePtrOrNull();
9528 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9529 !Ty->isPointerType())) {
9530 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9531 if (D) {
9532 bool IsDecl =
9533 !VD ||
9534 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9535 Diag(D->getLocation(),
9536 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9537 << D;
9538 }
9539 return true;
9540 }
9541 return false;
9542}
9543
Alexey Bataev182227b2015-08-20 10:54:39 +00009544OMPClause *Sema::ActOnOpenMPLinearClause(
9545 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9546 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9547 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009548 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009549 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009550 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009551 SmallVector<Decl *, 4> ExprCaptures;
9552 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009553 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009554 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009555 for (auto &RefExpr : VarList) {
9556 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009557 SourceLocation ELoc;
9558 SourceRange ERange;
9559 Expr *SimpleRefExpr = RefExpr;
9560 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9561 /*AllowArraySection=*/false);
9562 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009563 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009564 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009565 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009566 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009567 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009568 ValueDecl *D = Res.first;
9569 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009570 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009571
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009572 QualType Type = D->getType();
9573 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009574
9575 // OpenMP [2.14.3.7, linear clause]
9576 // A list-item cannot appear in more than one linear clause.
9577 // A list-item that appears in a linear clause cannot appear in any
9578 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009579 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009580 if (DVar.RefExpr) {
9581 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9582 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009583 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009584 continue;
9585 }
9586
Alexey Bataevecba70f2016-04-12 11:02:11 +00009587 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009588 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009589 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009590
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009591 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009592 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9593 D->hasAttrs() ? &D->getAttrs() : nullptr);
9594 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009595 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009596 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009597 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009598 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009599 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009600 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9601 if (!IsOpenMPCapturedDecl(D)) {
9602 ExprCaptures.push_back(Ref->getDecl());
9603 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9604 ExprResult RefRes = DefaultLvalueConversion(Ref);
9605 if (!RefRes.isUsable())
9606 continue;
9607 ExprResult PostUpdateRes =
9608 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9609 SimpleRefExpr, RefRes.get());
9610 if (!PostUpdateRes.isUsable())
9611 continue;
9612 ExprPostUpdates.push_back(
9613 IgnoredValueConversions(PostUpdateRes.get()).get());
9614 }
9615 }
9616 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009617 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009618 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009619 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009620 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009621 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009622 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9623 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9624
9625 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009626 Vars.push_back((VD || CurContext->isDependentContext())
9627 ? RefExpr->IgnoreParens()
9628 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009629 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009630 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009631 }
9632
9633 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009634 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009635
9636 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009637 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009638 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9639 !Step->isInstantiationDependent() &&
9640 !Step->containsUnexpandedParameterPack()) {
9641 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009642 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009643 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009644 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009645 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009646
Alexander Musman3276a272015-03-21 10:12:56 +00009647 // Build var to save the step value.
9648 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009649 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009650 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009651 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009652 ExprResult CalcStep =
9653 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009654 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009655
Alexander Musman8dba6642014-04-22 13:09:42 +00009656 // Warn about zero linear step (it would be probably better specified as
9657 // making corresponding variables 'const').
9658 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009659 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9660 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009661 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9662 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009663 if (!IsConstant && CalcStep.isUsable()) {
9664 // Calculate the step beforehand instead of doing this on each iteration.
9665 // (This is not used if the number of iterations may be kfold-ed).
9666 CalcStepExpr = CalcStep.get();
9667 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009668 }
9669
Alexey Bataev182227b2015-08-20 10:54:39 +00009670 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9671 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009672 StepExpr, CalcStepExpr,
9673 buildPreInits(Context, ExprCaptures),
9674 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009675}
9676
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009677static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9678 Expr *NumIterations, Sema &SemaRef,
9679 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009680 // Walk the vars and build update/final expressions for the CodeGen.
9681 SmallVector<Expr *, 8> Updates;
9682 SmallVector<Expr *, 8> Finals;
9683 Expr *Step = Clause.getStep();
9684 Expr *CalcStep = Clause.getCalcStep();
9685 // OpenMP [2.14.3.7, linear clause]
9686 // If linear-step is not specified it is assumed to be 1.
9687 if (Step == nullptr)
9688 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009689 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009690 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009691 }
Alexander Musman3276a272015-03-21 10:12:56 +00009692 bool HasErrors = false;
9693 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009694 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009695 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009696 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009697 SourceLocation ELoc;
9698 SourceRange ERange;
9699 Expr *SimpleRefExpr = RefExpr;
9700 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9701 /*AllowArraySection=*/false);
9702 ValueDecl *D = Res.first;
9703 if (Res.second || !D) {
9704 Updates.push_back(nullptr);
9705 Finals.push_back(nullptr);
9706 HasErrors = true;
9707 continue;
9708 }
9709 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9710 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9711 ->getMemberDecl();
9712 }
9713 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009714 Expr *InitExpr = *CurInit;
9715
9716 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009717 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009718 Expr *CapturedRef;
9719 if (LinKind == OMPC_LINEAR_uval)
9720 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9721 else
9722 CapturedRef =
9723 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9724 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9725 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009726
9727 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009728 ExprResult Update;
9729 if (!Info.first) {
9730 Update =
9731 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9732 InitExpr, IV, Step, /* Subtract */ false);
9733 } else
9734 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009735 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9736 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009737
9738 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009739 ExprResult Final;
9740 if (!Info.first) {
9741 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9742 InitExpr, NumIterations, Step,
9743 /* Subtract */ false);
9744 } else
9745 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009746 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9747 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009748
Alexander Musman3276a272015-03-21 10:12:56 +00009749 if (!Update.isUsable() || !Final.isUsable()) {
9750 Updates.push_back(nullptr);
9751 Finals.push_back(nullptr);
9752 HasErrors = true;
9753 } else {
9754 Updates.push_back(Update.get());
9755 Finals.push_back(Final.get());
9756 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009757 ++CurInit;
9758 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009759 }
9760 Clause.setUpdates(Updates);
9761 Clause.setFinals(Finals);
9762 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009763}
9764
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009765OMPClause *Sema::ActOnOpenMPAlignedClause(
9766 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9767 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9768
9769 SmallVector<Expr *, 8> Vars;
9770 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009771 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9772 SourceLocation ELoc;
9773 SourceRange ERange;
9774 Expr *SimpleRefExpr = RefExpr;
9775 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9776 /*AllowArraySection=*/false);
9777 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009778 // It will be analyzed later.
9779 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009780 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009781 ValueDecl *D = Res.first;
9782 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009783 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009784
Alexey Bataev1efd1662016-03-29 10:59:56 +00009785 QualType QType = D->getType();
9786 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009787
9788 // OpenMP [2.8.1, simd construct, Restrictions]
9789 // The type of list items appearing in the aligned clause must be
9790 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009791 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009792 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009793 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009794 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009795 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009796 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009797 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009798 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009799 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009800 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009801 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009802 continue;
9803 }
9804
9805 // OpenMP [2.8.1, simd construct, Restrictions]
9806 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009807 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009808 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009809 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9810 << getOpenMPClauseName(OMPC_aligned);
9811 continue;
9812 }
9813
Alexey Bataev1efd1662016-03-29 10:59:56 +00009814 DeclRefExpr *Ref = nullptr;
9815 if (!VD && IsOpenMPCapturedDecl(D))
9816 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9817 Vars.push_back(DefaultFunctionArrayConversion(
9818 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9819 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009820 }
9821
9822 // OpenMP [2.8.1, simd construct, Description]
9823 // The parameter of the aligned clause, alignment, must be a constant
9824 // positive integer expression.
9825 // If no optional parameter is specified, implementation-defined default
9826 // alignments for SIMD instructions on the target platforms are assumed.
9827 if (Alignment != nullptr) {
9828 ExprResult AlignResult =
9829 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9830 if (AlignResult.isInvalid())
9831 return nullptr;
9832 Alignment = AlignResult.get();
9833 }
9834 if (Vars.empty())
9835 return nullptr;
9836
9837 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9838 EndLoc, Vars, Alignment);
9839}
9840
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009841OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9842 SourceLocation StartLoc,
9843 SourceLocation LParenLoc,
9844 SourceLocation EndLoc) {
9845 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009846 SmallVector<Expr *, 8> SrcExprs;
9847 SmallVector<Expr *, 8> DstExprs;
9848 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009849 for (auto &RefExpr : VarList) {
9850 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9851 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009852 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009853 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009854 SrcExprs.push_back(nullptr);
9855 DstExprs.push_back(nullptr);
9856 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009857 continue;
9858 }
9859
Alexey Bataeved09d242014-05-28 05:53:51 +00009860 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009861 // OpenMP [2.1, C/C++]
9862 // A list item is a variable name.
9863 // OpenMP [2.14.4.1, Restrictions, p.1]
9864 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009865 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009866 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009867 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9868 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009869 continue;
9870 }
9871
9872 Decl *D = DE->getDecl();
9873 VarDecl *VD = cast<VarDecl>(D);
9874
9875 QualType Type = VD->getType();
9876 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9877 // It will be analyzed later.
9878 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009879 SrcExprs.push_back(nullptr);
9880 DstExprs.push_back(nullptr);
9881 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009882 continue;
9883 }
9884
9885 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9886 // A list item that appears in a copyin clause must be threadprivate.
9887 if (!DSAStack->isThreadPrivate(VD)) {
9888 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009889 << getOpenMPClauseName(OMPC_copyin)
9890 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009891 continue;
9892 }
9893
9894 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9895 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009896 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009897 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009898 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009899 auto *SrcVD =
9900 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9901 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009902 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009903 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9904 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009905 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9906 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009907 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009908 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009909 // For arrays generate assignment operation for single element and replace
9910 // it by the original array element in CodeGen.
9911 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9912 PseudoDstExpr, PseudoSrcExpr);
9913 if (AssignmentOp.isInvalid())
9914 continue;
9915 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9916 /*DiscardedValue=*/true);
9917 if (AssignmentOp.isInvalid())
9918 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009919
9920 DSAStack->addDSA(VD, DE, OMPC_copyin);
9921 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009922 SrcExprs.push_back(PseudoSrcExpr);
9923 DstExprs.push_back(PseudoDstExpr);
9924 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009925 }
9926
Alexey Bataeved09d242014-05-28 05:53:51 +00009927 if (Vars.empty())
9928 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009929
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009930 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9931 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009932}
9933
Alexey Bataevbae9a792014-06-27 10:37:06 +00009934OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9935 SourceLocation StartLoc,
9936 SourceLocation LParenLoc,
9937 SourceLocation EndLoc) {
9938 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009939 SmallVector<Expr *, 8> SrcExprs;
9940 SmallVector<Expr *, 8> DstExprs;
9941 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009942 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009943 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9944 SourceLocation ELoc;
9945 SourceRange ERange;
9946 Expr *SimpleRefExpr = RefExpr;
9947 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9948 /*AllowArraySection=*/false);
9949 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009950 // It will be analyzed later.
9951 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009952 SrcExprs.push_back(nullptr);
9953 DstExprs.push_back(nullptr);
9954 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009955 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009956 ValueDecl *D = Res.first;
9957 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009958 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009959
Alexey Bataeve122da12016-03-17 10:50:17 +00009960 QualType Type = D->getType();
9961 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009962
9963 // OpenMP [2.14.4.2, Restrictions, p.2]
9964 // A list item that appears in a copyprivate clause may not appear in a
9965 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009966 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9967 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009968 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9969 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009970 Diag(ELoc, diag::err_omp_wrong_dsa)
9971 << getOpenMPClauseName(DVar.CKind)
9972 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009973 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009974 continue;
9975 }
9976
9977 // OpenMP [2.11.4.2, Restrictions, p.1]
9978 // All list items that appear in a copyprivate clause must be either
9979 // threadprivate or private in the enclosing context.
9980 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009981 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009982 if (DVar.CKind == OMPC_shared) {
9983 Diag(ELoc, diag::err_omp_required_access)
9984 << getOpenMPClauseName(OMPC_copyprivate)
9985 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009986 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009987 continue;
9988 }
9989 }
9990 }
9991
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009992 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009993 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009994 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009995 << getOpenMPClauseName(OMPC_copyprivate) << Type
9996 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009997 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009998 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009999 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010000 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010001 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010002 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010003 continue;
10004 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010005
Alexey Bataevbae9a792014-06-27 10:37:06 +000010006 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10007 // A variable of class type (or array thereof) that appears in a
10008 // copyin clause requires an accessible, unambiguous copy assignment
10009 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010010 Type = Context.getBaseElementType(Type.getNonReferenceType())
10011 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010012 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010013 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10014 D->hasAttrs() ? &D->getAttrs() : nullptr);
10015 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010016 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010017 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10018 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010019 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010020 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10021 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010022 PseudoDstExpr, PseudoSrcExpr);
10023 if (AssignmentOp.isInvalid())
10024 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010025 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010026 /*DiscardedValue=*/true);
10027 if (AssignmentOp.isInvalid())
10028 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010029
10030 // No need to mark vars as copyprivate, they are already threadprivate or
10031 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010032 assert(VD || IsOpenMPCapturedDecl(D));
10033 Vars.push_back(
10034 VD ? RefExpr->IgnoreParens()
10035 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010036 SrcExprs.push_back(PseudoSrcExpr);
10037 DstExprs.push_back(PseudoDstExpr);
10038 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010039 }
10040
10041 if (Vars.empty())
10042 return nullptr;
10043
Alexey Bataeva63048e2015-03-23 06:18:07 +000010044 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10045 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010046}
10047
Alexey Bataev6125da92014-07-21 11:26:11 +000010048OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10049 SourceLocation StartLoc,
10050 SourceLocation LParenLoc,
10051 SourceLocation EndLoc) {
10052 if (VarList.empty())
10053 return nullptr;
10054
10055 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10056}
Alexey Bataevdea47612014-07-23 07:46:59 +000010057
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010058OMPClause *
10059Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10060 SourceLocation DepLoc, SourceLocation ColonLoc,
10061 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10062 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010063 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010064 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010065 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010066 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010067 return nullptr;
10068 }
10069 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010070 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10071 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010072 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010073 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010074 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10075 /*Last=*/OMPC_DEPEND_unknown, Except)
10076 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010077 return nullptr;
10078 }
10079 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010080 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010081 llvm::APSInt DepCounter(/*BitWidth=*/32);
10082 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10083 if (DepKind == OMPC_DEPEND_sink) {
10084 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10085 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10086 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010087 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010088 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010089 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10090 DSAStack->getParentOrderedRegionParam()) {
10091 for (auto &RefExpr : VarList) {
10092 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010093 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010094 // It will be analyzed later.
10095 Vars.push_back(RefExpr);
10096 continue;
10097 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010098
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010099 SourceLocation ELoc = RefExpr->getExprLoc();
10100 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10101 if (DepKind == OMPC_DEPEND_sink) {
10102 if (DepCounter >= TotalDepCount) {
10103 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10104 continue;
10105 }
10106 ++DepCounter;
10107 // OpenMP [2.13.9, Summary]
10108 // depend(dependence-type : vec), where dependence-type is:
10109 // 'sink' and where vec is the iteration vector, which has the form:
10110 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10111 // where n is the value specified by the ordered clause in the loop
10112 // directive, xi denotes the loop iteration variable of the i-th nested
10113 // loop associated with the loop directive, and di is a constant
10114 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010115 if (CurContext->isDependentContext()) {
10116 // It will be analyzed later.
10117 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010118 continue;
10119 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010120 SimpleExpr = SimpleExpr->IgnoreImplicit();
10121 OverloadedOperatorKind OOK = OO_None;
10122 SourceLocation OOLoc;
10123 Expr *LHS = SimpleExpr;
10124 Expr *RHS = nullptr;
10125 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10126 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10127 OOLoc = BO->getOperatorLoc();
10128 LHS = BO->getLHS()->IgnoreParenImpCasts();
10129 RHS = BO->getRHS()->IgnoreParenImpCasts();
10130 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10131 OOK = OCE->getOperator();
10132 OOLoc = OCE->getOperatorLoc();
10133 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10134 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10135 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10136 OOK = MCE->getMethodDecl()
10137 ->getNameInfo()
10138 .getName()
10139 .getCXXOverloadedOperator();
10140 OOLoc = MCE->getCallee()->getExprLoc();
10141 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10142 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10143 }
10144 SourceLocation ELoc;
10145 SourceRange ERange;
10146 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10147 /*AllowArraySection=*/false);
10148 if (Res.second) {
10149 // It will be analyzed later.
10150 Vars.push_back(RefExpr);
10151 }
10152 ValueDecl *D = Res.first;
10153 if (!D)
10154 continue;
10155
10156 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10157 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10158 continue;
10159 }
10160 if (RHS) {
10161 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10162 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10163 if (RHSRes.isInvalid())
10164 continue;
10165 }
10166 if (!CurContext->isDependentContext() &&
10167 DSAStack->getParentOrderedRegionParam() &&
10168 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10169 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10170 << DSAStack->getParentLoopControlVariable(
10171 DepCounter.getZExtValue());
10172 continue;
10173 }
10174 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010175 } else {
10176 // OpenMP [2.11.1.1, Restrictions, p.3]
10177 // A variable that is part of another variable (such as a field of a
10178 // structure) but is not an array element or an array section cannot
10179 // appear in a depend clause.
10180 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10181 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10182 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10183 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10184 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010185 (ASE &&
10186 !ASE->getBase()
10187 ->getType()
10188 .getNonReferenceType()
10189 ->isPointerType() &&
10190 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010191 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10192 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010193 continue;
10194 }
10195 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010196 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10197 }
10198
10199 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10200 TotalDepCount > VarList.size() &&
10201 DSAStack->getParentOrderedRegionParam()) {
10202 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10203 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10204 }
10205 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10206 Vars.empty())
10207 return nullptr;
10208 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010209 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10210 DepKind, DepLoc, ColonLoc, Vars);
10211 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10212 DSAStack->addDoacrossDependClause(C, OpsOffs);
10213 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010214}
Michael Wonge710d542015-08-07 16:16:36 +000010215
10216OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10217 SourceLocation LParenLoc,
10218 SourceLocation EndLoc) {
10219 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010220
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010221 // OpenMP [2.9.1, Restrictions]
10222 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010223 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10224 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010225 return nullptr;
10226
Michael Wonge710d542015-08-07 16:16:36 +000010227 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10228}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010229
10230static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10231 DSAStackTy *Stack, CXXRecordDecl *RD) {
10232 if (!RD || RD->isInvalidDecl())
10233 return true;
10234
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010235 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10236 if (auto *CTD = CTSD->getSpecializedTemplate())
10237 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010238 auto QTy = SemaRef.Context.getRecordType(RD);
10239 if (RD->isDynamicClass()) {
10240 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10241 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10242 return false;
10243 }
10244 auto *DC = RD;
10245 bool IsCorrect = true;
10246 for (auto *I : DC->decls()) {
10247 if (I) {
10248 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10249 if (MD->isStatic()) {
10250 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10251 SemaRef.Diag(MD->getLocation(),
10252 diag::note_omp_static_member_in_target);
10253 IsCorrect = false;
10254 }
10255 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10256 if (VD->isStaticDataMember()) {
10257 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10258 SemaRef.Diag(VD->getLocation(),
10259 diag::note_omp_static_member_in_target);
10260 IsCorrect = false;
10261 }
10262 }
10263 }
10264 }
10265
10266 for (auto &I : RD->bases()) {
10267 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10268 I.getType()->getAsCXXRecordDecl()))
10269 IsCorrect = false;
10270 }
10271 return IsCorrect;
10272}
10273
10274static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10275 DSAStackTy *Stack, QualType QTy) {
10276 NamedDecl *ND;
10277 if (QTy->isIncompleteType(&ND)) {
10278 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10279 return false;
10280 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10281 if (!RD->isInvalidDecl() &&
10282 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10283 return false;
10284 }
10285 return true;
10286}
10287
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010288/// \brief Return true if it can be proven that the provided array expression
10289/// (array section or array subscript) does NOT specify the whole size of the
10290/// array whose base type is \a BaseQTy.
10291static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10292 const Expr *E,
10293 QualType BaseQTy) {
10294 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10295
10296 // If this is an array subscript, it refers to the whole size if the size of
10297 // the dimension is constant and equals 1. Also, an array section assumes the
10298 // format of an array subscript if no colon is used.
10299 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10300 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10301 return ATy->getSize().getSExtValue() != 1;
10302 // Size can't be evaluated statically.
10303 return false;
10304 }
10305
10306 assert(OASE && "Expecting array section if not an array subscript.");
10307 auto *LowerBound = OASE->getLowerBound();
10308 auto *Length = OASE->getLength();
10309
10310 // If there is a lower bound that does not evaluates to zero, we are not
10311 // convering the whole dimension.
10312 if (LowerBound) {
10313 llvm::APSInt ConstLowerBound;
10314 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10315 return false; // Can't get the integer value as a constant.
10316 if (ConstLowerBound.getSExtValue())
10317 return true;
10318 }
10319
10320 // If we don't have a length we covering the whole dimension.
10321 if (!Length)
10322 return false;
10323
10324 // If the base is a pointer, we don't have a way to get the size of the
10325 // pointee.
10326 if (BaseQTy->isPointerType())
10327 return false;
10328
10329 // We can only check if the length is the same as the size of the dimension
10330 // if we have a constant array.
10331 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10332 if (!CATy)
10333 return false;
10334
10335 llvm::APSInt ConstLength;
10336 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10337 return false; // Can't get the integer value as a constant.
10338
10339 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10340}
10341
10342// Return true if it can be proven that the provided array expression (array
10343// section or array subscript) does NOT specify a single element of the array
10344// whose base type is \a BaseQTy.
10345static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10346 const Expr *E,
10347 QualType BaseQTy) {
10348 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10349
10350 // An array subscript always refer to a single element. Also, an array section
10351 // assumes the format of an array subscript if no colon is used.
10352 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10353 return false;
10354
10355 assert(OASE && "Expecting array section if not an array subscript.");
10356 auto *Length = OASE->getLength();
10357
10358 // If we don't have a length we have to check if the array has unitary size
10359 // for this dimension. Also, we should always expect a length if the base type
10360 // is pointer.
10361 if (!Length) {
10362 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10363 return ATy->getSize().getSExtValue() != 1;
10364 // We cannot assume anything.
10365 return false;
10366 }
10367
10368 // Check if the length evaluates to 1.
10369 llvm::APSInt ConstLength;
10370 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10371 return false; // Can't get the integer value as a constant.
10372
10373 return ConstLength.getSExtValue() != 1;
10374}
10375
Samuel Antao661c0902016-05-26 17:39:58 +000010376// Return the expression of the base of the mappable expression or null if it
10377// cannot be determined and do all the necessary checks to see if the expression
10378// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010379// components of the expression.
10380static Expr *CheckMapClauseExpressionBase(
10381 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010382 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10383 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010384 SourceLocation ELoc = E->getExprLoc();
10385 SourceRange ERange = E->getSourceRange();
10386
10387 // The base of elements of list in a map clause have to be either:
10388 // - a reference to variable or field.
10389 // - a member expression.
10390 // - an array expression.
10391 //
10392 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10393 // reference to 'r'.
10394 //
10395 // If we have:
10396 //
10397 // struct SS {
10398 // Bla S;
10399 // foo() {
10400 // #pragma omp target map (S.Arr[:12]);
10401 // }
10402 // }
10403 //
10404 // We want to retrieve the member expression 'this->S';
10405
10406 Expr *RelevantExpr = nullptr;
10407
Samuel Antao5de996e2016-01-22 20:21:36 +000010408 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10409 // If a list item is an array section, it must specify contiguous storage.
10410 //
10411 // For this restriction it is sufficient that we make sure only references
10412 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010413 // exist except in the rightmost expression (unless they cover the whole
10414 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010415 //
10416 // r.ArrS[3:5].Arr[6:7]
10417 //
10418 // r.ArrS[3:5].x
10419 //
10420 // but these would be valid:
10421 // r.ArrS[3].Arr[6:7]
10422 //
10423 // r.ArrS[3].x
10424
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010425 bool AllowUnitySizeArraySection = true;
10426 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010427
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010428 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010429 E = E->IgnoreParenImpCasts();
10430
10431 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10432 if (!isa<VarDecl>(CurE->getDecl()))
10433 break;
10434
10435 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010436
10437 // If we got a reference to a declaration, we should not expect any array
10438 // section before that.
10439 AllowUnitySizeArraySection = false;
10440 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010441
10442 // Record the component.
10443 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10444 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010445 continue;
10446 }
10447
10448 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10449 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10450
10451 if (isa<CXXThisExpr>(BaseE))
10452 // We found a base expression: this->Val.
10453 RelevantExpr = CurE;
10454 else
10455 E = BaseE;
10456
10457 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10458 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10459 << CurE->getSourceRange();
10460 break;
10461 }
10462
10463 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10464
10465 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10466 // A bit-field cannot appear in a map clause.
10467 //
10468 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010469 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10470 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010471 break;
10472 }
10473
10474 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10475 // If the type of a list item is a reference to a type T then the type
10476 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010477 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010478
10479 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10480 // A list item cannot be a variable that is a member of a structure with
10481 // a union type.
10482 //
10483 if (auto *RT = CurType->getAs<RecordType>())
10484 if (RT->isUnionType()) {
10485 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10486 << CurE->getSourceRange();
10487 break;
10488 }
10489
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010490 // If we got a member expression, we should not expect any array section
10491 // before that:
10492 //
10493 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10494 // If a list item is an element of a structure, only the rightmost symbol
10495 // of the variable reference can be an array section.
10496 //
10497 AllowUnitySizeArraySection = false;
10498 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010499
10500 // Record the component.
10501 CurComponents.push_back(
10502 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010503 continue;
10504 }
10505
10506 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10507 E = CurE->getBase()->IgnoreParenImpCasts();
10508
10509 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10510 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10511 << 0 << CurE->getSourceRange();
10512 break;
10513 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010514
10515 // If we got an array subscript that express the whole dimension we
10516 // can have any array expressions before. If it only expressing part of
10517 // the dimension, we can only have unitary-size array expressions.
10518 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10519 E->getType()))
10520 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010521
10522 // Record the component - we don't have any declaration associated.
10523 CurComponents.push_back(
10524 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010525 continue;
10526 }
10527
10528 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010529 E = CurE->getBase()->IgnoreParenImpCasts();
10530
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010531 auto CurType =
10532 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10533
Samuel Antao5de996e2016-01-22 20:21:36 +000010534 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10535 // If the type of a list item is a reference to a type T then the type
10536 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010537 if (CurType->isReferenceType())
10538 CurType = CurType->getPointeeType();
10539
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010540 bool IsPointer = CurType->isAnyPointerType();
10541
10542 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010543 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10544 << 0 << CurE->getSourceRange();
10545 break;
10546 }
10547
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010548 bool NotWhole =
10549 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10550 bool NotUnity =
10551 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10552
10553 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
10554 // Any array section is currently allowed.
10555 //
10556 // If this array section refers to the whole dimension we can still
10557 // accept other array sections before this one, except if the base is a
10558 // pointer. Otherwise, only unitary sections are accepted.
10559 if (NotWhole || IsPointer)
10560 AllowWholeSizeArraySection = false;
10561 } else if ((AllowUnitySizeArraySection && NotUnity) ||
10562 (AllowWholeSizeArraySection && NotWhole)) {
10563 // A unity or whole array section is not allowed and that is not
10564 // compatible with the properties of the current array section.
10565 SemaRef.Diag(
10566 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10567 << CurE->getSourceRange();
10568 break;
10569 }
Samuel Antao90927002016-04-26 14:54:23 +000010570
10571 // Record the component - we don't have any declaration associated.
10572 CurComponents.push_back(
10573 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010574 continue;
10575 }
10576
10577 // If nothing else worked, this is not a valid map clause expression.
10578 SemaRef.Diag(ELoc,
10579 diag::err_omp_expected_named_var_member_or_array_expression)
10580 << ERange;
10581 break;
10582 }
10583
10584 return RelevantExpr;
10585}
10586
10587// Return true if expression E associated with value VD has conflicts with other
10588// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010589static bool CheckMapConflicts(
10590 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10591 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010592 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10593 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010594 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010595 SourceLocation ELoc = E->getExprLoc();
10596 SourceRange ERange = E->getSourceRange();
10597
10598 // In order to easily check the conflicts we need to match each component of
10599 // the expression under test with the components of the expressions that are
10600 // already in the stack.
10601
Samuel Antao5de996e2016-01-22 20:21:36 +000010602 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010603 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010604 "Map clause expression with unexpected base!");
10605
10606 // Variables to help detecting enclosing problems in data environment nests.
10607 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010608 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010609
Samuel Antao90927002016-04-26 14:54:23 +000010610 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10611 VD, CurrentRegionOnly,
10612 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10613 StackComponents) -> bool {
10614
Samuel Antao5de996e2016-01-22 20:21:36 +000010615 assert(!StackComponents.empty() &&
10616 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010617 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010618 "Map clause expression with unexpected base!");
10619
Samuel Antao90927002016-04-26 14:54:23 +000010620 // The whole expression in the stack.
10621 auto *RE = StackComponents.front().getAssociatedExpression();
10622
Samuel Antao5de996e2016-01-22 20:21:36 +000010623 // Expressions must start from the same base. Here we detect at which
10624 // point both expressions diverge from each other and see if we can
10625 // detect if the memory referred to both expressions is contiguous and
10626 // do not overlap.
10627 auto CI = CurComponents.rbegin();
10628 auto CE = CurComponents.rend();
10629 auto SI = StackComponents.rbegin();
10630 auto SE = StackComponents.rend();
10631 for (; CI != CE && SI != SE; ++CI, ++SI) {
10632
10633 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10634 // At most one list item can be an array item derived from a given
10635 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010636 if (CurrentRegionOnly &&
10637 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10638 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10639 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10640 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10641 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010642 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010643 << CI->getAssociatedExpression()->getSourceRange();
10644 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10645 diag::note_used_here)
10646 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010647 return true;
10648 }
10649
10650 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010651 if (CI->getAssociatedExpression()->getStmtClass() !=
10652 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010653 break;
10654
10655 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010656 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010657 break;
10658 }
10659
10660 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10661 // List items of map clauses in the same construct must not share
10662 // original storage.
10663 //
10664 // If the expressions are exactly the same or one is a subset of the
10665 // other, it means they are sharing storage.
10666 if (CI == CE && SI == SE) {
10667 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010668 if (CKind == OMPC_map)
10669 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10670 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010671 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010672 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10673 << ERange;
10674 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010675 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10676 << RE->getSourceRange();
10677 return true;
10678 } else {
10679 // If we find the same expression in the enclosing data environment,
10680 // that is legal.
10681 IsEnclosedByDataEnvironmentExpr = true;
10682 return false;
10683 }
10684 }
10685
Samuel Antao90927002016-04-26 14:54:23 +000010686 QualType DerivedType =
10687 std::prev(CI)->getAssociatedDeclaration()->getType();
10688 SourceLocation DerivedLoc =
10689 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010690
10691 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10692 // If the type of a list item is a reference to a type T then the type
10693 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010694 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010695
10696 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10697 // A variable for which the type is pointer and an array section
10698 // derived from that variable must not appear as list items of map
10699 // clauses of the same construct.
10700 //
10701 // Also, cover one of the cases in:
10702 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10703 // If any part of the original storage of a list item has corresponding
10704 // storage in the device data environment, all of the original storage
10705 // must have corresponding storage in the device data environment.
10706 //
10707 if (DerivedType->isAnyPointerType()) {
10708 if (CI == CE || SI == SE) {
10709 SemaRef.Diag(
10710 DerivedLoc,
10711 diag::err_omp_pointer_mapped_along_with_derived_section)
10712 << DerivedLoc;
10713 } else {
10714 assert(CI != CE && SI != SE);
10715 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10716 << DerivedLoc;
10717 }
10718 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10719 << RE->getSourceRange();
10720 return true;
10721 }
10722
10723 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10724 // List items of map clauses in the same construct must not share
10725 // original storage.
10726 //
10727 // An expression is a subset of the other.
10728 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010729 if (CKind == OMPC_map)
10730 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10731 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010732 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010733 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10734 << ERange;
10735 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010736 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10737 << RE->getSourceRange();
10738 return true;
10739 }
10740
10741 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010742 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010743 if (!CurrentRegionOnly && SI != SE)
10744 EnclosingExpr = RE;
10745
10746 // The current expression is a subset of the expression in the data
10747 // environment.
10748 IsEnclosedByDataEnvironmentExpr |=
10749 (!CurrentRegionOnly && CI != CE && SI == SE);
10750
10751 return false;
10752 });
10753
10754 if (CurrentRegionOnly)
10755 return FoundError;
10756
10757 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10758 // If any part of the original storage of a list item has corresponding
10759 // storage in the device data environment, all of the original storage must
10760 // have corresponding storage in the device data environment.
10761 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10762 // If a list item is an element of a structure, and a different element of
10763 // the structure has a corresponding list item in the device data environment
10764 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010765 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010766 // data environment prior to the task encountering the construct.
10767 //
10768 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10769 SemaRef.Diag(ELoc,
10770 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10771 << ERange;
10772 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10773 << EnclosingExpr->getSourceRange();
10774 return true;
10775 }
10776
10777 return FoundError;
10778}
10779
Samuel Antao661c0902016-05-26 17:39:58 +000010780namespace {
10781// Utility struct that gathers all the related lists associated with a mappable
10782// expression.
10783struct MappableVarListInfo final {
10784 // The list of expressions.
10785 ArrayRef<Expr *> VarList;
10786 // The list of processed expressions.
10787 SmallVector<Expr *, 16> ProcessedVarList;
10788 // The mappble components for each expression.
10789 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10790 // The base declaration of the variable.
10791 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10792
10793 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10794 // We have a list of components and base declarations for each entry in the
10795 // variable list.
10796 VarComponents.reserve(VarList.size());
10797 VarBaseDeclarations.reserve(VarList.size());
10798 }
10799};
10800}
10801
10802// Check the validity of the provided variable list for the provided clause kind
10803// \a CKind. In the check process the valid expressions, and mappable expression
10804// components and variables are extracted and used to fill \a Vars,
10805// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10806// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10807static void
10808checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10809 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10810 SourceLocation StartLoc,
10811 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10812 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010813 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10814 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010815 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010816
Samuel Antao90927002016-04-26 14:54:23 +000010817 // Keep track of the mappable components and base declarations in this clause.
10818 // Each entry in the list is going to have a list of components associated. We
10819 // record each set of the components so that we can build the clause later on.
10820 // In the end we should have the same amount of declarations and component
10821 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010822
Samuel Antao661c0902016-05-26 17:39:58 +000010823 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010824 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010825 SourceLocation ELoc = RE->getExprLoc();
10826
Kelvin Li0bff7af2015-11-23 05:32:03 +000010827 auto *VE = RE->IgnoreParenLValueCasts();
10828
10829 if (VE->isValueDependent() || VE->isTypeDependent() ||
10830 VE->isInstantiationDependent() ||
10831 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010832 // We can only analyze this information once the missing information is
10833 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000010834 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010835 continue;
10836 }
10837
10838 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010839
Samuel Antao5de996e2016-01-22 20:21:36 +000010840 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010841 SemaRef.Diag(ELoc,
10842 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000010843 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010844 continue;
10845 }
10846
Samuel Antao90927002016-04-26 14:54:23 +000010847 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10848 ValueDecl *CurDeclaration = nullptr;
10849
10850 // Obtain the array or member expression bases if required. Also, fill the
10851 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000010852 auto *BE =
10853 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010854 if (!BE)
10855 continue;
10856
Samuel Antao90927002016-04-26 14:54:23 +000010857 assert(!CurComponents.empty() &&
10858 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010859
Samuel Antao90927002016-04-26 14:54:23 +000010860 // For the following checks, we rely on the base declaration which is
10861 // expected to be associated with the last component. The declaration is
10862 // expected to be a variable or a field (if 'this' is being mapped).
10863 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10864 assert(CurDeclaration && "Null decl on map clause.");
10865 assert(
10866 CurDeclaration->isCanonicalDecl() &&
10867 "Expecting components to have associated only canonical declarations.");
10868
10869 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10870 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010871
10872 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010873 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010874
10875 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000010876 // threadprivate variables cannot appear in a map clause.
10877 // OpenMP 4.5 [2.10.5, target update Construct]
10878 // threadprivate variables cannot appear in a from clause.
10879 if (VD && DSAS->isThreadPrivate(VD)) {
10880 auto DVar = DSAS->getTopDSA(VD, false);
10881 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10882 << getOpenMPClauseName(CKind);
10883 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010884 continue;
10885 }
10886
Samuel Antao5de996e2016-01-22 20:21:36 +000010887 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10888 // A list item cannot appear in both a map clause and a data-sharing
10889 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010890
Samuel Antao5de996e2016-01-22 20:21:36 +000010891 // Check conflicts with other map clause expressions. We check the conflicts
10892 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000010893 // environment, because the restrictions are different. We only have to
10894 // check conflicts across regions for the map clauses.
10895 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10896 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010897 break;
Samuel Antao661c0902016-05-26 17:39:58 +000010898 if (CKind == OMPC_map &&
10899 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10900 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010901 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010902
Samuel Antao661c0902016-05-26 17:39:58 +000010903 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000010904 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10905 // If the type of a list item is a reference to a type T then the type will
10906 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010907 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010908
Samuel Antao661c0902016-05-26 17:39:58 +000010909 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10910 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000010911 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010912 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000010913 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10914 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010915 continue;
10916
Samuel Antao661c0902016-05-26 17:39:58 +000010917 if (CKind == OMPC_map) {
10918 // target enter data
10919 // OpenMP [2.10.2, Restrictions, p. 99]
10920 // A map-type must be specified in all map clauses and must be either
10921 // to or alloc.
10922 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10923 if (DKind == OMPD_target_enter_data &&
10924 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10925 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10926 << (IsMapTypeImplicit ? 1 : 0)
10927 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10928 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010929 continue;
10930 }
Samuel Antao661c0902016-05-26 17:39:58 +000010931
10932 // target exit_data
10933 // OpenMP [2.10.3, Restrictions, p. 102]
10934 // A map-type must be specified in all map clauses and must be either
10935 // from, release, or delete.
10936 if (DKind == OMPD_target_exit_data &&
10937 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10938 MapType == OMPC_MAP_delete)) {
10939 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10940 << (IsMapTypeImplicit ? 1 : 0)
10941 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10942 << getOpenMPDirectiveName(DKind);
10943 continue;
10944 }
10945
10946 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10947 // A list item cannot appear in both a map clause and a data-sharing
10948 // attribute clause on the same construct
10949 if (DKind == OMPD_target && VD) {
10950 auto DVar = DSAS->getTopDSA(VD, false);
10951 if (isOpenMPPrivate(DVar.CKind)) {
10952 SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10953 << getOpenMPClauseName(DVar.CKind)
10954 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
10955 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
10956 continue;
10957 }
10958 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010959 }
10960
Samuel Antao90927002016-04-26 14:54:23 +000010961 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000010962 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010963
10964 // Store the components in the stack so that they can be used to check
10965 // against other clauses later on.
Samuel Antao661c0902016-05-26 17:39:58 +000010966 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
Samuel Antao90927002016-04-26 14:54:23 +000010967
10968 // Save the components and declaration to create the clause. For purposes of
10969 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000010970 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000010971 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
10972 MVLI.VarComponents.back().append(CurComponents.begin(),
10973 CurComponents.end());
10974 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10975 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010976 }
Samuel Antao661c0902016-05-26 17:39:58 +000010977}
10978
10979OMPClause *
10980Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10981 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10982 SourceLocation MapLoc, SourceLocation ColonLoc,
10983 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10984 SourceLocation LParenLoc, SourceLocation EndLoc) {
10985 MappableVarListInfo MVLI(VarList);
10986 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
10987 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010988
Samuel Antao5de996e2016-01-22 20:21:36 +000010989 // We need to produce a map clause even if we don't have variables so that
10990 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000010991 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10992 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
10993 MVLI.VarComponents, MapTypeModifier, MapType,
10994 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010995}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010996
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010997QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10998 TypeResult ParsedType) {
10999 assert(ParsedType.isUsable());
11000
11001 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11002 if (ReductionType.isNull())
11003 return QualType();
11004
11005 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11006 // A type name in a declare reduction directive cannot be a function type, an
11007 // array type, a reference type, or a type qualified with const, volatile or
11008 // restrict.
11009 if (ReductionType.hasQualifiers()) {
11010 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11011 return QualType();
11012 }
11013
11014 if (ReductionType->isFunctionType()) {
11015 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11016 return QualType();
11017 }
11018 if (ReductionType->isReferenceType()) {
11019 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11020 return QualType();
11021 }
11022 if (ReductionType->isArrayType()) {
11023 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11024 return QualType();
11025 }
11026 return ReductionType;
11027}
11028
11029Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11030 Scope *S, DeclContext *DC, DeclarationName Name,
11031 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11032 AccessSpecifier AS, Decl *PrevDeclInScope) {
11033 SmallVector<Decl *, 8> Decls;
11034 Decls.reserve(ReductionTypes.size());
11035
11036 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11037 ForRedeclaration);
11038 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11039 // A reduction-identifier may not be re-declared in the current scope for the
11040 // same type or for a type that is compatible according to the base language
11041 // rules.
11042 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11043 OMPDeclareReductionDecl *PrevDRD = nullptr;
11044 bool InCompoundScope = true;
11045 if (S != nullptr) {
11046 // Find previous declaration with the same name not referenced in other
11047 // declarations.
11048 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11049 InCompoundScope =
11050 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11051 LookupName(Lookup, S);
11052 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11053 /*AllowInlineNamespace=*/false);
11054 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11055 auto Filter = Lookup.makeFilter();
11056 while (Filter.hasNext()) {
11057 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11058 if (InCompoundScope) {
11059 auto I = UsedAsPrevious.find(PrevDecl);
11060 if (I == UsedAsPrevious.end())
11061 UsedAsPrevious[PrevDecl] = false;
11062 if (auto *D = PrevDecl->getPrevDeclInScope())
11063 UsedAsPrevious[D] = true;
11064 }
11065 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11066 PrevDecl->getLocation();
11067 }
11068 Filter.done();
11069 if (InCompoundScope) {
11070 for (auto &PrevData : UsedAsPrevious) {
11071 if (!PrevData.second) {
11072 PrevDRD = PrevData.first;
11073 break;
11074 }
11075 }
11076 }
11077 } else if (PrevDeclInScope != nullptr) {
11078 auto *PrevDRDInScope = PrevDRD =
11079 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11080 do {
11081 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11082 PrevDRDInScope->getLocation();
11083 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11084 } while (PrevDRDInScope != nullptr);
11085 }
11086 for (auto &TyData : ReductionTypes) {
11087 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11088 bool Invalid = false;
11089 if (I != PreviousRedeclTypes.end()) {
11090 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11091 << TyData.first;
11092 Diag(I->second, diag::note_previous_definition);
11093 Invalid = true;
11094 }
11095 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11096 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11097 Name, TyData.first, PrevDRD);
11098 DC->addDecl(DRD);
11099 DRD->setAccess(AS);
11100 Decls.push_back(DRD);
11101 if (Invalid)
11102 DRD->setInvalidDecl();
11103 else
11104 PrevDRD = DRD;
11105 }
11106
11107 return DeclGroupPtrTy::make(
11108 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11109}
11110
11111void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11112 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11113
11114 // Enter new function scope.
11115 PushFunctionScope();
11116 getCurFunction()->setHasBranchProtectedScope();
11117 getCurFunction()->setHasOMPDeclareReductionCombiner();
11118
11119 if (S != nullptr)
11120 PushDeclContext(S, DRD);
11121 else
11122 CurContext = DRD;
11123
11124 PushExpressionEvaluationContext(PotentiallyEvaluated);
11125
11126 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011127 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11128 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11129 // uses semantics of argument handles by value, but it should be passed by
11130 // reference. C lang does not support references, so pass all parameters as
11131 // pointers.
11132 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011133 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011134 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011135 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11136 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11137 // uses semantics of argument handles by value, but it should be passed by
11138 // reference. C lang does not support references, so pass all parameters as
11139 // pointers.
11140 // Create 'T omp_out;' variable.
11141 auto *OmpOutParm =
11142 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11143 if (S != nullptr) {
11144 PushOnScopeChains(OmpInParm, S);
11145 PushOnScopeChains(OmpOutParm, S);
11146 } else {
11147 DRD->addDecl(OmpInParm);
11148 DRD->addDecl(OmpOutParm);
11149 }
11150}
11151
11152void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11153 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11154 DiscardCleanupsInEvaluationContext();
11155 PopExpressionEvaluationContext();
11156
11157 PopDeclContext();
11158 PopFunctionScopeInfo();
11159
11160 if (Combiner != nullptr)
11161 DRD->setCombiner(Combiner);
11162 else
11163 DRD->setInvalidDecl();
11164}
11165
11166void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11167 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11168
11169 // Enter new function scope.
11170 PushFunctionScope();
11171 getCurFunction()->setHasBranchProtectedScope();
11172
11173 if (S != nullptr)
11174 PushDeclContext(S, DRD);
11175 else
11176 CurContext = DRD;
11177
11178 PushExpressionEvaluationContext(PotentiallyEvaluated);
11179
11180 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011181 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11182 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11183 // uses semantics of argument handles by value, but it should be passed by
11184 // reference. C lang does not support references, so pass all parameters as
11185 // pointers.
11186 // Create 'T omp_priv;' variable.
11187 auto *OmpPrivParm =
11188 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011189 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11190 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11191 // uses semantics of argument handles by value, but it should be passed by
11192 // reference. C lang does not support references, so pass all parameters as
11193 // pointers.
11194 // Create 'T omp_orig;' variable.
11195 auto *OmpOrigParm =
11196 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011197 if (S != nullptr) {
11198 PushOnScopeChains(OmpPrivParm, S);
11199 PushOnScopeChains(OmpOrigParm, S);
11200 } else {
11201 DRD->addDecl(OmpPrivParm);
11202 DRD->addDecl(OmpOrigParm);
11203 }
11204}
11205
11206void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11207 Expr *Initializer) {
11208 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11209 DiscardCleanupsInEvaluationContext();
11210 PopExpressionEvaluationContext();
11211
11212 PopDeclContext();
11213 PopFunctionScopeInfo();
11214
11215 if (Initializer != nullptr)
11216 DRD->setInitializer(Initializer);
11217 else
11218 DRD->setInvalidDecl();
11219}
11220
11221Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11222 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11223 for (auto *D : DeclReductions.get()) {
11224 if (IsValid) {
11225 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11226 if (S != nullptr)
11227 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11228 } else
11229 D->setInvalidDecl();
11230 }
11231 return DeclReductions;
11232}
11233
Kelvin Li099bb8c2015-11-24 20:50:12 +000011234OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11235 SourceLocation StartLoc,
11236 SourceLocation LParenLoc,
11237 SourceLocation EndLoc) {
11238 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011239
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011240 // OpenMP [teams Constrcut, Restrictions]
11241 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011242 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11243 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011244 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011245
11246 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11247}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011248
11249OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11250 SourceLocation StartLoc,
11251 SourceLocation LParenLoc,
11252 SourceLocation EndLoc) {
11253 Expr *ValExpr = ThreadLimit;
11254
11255 // OpenMP [teams Constrcut, Restrictions]
11256 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011257 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11258 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011259 return nullptr;
11260
11261 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11262 EndLoc);
11263}
Alexey Bataeva0569352015-12-01 10:17:31 +000011264
11265OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11266 SourceLocation StartLoc,
11267 SourceLocation LParenLoc,
11268 SourceLocation EndLoc) {
11269 Expr *ValExpr = Priority;
11270
11271 // OpenMP [2.9.1, task Constrcut]
11272 // The priority-value is a non-negative numerical scalar expression.
11273 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11274 /*StrictlyPositive=*/false))
11275 return nullptr;
11276
11277 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11278}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011279
11280OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11281 SourceLocation StartLoc,
11282 SourceLocation LParenLoc,
11283 SourceLocation EndLoc) {
11284 Expr *ValExpr = Grainsize;
11285
11286 // OpenMP [2.9.2, taskloop Constrcut]
11287 // The parameter of the grainsize clause must be a positive integer
11288 // expression.
11289 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11290 /*StrictlyPositive=*/true))
11291 return nullptr;
11292
11293 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11294}
Alexey Bataev382967a2015-12-08 12:06:20 +000011295
11296OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11297 SourceLocation StartLoc,
11298 SourceLocation LParenLoc,
11299 SourceLocation EndLoc) {
11300 Expr *ValExpr = NumTasks;
11301
11302 // OpenMP [2.9.2, taskloop Constrcut]
11303 // The parameter of the num_tasks clause must be a positive integer
11304 // expression.
11305 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11306 /*StrictlyPositive=*/true))
11307 return nullptr;
11308
11309 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11310}
11311
Alexey Bataev28c75412015-12-15 08:19:24 +000011312OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11313 SourceLocation LParenLoc,
11314 SourceLocation EndLoc) {
11315 // OpenMP [2.13.2, critical construct, Description]
11316 // ... where hint-expression is an integer constant expression that evaluates
11317 // to a valid lock hint.
11318 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11319 if (HintExpr.isInvalid())
11320 return nullptr;
11321 return new (Context)
11322 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11323}
11324
Carlo Bertollib4adf552016-01-15 18:50:31 +000011325OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11326 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11327 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11328 SourceLocation EndLoc) {
11329 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11330 std::string Values;
11331 Values += "'";
11332 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11333 Values += "'";
11334 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11335 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11336 return nullptr;
11337 }
11338 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011339 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011340 if (ChunkSize) {
11341 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11342 !ChunkSize->isInstantiationDependent() &&
11343 !ChunkSize->containsUnexpandedParameterPack()) {
11344 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11345 ExprResult Val =
11346 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11347 if (Val.isInvalid())
11348 return nullptr;
11349
11350 ValExpr = Val.get();
11351
11352 // OpenMP [2.7.1, Restrictions]
11353 // chunk_size must be a loop invariant integer expression with a positive
11354 // value.
11355 llvm::APSInt Result;
11356 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11357 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11358 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11359 << "dist_schedule" << ChunkSize->getSourceRange();
11360 return nullptr;
11361 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011362 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11363 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011364 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11365 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11366 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011367 }
11368 }
11369 }
11370
11371 return new (Context)
11372 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011373 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011374}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011375
11376OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11377 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11378 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11379 SourceLocation KindLoc, SourceLocation EndLoc) {
11380 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11381 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11382 Kind != OMPC_DEFAULTMAP_scalar) {
11383 std::string Value;
11384 SourceLocation Loc;
11385 Value += "'";
11386 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11387 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11388 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11389 Loc = MLoc;
11390 } else {
11391 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11392 OMPC_DEFAULTMAP_scalar);
11393 Loc = KindLoc;
11394 }
11395 Value += "'";
11396 Diag(Loc, diag::err_omp_unexpected_clause_value)
11397 << Value << getOpenMPClauseName(OMPC_defaultmap);
11398 return nullptr;
11399 }
11400
11401 return new (Context)
11402 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11403}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011404
11405bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11406 DeclContext *CurLexicalContext = getCurLexicalContext();
11407 if (!CurLexicalContext->isFileContext() &&
11408 !CurLexicalContext->isExternCContext() &&
11409 !CurLexicalContext->isExternCXXContext()) {
11410 Diag(Loc, diag::err_omp_region_not_file_context);
11411 return false;
11412 }
11413 if (IsInOpenMPDeclareTargetContext) {
11414 Diag(Loc, diag::err_omp_enclosed_declare_target);
11415 return false;
11416 }
11417
11418 IsInOpenMPDeclareTargetContext = true;
11419 return true;
11420}
11421
11422void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11423 assert(IsInOpenMPDeclareTargetContext &&
11424 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11425
11426 IsInOpenMPDeclareTargetContext = false;
11427}
11428
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011429void
11430Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11431 const DeclarationNameInfo &Id,
11432 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11433 NamedDeclSetType &SameDirectiveDecls) {
11434 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11435 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11436
11437 if (Lookup.isAmbiguous())
11438 return;
11439 Lookup.suppressDiagnostics();
11440
11441 if (!Lookup.isSingleResult()) {
11442 if (TypoCorrection Corrected =
11443 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11444 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11445 CTK_ErrorRecovery)) {
11446 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11447 << Id.getName());
11448 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11449 return;
11450 }
11451
11452 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11453 return;
11454 }
11455
11456 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11457 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11458 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11459 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11460
11461 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11462 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11463 ND->addAttr(A);
11464 if (ASTMutationListener *ML = Context.getASTMutationListener())
11465 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11466 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11467 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11468 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11469 << Id.getName();
11470 }
11471 } else
11472 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11473}
11474
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011475static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11476 Sema &SemaRef, Decl *D) {
11477 if (!D)
11478 return;
11479 Decl *LD = nullptr;
11480 if (isa<TagDecl>(D)) {
11481 LD = cast<TagDecl>(D)->getDefinition();
11482 } else if (isa<VarDecl>(D)) {
11483 LD = cast<VarDecl>(D)->getDefinition();
11484
11485 // If this is an implicit variable that is legal and we do not need to do
11486 // anything.
11487 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011488 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11489 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11490 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011491 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011492 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011493 return;
11494 }
11495
11496 } else if (isa<FunctionDecl>(D)) {
11497 const FunctionDecl *FD = nullptr;
11498 if (cast<FunctionDecl>(D)->hasBody(FD))
11499 LD = const_cast<FunctionDecl *>(FD);
11500
11501 // If the definition is associated with the current declaration in the
11502 // target region (it can be e.g. a lambda) that is legal and we do not need
11503 // to do anything else.
11504 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011505 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11506 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11507 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011508 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011509 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011510 return;
11511 }
11512 }
11513 if (!LD)
11514 LD = D;
11515 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11516 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11517 // Outlined declaration is not declared target.
11518 if (LD->isOutOfLine()) {
11519 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11520 SemaRef.Diag(SL, diag::note_used_here) << SR;
11521 } else {
11522 DeclContext *DC = LD->getDeclContext();
11523 while (DC) {
11524 if (isa<FunctionDecl>(DC) &&
11525 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11526 break;
11527 DC = DC->getParent();
11528 }
11529 if (DC)
11530 return;
11531
11532 // Is not declared in target context.
11533 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11534 SemaRef.Diag(SL, diag::note_used_here) << SR;
11535 }
11536 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011537 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11538 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11539 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011540 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011541 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011542 }
11543}
11544
11545static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11546 Sema &SemaRef, DSAStackTy *Stack,
11547 ValueDecl *VD) {
11548 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11549 return true;
11550 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11551 return false;
11552 return true;
11553}
11554
11555void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11556 if (!D || D->isInvalidDecl())
11557 return;
11558 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11559 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11560 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11561 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11562 if (DSAStack->isThreadPrivate(VD)) {
11563 Diag(SL, diag::err_omp_threadprivate_in_target);
11564 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11565 return;
11566 }
11567 }
11568 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11569 // Problem if any with var declared with incomplete type will be reported
11570 // as normal, so no need to check it here.
11571 if ((E || !VD->getType()->isIncompleteType()) &&
11572 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11573 // Mark decl as declared target to prevent further diagnostic.
11574 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011575 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11576 Context, OMPDeclareTargetDeclAttr::MT_To);
11577 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011578 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011579 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011580 }
11581 return;
11582 }
11583 }
11584 if (!E) {
11585 // Checking declaration inside declare target region.
11586 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11587 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011588 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11589 Context, OMPDeclareTargetDeclAttr::MT_To);
11590 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011591 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011592 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011593 }
11594 return;
11595 }
11596 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11597}
Samuel Antao661c0902016-05-26 17:39:58 +000011598
11599OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11600 SourceLocation StartLoc,
11601 SourceLocation LParenLoc,
11602 SourceLocation EndLoc) {
11603 MappableVarListInfo MVLI(VarList);
11604 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11605 if (MVLI.ProcessedVarList.empty())
11606 return nullptr;
11607
11608 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11609 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11610 MVLI.VarComponents);
11611}
Samuel Antaoec172c62016-05-26 17:49:04 +000011612
11613OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11614 SourceLocation StartLoc,
11615 SourceLocation LParenLoc,
11616 SourceLocation EndLoc) {
11617 MappableVarListInfo MVLI(VarList);
11618 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11619 if (MVLI.ProcessedVarList.empty())
11620 return nullptr;
11621
11622 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11623 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11624 MVLI.VarComponents);
11625}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011626
11627OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11628 SourceLocation StartLoc,
11629 SourceLocation LParenLoc,
11630 SourceLocation EndLoc) {
11631 SmallVector<Expr *, 8> Vars;
11632 for (auto &RefExpr : VarList) {
11633 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11634 SourceLocation ELoc;
11635 SourceRange ERange;
11636 Expr *SimpleRefExpr = RefExpr;
11637 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11638 if (Res.second) {
11639 // It will be analyzed later.
11640 Vars.push_back(RefExpr);
11641 }
11642 ValueDecl *D = Res.first;
11643 if (!D)
11644 continue;
11645
11646 QualType Type = D->getType();
11647 // item should be a pointer or reference to pointer
11648 if (!Type.getNonReferenceType()->isPointerType()) {
11649 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11650 << 0 << RefExpr->getSourceRange();
11651 continue;
11652 }
11653 Vars.push_back(RefExpr->IgnoreParens());
11654 }
11655
11656 if (Vars.empty())
11657 return nullptr;
11658
11659 return OMPUseDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11660 Vars);
11661}