blob: ea193e61f29a0078000d965448dcee7f993efe8a [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000018#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000025#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000027#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000028#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000031#include "clang/Sema/Scope.h"
32#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000033#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000034using namespace clang;
35
Alexey Bataev758e55e2013-09-06 18:03:48 +000036//===----------------------------------------------------------------------===//
37// Stack of data-sharing attributes for variables
38//===----------------------------------------------------------------------===//
39
40namespace {
41/// \brief Default data sharing attributes, which can be applied to directive.
42enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000043 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000046};
Alexey Bataev7ff55242014-06-19 09:13:45 +000047
Alexey Bataev758e55e2013-09-06 18:03:48 +000048/// \brief Stack for tracking declarations used in OpenMP directives and
49/// clauses and their data-sharing attributes.
Alexey Bataev7ace49d2016-05-17 08:55:33 +000050class DSAStackTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000051public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000052 struct DSAVarData final {
53 OpenMPDirectiveKind DKind = OMPD_unknown;
54 OpenMPClauseKind CKind = OMPC_unknown;
55 Expr *RefExpr = nullptr;
56 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000057 SourceLocation ImplicitDSALoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000058 DSAVarData() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000059 };
Alexey Bataev8b427062016-05-25 12:36:08 +000060 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
61 OperatorOffsetTy;
Alexey Bataeved09d242014-05-28 05:53:51 +000062
Alexey Bataev758e55e2013-09-06 18:03:48 +000063private:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000064 struct DSAInfo final {
65 OpenMPClauseKind Attributes = OMPC_unknown;
66 /// Pointer to a reference expression and a flag which shows that the
67 /// variable is marked as lastprivate(true) or not (false).
68 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000070 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000071 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000073 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Samuel Antao90927002016-04-26 14:54:23 +000075 typedef llvm::DenseMap<
76 ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists>
77 MappedExprComponentsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000078 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
79 CriticalsWithHintsTy;
Alexey Bataev8b427062016-05-25 12:36:08 +000080 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
81 DoacrossDependMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000082
Alexey Bataev7ace49d2016-05-17 08:55:33 +000083 struct SharingMapTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000085 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +000086 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000087 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000088 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000090 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000092 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +000094 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
95 /// get the data (loop counters etc.) about enclosing loop-based construct.
96 /// This data is required during codegen.
97 DoacrossDependMapTy DoacrossDepends;
Alexey Bataev346265e2015-09-25 10:37:12 +000098 /// \brief first argument (Expr *) contains optional argument of the
99 /// 'ordered' clause, the second one is true if the regions has 'ordered'
100 /// clause, false otherwise.
101 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000102 bool NowaitRegion = false;
103 bool CancelRegion = false;
104 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000105 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000106 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000108 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
109 ConstructLoc(Loc) {}
110 SharingMapTy() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000111 };
112
Axel Naumann323862e2016-02-03 10:45:22 +0000113 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114
115 /// \brief Stack of used declaration and their data-sharing attributes.
116 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000117 /// \brief true, if check for DSA must be from parent directive, false, if
118 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000119 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000120 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000121 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000122 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123
124 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
125
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000126 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000127
128 /// \brief Checks if the variable is a local for OpenMP region.
129 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000130
Alexey Bataev758e55e2013-09-06 18:03:48 +0000131public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000132 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
135 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000137 bool isForceVarCapturing() const { return ForceCapturing; }
138 void setForceVarCapturing(bool V) { ForceCapturing = V; }
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000141 Scope *CurScope, SourceLocation Loc) {
142 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
143 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 }
145
146 void pop() {
147 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
148 Stack.pop_back();
149 }
150
Alexey Bataev28c75412015-12-15 08:19:24 +0000151 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
152 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
153 }
154 const std::pair<OMPCriticalDirective *, llvm::APSInt>
155 getCriticalWithHint(const DeclarationNameInfo &Name) const {
156 auto I = Criticals.find(Name.getAsString());
157 if (I != Criticals.end())
158 return I->second;
159 return std::make_pair(nullptr, llvm::APSInt());
160 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000161 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000162 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000163 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000164 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000165
Alexey Bataev9c821032015-04-30 04:23:23 +0000166 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000167 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000168 /// \brief Check if the specified variable is a loop control variable for
169 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000170 /// \return The index of the loop control variable in the list of associated
171 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000172 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000173 /// \brief Check if the specified variable is a loop control variable for
174 /// parent region.
175 /// \return The index of the loop control variable in the list of associated
176 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000177 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000178 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
179 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000180 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000181
Alexey Bataev758e55e2013-09-06 18:03:48 +0000182 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000183 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
184 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000185
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186 /// \brief Returns data sharing attributes from top of the stack for the
187 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000188 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000189 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000190 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 /// \brief Checks if the specified variables has data-sharing attributes which
192 /// match specified \a CPred predicate in any directive which matches \a DPred
193 /// predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000194 DSAVarData hasDSA(ValueDecl *D,
195 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
196 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
197 bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000198 /// \brief Checks if the specified variables has data-sharing attributes which
199 /// match specified \a CPred predicate in any innermost directive which
200 /// matches \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000201 DSAVarData
202 hasInnermostDSA(ValueDecl *D,
203 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
204 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
205 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000206 /// \brief Checks if the specified variables has explicit data-sharing
207 /// attributes which match specified \a CPred predicate at the specified
208 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000209 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000210 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000211 unsigned Level, bool NotLastprivate = false);
Samuel Antao4be30e92015-10-02 17:14:03 +0000212
213 /// \brief Returns true if the directive at level \Level matches in the
214 /// specified \a DPred predicate.
215 bool hasExplicitDirective(
216 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
217 unsigned Level);
218
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000219 /// \brief Finds a directive which matches specified \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000220 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
221 const DeclarationNameInfo &,
222 SourceLocation)> &DPred,
223 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000224
Alexey Bataev758e55e2013-09-06 18:03:48 +0000225 /// \brief Returns currently analyzed directive.
226 OpenMPDirectiveKind getCurrentDirective() const {
227 return Stack.back().Directive;
228 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000229 /// \brief Returns parent directive.
230 OpenMPDirectiveKind getParentDirective() const {
231 if (Stack.size() > 2)
232 return Stack[Stack.size() - 2].Directive;
233 return OMPD_unknown;
234 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000235
236 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000237 void setDefaultDSANone(SourceLocation Loc) {
238 Stack.back().DefaultAttr = DSA_none;
239 Stack.back().DefaultAttrLoc = Loc;
240 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000241 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 void setDefaultDSAShared(SourceLocation Loc) {
243 Stack.back().DefaultAttr = DSA_shared;
244 Stack.back().DefaultAttrLoc = Loc;
245 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246
247 DefaultDataSharingAttributes getDefaultDSA() const {
248 return Stack.back().DefaultAttr;
249 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000250 SourceLocation getDefaultDSALocation() const {
251 return Stack.back().DefaultAttrLoc;
252 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000253
Alexey Bataevf29276e2014-06-18 04:14:57 +0000254 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000255 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000256 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000257 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000258 }
259
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000260 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000261 void setOrderedRegion(bool IsOrdered, Expr *Param) {
262 Stack.back().OrderedRegion.setInt(IsOrdered);
263 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000264 }
265 /// \brief Returns true, if parent region is ordered (has associated
266 /// 'ordered' clause), false - otherwise.
267 bool isParentOrderedRegion() const {
268 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000269 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000270 return false;
271 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000272 /// \brief Returns optional parameter for the ordered region.
273 Expr *getParentOrderedRegionParam() const {
274 if (Stack.size() > 2)
275 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
276 return nullptr;
277 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000278 /// \brief Marks current region as nowait (it has a 'nowait' clause).
279 void setNowaitRegion(bool IsNowait = true) {
280 Stack.back().NowaitRegion = IsNowait;
281 }
282 /// \brief Returns true, if parent region is nowait (has associated
283 /// 'nowait' clause), false - otherwise.
284 bool isParentNowaitRegion() const {
285 if (Stack.size() > 2)
286 return Stack[Stack.size() - 2].NowaitRegion;
287 return false;
288 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000289 /// \brief Marks parent region as cancel region.
290 void setParentCancelRegion(bool Cancel = true) {
291 if (Stack.size() > 2)
292 Stack[Stack.size() - 2].CancelRegion =
293 Stack[Stack.size() - 2].CancelRegion || Cancel;
294 }
295 /// \brief Return true if current region has inner cancel construct.
296 bool isCancelRegion() const {
297 return Stack.back().CancelRegion;
298 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000299
Alexey Bataev9c821032015-04-30 04:23:23 +0000300 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000301 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000302 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000303 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000304
Alexey Bataev13314bf2014-10-09 04:18:56 +0000305 /// \brief Marks current target region as one with closely nested teams
306 /// region.
307 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
308 if (Stack.size() > 2)
309 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
310 }
311 /// \brief Returns true, if current region has closely nested teams region.
312 bool hasInnerTeamsRegion() const {
313 return getInnerTeamsRegionLoc().isValid();
314 }
315 /// \brief Returns location of the nested teams region (if any).
316 SourceLocation getInnerTeamsRegionLoc() const {
317 if (Stack.size() > 1)
318 return Stack.back().InnerTeamsRegionLoc;
319 return SourceLocation();
320 }
321
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000322 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000324 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000325
Samuel Antao90927002016-04-26 14:54:23 +0000326 // Do the check specified in \a Check to all component lists and return true
327 // if any issue is found.
328 bool checkMappableExprComponentListsForDecl(
329 ValueDecl *VD, bool CurrentRegionOnly,
330 const llvm::function_ref<bool(
331 OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000332 auto SI = Stack.rbegin();
333 auto SE = Stack.rend();
334
335 if (SI == SE)
336 return false;
337
338 if (CurrentRegionOnly) {
339 SE = std::next(SI);
340 } else {
341 ++SI;
342 }
343
344 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000345 auto MI = SI->MappedExprComponents.find(VD);
346 if (MI != SI->MappedExprComponents.end())
347 for (auto &L : MI->second)
348 if (Check(L))
Samuel Antao5de996e2016-01-22 20:21:36 +0000349 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000350 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000351 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000352 }
353
Samuel Antao90927002016-04-26 14:54:23 +0000354 // Create a new mappable expression component list associated with a given
355 // declaration and initialize it with the provided list of components.
356 void addMappableExpressionComponents(
357 ValueDecl *VD,
358 OMPClauseMappableExprCommon::MappableExprComponentListRef Components) {
359 assert(Stack.size() > 1 &&
360 "Not expecting to retrieve components from a empty stack!");
361 auto &MEC = Stack.back().MappedExprComponents[VD];
362 // Create new entry and append the new components there.
363 MEC.resize(MEC.size() + 1);
364 MEC.back().append(Components.begin(), Components.end());
Kelvin Li0bff7af2015-11-23 05:32:03 +0000365 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000366
367 unsigned getNestingLevel() const {
368 assert(Stack.size() > 1);
369 return Stack.size() - 2;
370 }
Alexey Bataev8b427062016-05-25 12:36:08 +0000371 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
372 assert(Stack.size() > 2);
373 assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive));
374 Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
375 }
376 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
377 getDoacrossDependClauses() const {
378 assert(Stack.size() > 1);
379 if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
380 auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
381 return llvm::make_range(Ref.begin(), Ref.end());
382 }
383 return llvm::make_range(Stack[0].DoacrossDepends.end(),
384 Stack[0].DoacrossDepends.end());
385 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000387bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000388 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
389 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000390}
Alexey Bataeved09d242014-05-28 05:53:51 +0000391} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000393static ValueDecl *getCanonicalDecl(ValueDecl *D) {
394 auto *VD = dyn_cast<VarDecl>(D);
395 auto *FD = dyn_cast<FieldDecl>(D);
396 if (VD != nullptr) {
397 VD = VD->getCanonicalDecl();
398 D = VD;
399 } else {
400 assert(FD);
401 FD = FD->getCanonicalDecl();
402 D = FD;
403 }
404 return D;
405}
406
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000407DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000408 ValueDecl *D) {
409 D = getCanonicalDecl(D);
410 auto *VD = dyn_cast<VarDecl>(D);
411 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000412 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000413 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000414 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
415 // in a region but not in construct]
416 // File-scope or namespace-scope variables referenced in called routines
417 // in the region are shared unless they appear in a threadprivate
418 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000419 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 DVar.CKind = OMPC_shared;
421
422 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
423 // in a region but not in construct]
424 // Variables with static storage duration that are declared in called
425 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000426 if (VD && VD->hasGlobalStorage())
427 DVar.CKind = OMPC_shared;
428
429 // Non-static data members are shared by default.
430 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000431 DVar.CKind = OMPC_shared;
432
Alexey Bataev758e55e2013-09-06 18:03:48 +0000433 return DVar;
434 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000437 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
438 // in a Construct, C/C++, predetermined, p.1]
439 // Variables with automatic storage duration that are declared in a scope
440 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000441 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
442 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000443 DVar.CKind = OMPC_private;
444 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000445 }
446
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 // Explicitly specified attributes and local variables with predetermined
448 // attributes.
449 if (Iter->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000450 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000451 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000452 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000453 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 return DVar;
455 }
456
457 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
458 // in a Construct, C/C++, implicitly determined, p.1]
459 // In a parallel or task construct, the data-sharing attributes of these
460 // variables are determined by the default clause, if present.
461 switch (Iter->DefaultAttr) {
462 case DSA_shared:
463 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000464 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 return DVar;
466 case DSA_none:
467 return DVar;
468 case DSA_unspecified:
469 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470 // in a Construct, implicitly determined, p.2]
471 // In a parallel construct, if no default clause is present, these
472 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000473 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000474 if (isOpenMPParallelDirective(DVar.DKind) ||
475 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 DVar.CKind = OMPC_shared;
477 return DVar;
478 }
479
480 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
481 // in a Construct, implicitly determined, p.4]
482 // In a task construct, if no default clause is present, a variable that in
483 // the enclosing context is determined to be shared by all implicit tasks
484 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000485 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000486 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000487 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000488 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000489 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000490 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 // In a task construct, if no default clause is present, a variable
492 // whose data-sharing attribute is not determined by the rules above is
493 // firstprivate.
494 DVarTemp = getDSA(I, D);
495 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000496 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000497 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000498 return DVar;
499 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000500 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000501 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000502 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000504 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000505 return DVar;
506 }
507 }
508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
509 // in a Construct, implicitly determined, p.3]
510 // For constructs other than task, if no default clause is present, these
511 // variables inherit their data-sharing attributes from the enclosing
512 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000513 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000514}
515
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000516Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000517 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000518 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000519 auto It = Stack.back().AlignedMap.find(D);
520 if (It == Stack.back().AlignedMap.end()) {
521 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
522 Stack.back().AlignedMap[D] = NewDE;
523 return nullptr;
524 } else {
525 assert(It->second && "Unexpected nullptr expr in the aligned map");
526 return It->second;
527 }
528 return nullptr;
529}
530
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000531void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000532 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000533 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000534 Stack.back().LCVMap.insert(
535 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000536}
537
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000538DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000539 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000540 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000541 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
542 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000543}
544
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000545DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000546 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000547 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000548 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
549 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000550 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000551}
552
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000553ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000554 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
555 if (Stack[Stack.size() - 2].LCVMap.size() < I)
556 return nullptr;
557 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000558 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000559 return Pair.first;
560 }
561 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000562}
563
Alexey Bataev90c228f2016-02-08 09:29:13 +0000564void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
565 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000566 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000567 if (A == OMPC_threadprivate) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000568 auto &Data = Stack[0].SharingMap[D];
569 Data.Attributes = A;
570 Data.RefExpr.setPointer(E);
571 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000572 } else {
573 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000574 auto &Data = Stack.back().SharingMap[D];
575 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
576 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
577 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
578 (isLoopControlVariable(D).first && A == OMPC_private));
579 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
580 Data.RefExpr.setInt(/*IntVal=*/true);
581 return;
582 }
583 const bool IsLastprivate =
584 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
585 Data.Attributes = A;
586 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
587 Data.PrivateCopy = PrivateCopy;
588 if (PrivateCopy) {
589 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
590 Data.Attributes = A;
591 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
592 Data.PrivateCopy = nullptr;
593 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594 }
595}
596
Alexey Bataeved09d242014-05-28 05:53:51 +0000597bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000598 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000599 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000600 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000601 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000602 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000603 ++I;
604 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000605 if (I == E)
606 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000607 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000608 Scope *CurScope = getCurScope();
609 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000610 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000611 }
612 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000613 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000614 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000615}
616
Alexey Bataev39f915b82015-05-08 10:41:21 +0000617/// \brief Build a variable declaration for OpenMP loop iteration variable.
618static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000619 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000620 DeclContext *DC = SemaRef.CurContext;
621 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
622 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
623 VarDecl *Decl =
624 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000625 if (Attrs) {
626 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
627 I != E; ++I)
628 Decl->addAttr(*I);
629 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000630 Decl->setImplicit();
631 return Decl;
632}
633
634static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
635 SourceLocation Loc,
636 bool RefersToCapture = false) {
637 D->setReferenced();
638 D->markUsed(S.Context);
639 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
640 SourceLocation(), D, RefersToCapture, Loc, Ty,
641 VK_LValue);
642}
643
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000644DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
645 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000646 DSAVarData DVar;
647
648 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
649 // in a Construct, C/C++, predetermined, p.1]
650 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000651 auto *VD = dyn_cast<VarDecl>(D);
652 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
653 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000654 SemaRef.getLangOpts().OpenMPUseTLS &&
655 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000656 (VD && VD->getStorageClass() == SC_Register &&
657 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
658 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000659 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000660 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000661 }
662 if (Stack[0].SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000663 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 DVar.CKind = OMPC_threadprivate;
665 return DVar;
666 }
667
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000668 if (Stack.size() == 1) {
669 // Not in OpenMP execution region and top scope was already checked.
670 return DVar;
671 }
672
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000674 // in a Construct, C/C++, predetermined, p.4]
675 // Static data members are shared.
676 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
677 // in a Construct, C/C++, predetermined, p.7]
678 // Variables with static storage duration that are declared in a scope
679 // inside the construct are shared.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000680 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000681 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000682 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000683 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000684 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000686 DVar.CKind = OMPC_shared;
687 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000688 }
689
690 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000691 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
692 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000693 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
694 // in a Construct, C/C++, predetermined, p.6]
695 // Variables with const qualified type having no mutable member are
696 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000697 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000698 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000699 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
700 if (auto *CTD = CTSD->getSpecializedTemplate())
701 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000703 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
704 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000705 // Variables with const-qualified type having no mutable member may be
706 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000707 DSAVarData DVarTemp = hasDSA(
708 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
709 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000710 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
711 return DVar;
712
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713 DVar.CKind = OMPC_shared;
714 return DVar;
715 }
716
Alexey Bataev758e55e2013-09-06 18:03:48 +0000717 // Explicitly specified attributes and local variables with predetermined
718 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000724 auto I = std::prev(StartI);
725 if (I->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000726 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000727 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000728 DVar.CKind = I->SharingMap[D].Attributes;
729 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000730 }
731
732 return DVar;
733}
734
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000735DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
736 bool FromParent) {
737 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000738 auto StartI = Stack.rbegin();
739 auto EndI = std::prev(Stack.rend());
740 if (FromParent && StartI != EndI) {
741 StartI = std::next(StartI);
742 }
743 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000744}
745
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000746DSAStackTy::DSAVarData
747DSAStackTy::hasDSA(ValueDecl *D,
748 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
749 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
750 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000751 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000752 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000753 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000754 if (FromParent && StartI != EndI) {
755 StartI = std::next(StartI);
756 }
757 for (auto I = StartI, EE = EndI; I != EE; ++I) {
758 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000759 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000760 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000761 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000762 return DVar;
763 }
764 return DSAVarData();
765}
766
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000767DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
768 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
769 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
770 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000771 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000772 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000773 auto EndI = Stack.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +0000774 if (FromParent && StartI != EndI)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000775 StartI = std::next(StartI);
Alexey Bataeve3978122016-07-19 05:06:39 +0000776 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000777 return DSAVarData();
Alexey Bataeve3978122016-07-19 05:06:39 +0000778 DSAVarData DVar = getDSA(StartI, D);
779 return CPred(DVar.CKind) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000780}
781
Alexey Bataevaac108a2015-06-23 04:51:00 +0000782bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000783 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000784 unsigned Level, bool NotLastprivate) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000785 if (CPred(ClauseKindMode))
786 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000787 D = getCanonicalDecl(D);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000788 auto StartI = std::next(Stack.begin());
789 auto EndI = Stack.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000790 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000791 return false;
792 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000793 return (StartI->SharingMap.count(D) > 0) &&
794 StartI->SharingMap[D].RefExpr.getPointer() &&
795 CPred(StartI->SharingMap[D].Attributes) &&
796 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +0000797}
798
Samuel Antao4be30e92015-10-02 17:14:03 +0000799bool DSAStackTy::hasExplicitDirective(
800 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
801 unsigned Level) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000802 auto StartI = std::next(Stack.begin());
803 auto EndI = Stack.end();
Samuel Antao4be30e92015-10-02 17:14:03 +0000804 if (std::distance(StartI, EndI) <= (int)Level)
805 return false;
806 std::advance(StartI, Level);
807 return DPred(StartI->Directive);
808}
809
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000810bool DSAStackTy::hasDirective(
811 const llvm::function_ref<bool(OpenMPDirectiveKind,
812 const DeclarationNameInfo &, SourceLocation)>
813 &DPred,
814 bool FromParent) {
Samuel Antaof0d79752016-05-27 15:21:27 +0000815 // We look only in the enclosing region.
816 if (Stack.size() < 2)
817 return false;
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000818 auto StartI = std::next(Stack.rbegin());
819 auto EndI = std::prev(Stack.rend());
820 if (FromParent && StartI != EndI) {
821 StartI = std::next(StartI);
822 }
823 for (auto I = StartI, EE = EndI; I != EE; ++I) {
824 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
825 return true;
826 }
827 return false;
828}
829
Alexey Bataev758e55e2013-09-06 18:03:48 +0000830void Sema::InitDataSharingAttributesStack() {
831 VarDataSharingAttributesStack = new DSAStackTy(*this);
832}
833
834#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
835
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000836bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000837 assert(LangOpts.OpenMP && "OpenMP is not allowed");
838
839 auto &Ctx = getASTContext();
840 bool IsByRef = true;
841
842 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000843 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000844
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000845 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000846 // This table summarizes how a given variable should be passed to the device
847 // given its type and the clauses where it appears. This table is based on
848 // the description in OpenMP 4.5 [2.10.4, target Construct] and
849 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
850 //
851 // =========================================================================
852 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
853 // | |(tofrom:scalar)| | pvt | | | |
854 // =========================================================================
855 // | scl | | | | - | | bycopy|
856 // | scl | | - | x | - | - | bycopy|
857 // | scl | | x | - | - | - | null |
858 // | scl | x | | | - | | byref |
859 // | scl | x | - | x | - | - | bycopy|
860 // | scl | x | x | - | - | - | null |
861 // | scl | | - | - | - | x | byref |
862 // | scl | x | - | - | - | x | byref |
863 //
864 // | agg | n.a. | | | - | | byref |
865 // | agg | n.a. | - | x | - | - | byref |
866 // | agg | n.a. | x | - | - | - | null |
867 // | agg | n.a. | - | - | - | x | byref |
868 // | agg | n.a. | - | - | - | x[] | byref |
869 //
870 // | ptr | n.a. | | | - | | bycopy|
871 // | ptr | n.a. | - | x | - | - | bycopy|
872 // | ptr | n.a. | x | - | - | - | null |
873 // | ptr | n.a. | - | - | - | x | byref |
874 // | ptr | n.a. | - | - | - | x[] | bycopy|
875 // | ptr | n.a. | - | - | x | | bycopy|
876 // | ptr | n.a. | - | - | x | x | bycopy|
877 // | ptr | n.a. | - | - | x | x[] | bycopy|
878 // =========================================================================
879 // Legend:
880 // scl - scalar
881 // ptr - pointer
882 // agg - aggregate
883 // x - applies
884 // - - invalid in this combination
885 // [] - mapped with an array section
886 // byref - should be mapped by reference
887 // byval - should be mapped by value
888 // null - initialize a local variable to null on the device
889 //
890 // Observations:
891 // - All scalar declarations that show up in a map clause have to be passed
892 // by reference, because they may have been mapped in the enclosing data
893 // environment.
894 // - If the scalar value does not fit the size of uintptr, it has to be
895 // passed by reference, regardless the result in the table above.
896 // - For pointers mapped by value that have either an implicit map or an
897 // array section, the runtime library may pass the NULL value to the
898 // device instead of the value passed to it by the compiler.
899
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000900
901 if (Ty->isReferenceType())
902 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000903
904 // Locate map clauses and see if the variable being captured is referred to
905 // in any of those clauses. Here we only care about variables, not fields,
906 // because fields are part of aggregates.
907 bool IsVariableUsedInMapClause = false;
908 bool IsVariableAssociatedWithSection = false;
909
910 DSAStack->checkMappableExprComponentListsForDecl(
911 D, /*CurrentRegionOnly=*/true,
912 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
913 MapExprComponents) {
914
915 auto EI = MapExprComponents.rbegin();
916 auto EE = MapExprComponents.rend();
917
918 assert(EI != EE && "Invalid map expression!");
919
920 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
921 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
922
923 ++EI;
924 if (EI == EE)
925 return false;
926
927 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
928 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
929 isa<MemberExpr>(EI->getAssociatedExpression())) {
930 IsVariableAssociatedWithSection = true;
931 // There is nothing more we need to know about this variable.
932 return true;
933 }
934
935 // Keep looking for more map info.
936 return false;
937 });
938
939 if (IsVariableUsedInMapClause) {
940 // If variable is identified in a map clause it is always captured by
941 // reference except if it is a pointer that is dereferenced somehow.
942 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
943 } else {
944 // By default, all the data that has a scalar type is mapped by copy.
945 IsByRef = !Ty->isScalarType();
946 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000947 }
948
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000949 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
950 IsByRef = !DSAStack->hasExplicitDSA(
951 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
952 Level, /*NotLastprivate=*/true);
953 }
954
Samuel Antao86ace552016-04-27 22:40:57 +0000955 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000956 // and alignment, because the runtime library only deals with uintptr types.
957 // If it does not fit the uintptr size, we need to pass the data by reference
958 // instead.
959 if (!IsByRef &&
960 (Ctx.getTypeSizeInChars(Ty) >
961 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000962 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000963 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000964 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000965
966 return IsByRef;
967}
968
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000969unsigned Sema::getOpenMPNestingLevel() const {
970 assert(getLangOpts().OpenMP);
971 return DSAStack->getNestingLevel();
972}
973
Alexey Bataev90c228f2016-02-08 09:29:13 +0000974VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000975 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000976 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000977
978 // If we are attempting to capture a global variable in a directive with
979 // 'target' we return true so that this global is also mapped to the device.
980 //
981 // FIXME: If the declaration is enclosed in a 'declare target' directive,
982 // then it should not be captured. Therefore, an extra check has to be
983 // inserted here once support for 'declare target' is added.
984 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000985 auto *VD = dyn_cast<VarDecl>(D);
986 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000987 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000988 !DSAStack->isClauseParsingMode())
989 return VD;
Samuel Antaof0d79752016-05-27 15:21:27 +0000990 if (DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000991 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
992 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000993 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000994 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000995 false))
996 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000997 }
998
Alexey Bataev48977c32015-08-04 08:10:48 +0000999 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1000 (!DSAStack->isClauseParsingMode() ||
1001 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001002 auto &&Info = DSAStack->isLoopControlVariable(D);
1003 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001004 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001005 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001006 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001007 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001008 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001009 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001010 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001011 DVarPrivate = DSAStack->hasDSA(
1012 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1013 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001014 if (DVarPrivate.CKind != OMPC_unknown)
1015 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001016 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001017 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001018}
1019
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001020bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001021 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1022 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001023 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001024}
1025
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001026bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001027 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1028 // Return true if the current level is no longer enclosed in a target region.
1029
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001030 auto *VD = dyn_cast<VarDecl>(D);
1031 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001032 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1033 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001034}
1035
Alexey Bataeved09d242014-05-28 05:53:51 +00001036void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001037
1038void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1039 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001040 Scope *CurScope, SourceLocation Loc) {
1041 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001042 PushExpressionEvaluationContext(PotentiallyEvaluated);
1043}
1044
Alexey Bataevaac108a2015-06-23 04:51:00 +00001045void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1046 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001047}
1048
Alexey Bataevaac108a2015-06-23 04:51:00 +00001049void Sema::EndOpenMPClause() {
1050 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001051}
1052
Alexey Bataev758e55e2013-09-06 18:03:48 +00001053void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001054 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1055 // A variable of class type (or array thereof) that appears in a lastprivate
1056 // clause requires an accessible, unambiguous default constructor for the
1057 // class type, unless the list item is also specified in a firstprivate
1058 // clause.
1059 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001060 for (auto *C : D->clauses()) {
1061 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1062 SmallVector<Expr *, 8> PrivateCopies;
1063 for (auto *DE : Clause->varlists()) {
1064 if (DE->isValueDependent() || DE->isTypeDependent()) {
1065 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001066 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001067 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001068 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001069 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1070 QualType Type = VD->getType().getNonReferenceType();
1071 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001072 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001073 // Generate helper private variable and initialize it with the
1074 // default value. The address of the original variable is replaced
1075 // by the address of the new private variable in CodeGen. This new
1076 // variable is not added to IdResolver, so the code in the OpenMP
1077 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001078 auto *VDPrivate = buildVarDecl(
1079 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001080 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001081 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1082 if (VDPrivate->isInvalidDecl())
1083 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001084 PrivateCopies.push_back(buildDeclRefExpr(
1085 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001086 } else {
1087 // The variable is also a firstprivate, so initialization sequence
1088 // for private copy is generated already.
1089 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001090 }
1091 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001092 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001093 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001094 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001095 }
1096 }
1097 }
1098
Alexey Bataev758e55e2013-09-06 18:03:48 +00001099 DSAStack->pop();
1100 DiscardCleanupsInEvaluationContext();
1101 PopExpressionEvaluationContext();
1102}
1103
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001104static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1105 Expr *NumIterations, Sema &SemaRef,
1106 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001107
Alexey Bataeva769e072013-03-22 06:34:35 +00001108namespace {
1109
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001110class VarDeclFilterCCC : public CorrectionCandidateCallback {
1111private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001112 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001113
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001114public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001115 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001116 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001117 NamedDecl *ND = Candidate.getCorrectionDecl();
1118 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1119 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001120 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1121 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001122 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001123 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001124 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001125};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001126
1127class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1128private:
1129 Sema &SemaRef;
1130
1131public:
1132 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1133 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1134 NamedDecl *ND = Candidate.getCorrectionDecl();
1135 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1136 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1137 SemaRef.getCurScope());
1138 }
1139 return false;
1140 }
1141};
1142
Alexey Bataeved09d242014-05-28 05:53:51 +00001143} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001144
1145ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1146 CXXScopeSpec &ScopeSpec,
1147 const DeclarationNameInfo &Id) {
1148 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1149 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1150
1151 if (Lookup.isAmbiguous())
1152 return ExprError();
1153
1154 VarDecl *VD;
1155 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001156 if (TypoCorrection Corrected = CorrectTypo(
1157 Id, LookupOrdinaryName, CurScope, nullptr,
1158 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001159 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001160 PDiag(Lookup.empty()
1161 ? diag::err_undeclared_var_use_suggest
1162 : diag::err_omp_expected_var_arg_suggest)
1163 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001164 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001165 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001166 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1167 : diag::err_omp_expected_var_arg)
1168 << Id.getName();
1169 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001170 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 } else {
1172 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001173 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001174 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1175 return ExprError();
1176 }
1177 }
1178 Lookup.suppressDiagnostics();
1179
1180 // OpenMP [2.9.2, Syntax, C/C++]
1181 // Variables must be file-scope, namespace-scope, or static block-scope.
1182 if (!VD->hasGlobalStorage()) {
1183 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001184 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1185 bool IsDecl =
1186 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001187 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001188 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1189 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001190 return ExprError();
1191 }
1192
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001193 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1194 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001195 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1196 // A threadprivate directive for file-scope variables must appear outside
1197 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001198 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1199 !getCurLexicalContext()->isTranslationUnit()) {
1200 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001201 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1202 bool IsDecl =
1203 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1204 Diag(VD->getLocation(),
1205 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1206 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001207 return ExprError();
1208 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001209 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1210 // A threadprivate directive for static class member variables must appear
1211 // in the class definition, in the same scope in which the member
1212 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001213 if (CanonicalVD->isStaticDataMember() &&
1214 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1215 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001216 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1217 bool IsDecl =
1218 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1219 Diag(VD->getLocation(),
1220 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1221 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001222 return ExprError();
1223 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001224 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1225 // A threadprivate directive for namespace-scope variables must appear
1226 // outside any definition or declaration other than the namespace
1227 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001228 if (CanonicalVD->getDeclContext()->isNamespace() &&
1229 (!getCurLexicalContext()->isFileContext() ||
1230 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1231 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001232 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1233 bool IsDecl =
1234 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1235 Diag(VD->getLocation(),
1236 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1237 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001238 return ExprError();
1239 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001240 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1241 // A threadprivate directive for static block-scope variables must appear
1242 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001243 if (CanonicalVD->isStaticLocal() && CurScope &&
1244 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001245 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001246 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1247 bool IsDecl =
1248 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1249 Diag(VD->getLocation(),
1250 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1251 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001252 return ExprError();
1253 }
1254
1255 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1256 // A threadprivate directive must lexically precede all references to any
1257 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001258 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001259 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001260 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001261 return ExprError();
1262 }
1263
1264 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001265 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1266 SourceLocation(), VD,
1267 /*RefersToEnclosingVariableOrCapture=*/false,
1268 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001269}
1270
Alexey Bataeved09d242014-05-28 05:53:51 +00001271Sema::DeclGroupPtrTy
1272Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1273 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001274 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001275 CurContext->addDecl(D);
1276 return DeclGroupPtrTy::make(DeclGroupRef(D));
1277 }
David Blaikie0403cb12016-01-15 23:43:25 +00001278 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001279}
1280
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001281namespace {
1282class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1283 Sema &SemaRef;
1284
1285public:
1286 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1287 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1288 if (VD->hasLocalStorage()) {
1289 SemaRef.Diag(E->getLocStart(),
1290 diag::err_omp_local_var_in_threadprivate_init)
1291 << E->getSourceRange();
1292 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1293 << VD << VD->getSourceRange();
1294 return true;
1295 }
1296 }
1297 return false;
1298 }
1299 bool VisitStmt(const Stmt *S) {
1300 for (auto Child : S->children()) {
1301 if (Child && Visit(Child))
1302 return true;
1303 }
1304 return false;
1305 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001306 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001307};
1308} // namespace
1309
Alexey Bataeved09d242014-05-28 05:53:51 +00001310OMPThreadPrivateDecl *
1311Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001312 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001313 for (auto &RefExpr : VarList) {
1314 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001315 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1316 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001317
Alexey Bataev376b4a42016-02-09 09:41:09 +00001318 // Mark variable as used.
1319 VD->setReferenced();
1320 VD->markUsed(Context);
1321
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001322 QualType QType = VD->getType();
1323 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1324 // It will be analyzed later.
1325 Vars.push_back(DE);
1326 continue;
1327 }
1328
Alexey Bataeva769e072013-03-22 06:34:35 +00001329 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1330 // A threadprivate variable must not have an incomplete type.
1331 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001332 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001333 continue;
1334 }
1335
1336 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1337 // A threadprivate variable must not have a reference type.
1338 if (VD->getType()->isReferenceType()) {
1339 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001340 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1341 bool IsDecl =
1342 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1343 Diag(VD->getLocation(),
1344 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1345 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001346 continue;
1347 }
1348
Samuel Antaof8b50122015-07-13 22:54:53 +00001349 // Check if this is a TLS variable. If TLS is not being supported, produce
1350 // the corresponding diagnostic.
1351 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1352 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1353 getLangOpts().OpenMPUseTLS &&
1354 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001355 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1356 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001357 Diag(ILoc, diag::err_omp_var_thread_local)
1358 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001359 bool IsDecl =
1360 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1361 Diag(VD->getLocation(),
1362 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1363 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001364 continue;
1365 }
1366
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001367 // Check if initial value of threadprivate variable reference variable with
1368 // local storage (it is not supported by runtime).
1369 if (auto Init = VD->getAnyInitializer()) {
1370 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001371 if (Checker.Visit(Init))
1372 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001373 }
1374
Alexey Bataeved09d242014-05-28 05:53:51 +00001375 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001376 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001377 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1378 Context, SourceRange(Loc, Loc)));
1379 if (auto *ML = Context.getASTMutationListener())
1380 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001381 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001382 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001383 if (!Vars.empty()) {
1384 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1385 Vars);
1386 D->setAccess(AS_public);
1387 }
1388 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001389}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001390
Alexey Bataev7ff55242014-06-19 09:13:45 +00001391static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001392 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001393 bool IsLoopIterVar = false) {
1394 if (DVar.RefExpr) {
1395 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1396 << getOpenMPClauseName(DVar.CKind);
1397 return;
1398 }
1399 enum {
1400 PDSA_StaticMemberShared,
1401 PDSA_StaticLocalVarShared,
1402 PDSA_LoopIterVarPrivate,
1403 PDSA_LoopIterVarLinear,
1404 PDSA_LoopIterVarLastprivate,
1405 PDSA_ConstVarShared,
1406 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001407 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001408 PDSA_LocalVarPrivate,
1409 PDSA_Implicit
1410 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001411 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001412 auto ReportLoc = D->getLocation();
1413 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001414 if (IsLoopIterVar) {
1415 if (DVar.CKind == OMPC_private)
1416 Reason = PDSA_LoopIterVarPrivate;
1417 else if (DVar.CKind == OMPC_lastprivate)
1418 Reason = PDSA_LoopIterVarLastprivate;
1419 else
1420 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001421 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1422 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001423 Reason = PDSA_TaskVarFirstprivate;
1424 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001425 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001426 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001427 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001428 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001429 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001430 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001431 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001432 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001433 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001434 ReportHint = true;
1435 Reason = PDSA_LocalVarPrivate;
1436 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001437 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001438 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001439 << Reason << ReportHint
1440 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1441 } else if (DVar.ImplicitDSALoc.isValid()) {
1442 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1443 << getOpenMPClauseName(DVar.CKind);
1444 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001445}
1446
Alexey Bataev758e55e2013-09-06 18:03:48 +00001447namespace {
1448class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1449 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001450 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001451 bool ErrorFound;
1452 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001453 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001454 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001455
Alexey Bataev758e55e2013-09-06 18:03:48 +00001456public:
1457 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001458 if (E->isTypeDependent() || E->isValueDependent() ||
1459 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1460 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001461 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001462 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001463 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1464 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001465
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001466 auto DVar = Stack->getTopDSA(VD, false);
1467 // Check if the variable has explicit DSA set and stop analysis if it so.
1468 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001470 auto ELoc = E->getExprLoc();
1471 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001472 // The default(none) clause requires that each variable that is referenced
1473 // in the construct, and does not have a predetermined data-sharing
1474 // attribute, must have its data-sharing attribute explicitly determined
1475 // by being listed in a data-sharing attribute clause.
1476 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001477 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001478 VarsWithInheritedDSA.count(VD) == 0) {
1479 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001480 return;
1481 }
1482
1483 // OpenMP [2.9.3.6, Restrictions, p.2]
1484 // A list item that appears in a reduction clause of the innermost
1485 // enclosing worksharing or parallel construct may not be accessed in an
1486 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001487 DVar = Stack->hasInnermostDSA(
1488 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1489 [](OpenMPDirectiveKind K) -> bool {
1490 return isOpenMPParallelDirective(K) ||
1491 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1492 },
1493 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001494 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001495 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001496 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1497 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001498 return;
1499 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001500
1501 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001502 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001503 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1504 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001505 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001506 }
1507 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001508 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001509 if (E->isTypeDependent() || E->isValueDependent() ||
1510 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1511 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001512 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1513 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1514 auto DVar = Stack->getTopDSA(FD, false);
1515 // Check if the variable has explicit DSA set and stop analysis if it
1516 // so.
1517 if (DVar.RefExpr)
1518 return;
1519
1520 auto ELoc = E->getExprLoc();
1521 auto DKind = Stack->getCurrentDirective();
1522 // OpenMP [2.9.3.6, Restrictions, p.2]
1523 // A list item that appears in a reduction clause of the innermost
1524 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001525 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001526 DVar = Stack->hasInnermostDSA(
1527 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1528 [](OpenMPDirectiveKind K) -> bool {
1529 return isOpenMPParallelDirective(K) ||
1530 isOpenMPWorksharingDirective(K) ||
1531 isOpenMPTeamsDirective(K);
1532 },
1533 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001534 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001535 ErrorFound = true;
1536 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1537 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1538 return;
1539 }
1540
1541 // Define implicit data-sharing attributes for task.
1542 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001543 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1544 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001545 ImplicitFirstprivate.push_back(E);
1546 }
1547 }
1548 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001549 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001550 for (auto *C : S->clauses()) {
1551 // Skip analysis of arguments of implicitly defined firstprivate clause
1552 // for task directives.
1553 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1554 for (auto *CC : C->children()) {
1555 if (CC)
1556 Visit(CC);
1557 }
1558 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001559 }
1560 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001561 for (auto *C : S->children()) {
1562 if (C && !isa<OMPExecutableDirective>(C))
1563 Visit(C);
1564 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001565 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001566
1567 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001568 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001569 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001570 return VarsWithInheritedDSA;
1571 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001572
Alexey Bataev7ff55242014-06-19 09:13:45 +00001573 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1574 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001575};
Alexey Bataeved09d242014-05-28 05:53:51 +00001576} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001577
Alexey Bataevbae9a792014-06-27 10:37:06 +00001578void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001579 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00001580 case OMPD_parallel:
1581 case OMPD_parallel_for:
1582 case OMPD_parallel_for_simd:
1583 case OMPD_parallel_sections:
1584 case OMPD_teams: {
Alexey Bataev9959db52014-05-06 10:08:46 +00001585 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001586 QualType KmpInt32PtrTy =
1587 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001588 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001589 std::make_pair(".global_tid.", KmpInt32PtrTy),
1590 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1591 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001592 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001593 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1594 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001595 break;
1596 }
Kelvin Li70a12c52016-07-13 21:51:49 +00001597 case OMPD_simd:
1598 case OMPD_for:
1599 case OMPD_for_simd:
1600 case OMPD_sections:
1601 case OMPD_section:
1602 case OMPD_single:
1603 case OMPD_master:
1604 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00001605 case OMPD_taskgroup:
1606 case OMPD_distribute:
Kelvin Li70a12c52016-07-13 21:51:49 +00001607 case OMPD_ordered:
1608 case OMPD_atomic:
1609 case OMPD_target_data:
1610 case OMPD_target:
1611 case OMPD_target_parallel:
1612 case OMPD_target_parallel_for:
Kelvin Li986330c2016-07-20 22:57:10 +00001613 case OMPD_target_parallel_for_simd:
1614 case OMPD_target_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001615 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001616 std::make_pair(StringRef(), QualType()) // __context with shared vars
1617 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001618 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1619 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001620 break;
1621 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001622 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001623 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001624 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1625 FunctionProtoType::ExtProtoInfo EPI;
1626 EPI.Variadic = true;
1627 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001628 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001629 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001630 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1631 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1632 std::make_pair(".copy_fn.",
1633 Context.getPointerType(CopyFnType).withConst()),
1634 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001635 std::make_pair(StringRef(), QualType()) // __context with shared vars
1636 };
1637 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1638 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001639 // Mark this captured region as inlined, because we don't use outlined
1640 // function directly.
1641 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1642 AlwaysInlineAttr::CreateImplicit(
1643 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001644 break;
1645 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001646 case OMPD_taskloop:
1647 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001648 QualType KmpInt32Ty =
1649 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1650 QualType KmpUInt64Ty =
1651 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1652 QualType KmpInt64Ty =
1653 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1654 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1655 FunctionProtoType::ExtProtoInfo EPI;
1656 EPI.Variadic = true;
1657 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001658 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001659 std::make_pair(".global_tid.", KmpInt32Ty),
1660 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1661 std::make_pair(".privates.",
1662 Context.VoidPtrTy.withConst().withRestrict()),
1663 std::make_pair(
1664 ".copy_fn.",
1665 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1666 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1667 std::make_pair(".lb.", KmpUInt64Ty),
1668 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1669 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001670 std::make_pair(StringRef(), QualType()) // __context with shared vars
1671 };
1672 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1673 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001674 // Mark this captured region as inlined, because we don't use outlined
1675 // function directly.
1676 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1677 AlwaysInlineAttr::CreateImplicit(
1678 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001679 break;
1680 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001681 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001682 case OMPD_distribute_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001683 case OMPD_distribute_parallel_for: {
1684 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1685 QualType KmpInt32PtrTy =
1686 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1687 Sema::CapturedParamNameType Params[] = {
1688 std::make_pair(".global_tid.", KmpInt32PtrTy),
1689 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1690 std::make_pair(".previous.lb.", Context.getSizeType()),
1691 std::make_pair(".previous.ub.", Context.getSizeType()),
1692 std::make_pair(StringRef(), QualType()) // __context with shared vars
1693 };
1694 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1695 Params);
1696 break;
1697 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001698 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001699 case OMPD_taskyield:
1700 case OMPD_barrier:
1701 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001702 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001703 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001704 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001705 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001706 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001707 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001708 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001709 case OMPD_declare_target:
1710 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001711 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001712 llvm_unreachable("OpenMP Directive is not allowed");
1713 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001714 llvm_unreachable("Unknown OpenMP directive");
1715 }
1716}
1717
Alexey Bataev3392d762016-02-16 11:18:12 +00001718static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001719 Expr *CaptureExpr, bool WithInit,
1720 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001721 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001722 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001723 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001724 QualType Ty = Init->getType();
1725 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1726 if (S.getLangOpts().CPlusPlus)
1727 Ty = C.getLValueReferenceType(Ty);
1728 else {
1729 Ty = C.getPointerType(Ty);
1730 ExprResult Res =
1731 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1732 if (!Res.isUsable())
1733 return nullptr;
1734 Init = Res.get();
1735 }
Alexey Bataev61205072016-03-02 04:57:40 +00001736 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001737 }
1738 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001739 if (!WithInit)
1740 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001741 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001742 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1743 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001744 return CED;
1745}
1746
Alexey Bataev61205072016-03-02 04:57:40 +00001747static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1748 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001749 OMPCapturedExprDecl *CD;
1750 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1751 CD = cast<OMPCapturedExprDecl>(VD);
1752 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001753 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1754 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001755 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001756 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001757}
1758
Alexey Bataev5a3af132016-03-29 08:58:54 +00001759static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1760 if (!Ref) {
1761 auto *CD =
1762 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1763 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1764 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1765 CaptureExpr->getExprLoc());
1766 }
1767 ExprResult Res = Ref;
1768 if (!S.getLangOpts().CPlusPlus &&
1769 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1770 Ref->getType()->isPointerType())
1771 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1772 if (!Res.isUsable())
1773 return ExprError();
1774 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001775}
1776
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001777StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1778 ArrayRef<OMPClause *> Clauses) {
1779 if (!S.isUsable()) {
1780 ActOnCapturedRegionError();
1781 return StmtError();
1782 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001783
1784 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001785 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001786 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001787 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001788 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001789 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001790 Clause->getClauseKind() == OMPC_copyprivate ||
1791 (getLangOpts().OpenMPUseTLS &&
1792 getASTContext().getTargetInfo().isTLSSupported() &&
1793 Clause->getClauseKind() == OMPC_copyin)) {
1794 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001795 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001796 for (auto *VarRef : Clause->children()) {
1797 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001798 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001799 }
1800 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001801 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001802 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001803 // Mark all variables in private list clauses as used in inner region.
1804 // Required for proper codegen of combined directives.
1805 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001806 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001807 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1808 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001809 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1810 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001811 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001812 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1813 if (auto *E = C->getPostUpdateExpr())
1814 MarkDeclarationsReferencedInExpr(E);
1815 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001816 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001817 if (Clause->getClauseKind() == OMPC_schedule)
1818 SC = cast<OMPScheduleClause>(Clause);
1819 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001820 OC = cast<OMPOrderedClause>(Clause);
1821 else if (Clause->getClauseKind() == OMPC_linear)
1822 LCs.push_back(cast<OMPLinearClause>(Clause));
1823 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001824 bool ErrorFound = false;
1825 // OpenMP, 2.7.1 Loop Construct, Restrictions
1826 // The nonmonotonic modifier cannot be specified if an ordered clause is
1827 // specified.
1828 if (SC &&
1829 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1830 SC->getSecondScheduleModifier() ==
1831 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1832 OC) {
1833 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1834 ? SC->getFirstScheduleModifierLoc()
1835 : SC->getSecondScheduleModifierLoc(),
1836 diag::err_omp_schedule_nonmonotonic_ordered)
1837 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1838 ErrorFound = true;
1839 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001840 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1841 for (auto *C : LCs) {
1842 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1843 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1844 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001845 ErrorFound = true;
1846 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001847 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1848 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1849 OC->getNumForLoops()) {
1850 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1851 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1852 ErrorFound = true;
1853 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001854 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001855 ActOnCapturedRegionError();
1856 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001857 }
1858 return ActOnCapturedRegionEnd(S.get());
1859}
1860
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001861static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1862 OpenMPDirectiveKind CurrentRegion,
1863 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001864 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001865 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001866 // Allowed nesting of constructs
1867 // +------------------+-----------------+------------------------------------+
1868 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1869 // +------------------+-----------------+------------------------------------+
1870 // | parallel | parallel | * |
1871 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001872 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001873 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001874 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001875 // | parallel | simd | * |
1876 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001877 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001878 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001879 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001880 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001881 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001882 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001883 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001884 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001885 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001886 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001887 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001888 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001889 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001890 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001891 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001892 // | parallel | target parallel | * |
1893 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001894 // | parallel | target enter | * |
1895 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001896 // | parallel | target exit | * |
1897 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001898 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001899 // | parallel | cancellation | |
1900 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001901 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001902 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001903 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001904 // | parallel | distribute | + |
1905 // | parallel | distribute | + |
1906 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001907 // | parallel | distribute | + |
1908 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001909 // | parallel | distribute simd | + |
Kelvin Li986330c2016-07-20 22:57:10 +00001910 // | parallel | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001911 // +------------------+-----------------+------------------------------------+
1912 // | for | parallel | * |
1913 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001914 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001915 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001916 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001917 // | for | simd | * |
1918 // | for | sections | + |
1919 // | for | section | + |
1920 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001921 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001922 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001923 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001924 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001925 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001926 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001927 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001928 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001929 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001930 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001931 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001932 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001933 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001934 // | for | target parallel | * |
1935 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001936 // | for | target enter | * |
1937 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001938 // | for | target exit | * |
1939 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001940 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001941 // | for | cancellation | |
1942 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001943 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001944 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001945 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001946 // | for | distribute | + |
1947 // | for | distribute | + |
1948 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001949 // | for | distribute | + |
1950 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001951 // | for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001952 // | for | target parallel | + |
1953 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00001954 // | for | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001955 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001956 // | master | parallel | * |
1957 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001958 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001959 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001960 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001961 // | master | simd | * |
1962 // | master | sections | + |
1963 // | master | section | + |
1964 // | master | single | + |
1965 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001966 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001967 // | master |parallel sections| * |
1968 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001969 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001970 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001971 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001972 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001973 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001974 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001975 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001976 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001977 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001978 // | master | target parallel | * |
1979 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001980 // | master | target enter | * |
1981 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001982 // | master | target exit | * |
1983 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001984 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001985 // | master | cancellation | |
1986 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001987 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001988 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001989 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001990 // | master | distribute | + |
1991 // | master | distribute | + |
1992 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001993 // | master | distribute | + |
1994 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001995 // | master | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001996 // | master | target parallel | + |
1997 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00001998 // | master | target simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001999 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002000 // | critical | parallel | * |
2001 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002002 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002003 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002004 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002005 // | critical | simd | * |
2006 // | critical | sections | + |
2007 // | critical | section | + |
2008 // | critical | single | + |
2009 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002010 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002011 // | critical |parallel sections| * |
2012 // | critical | task | * |
2013 // | critical | taskyield | * |
2014 // | critical | barrier | + |
2015 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002016 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002017 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002018 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002019 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002020 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002021 // | critical | target parallel | * |
2022 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002023 // | critical | target enter | * |
2024 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002025 // | critical | target exit | * |
2026 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002027 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002028 // | critical | cancellation | |
2029 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002030 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002031 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002032 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002033 // | critical | distribute | + |
2034 // | critical | distribute | + |
2035 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002036 // | critical | distribute | + |
2037 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002038 // | critical | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002039 // | critical | target parallel | + |
2040 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002041 // | critical | target simd | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002042 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002043 // | simd | parallel | |
2044 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002045 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002046 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002047 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002048 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002049 // | simd | sections | |
2050 // | simd | section | |
2051 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002052 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002053 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002054 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002055 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002056 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002057 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002058 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002059 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002060 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002061 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002062 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002063 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002064 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002065 // | simd | target parallel | |
2066 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002067 // | simd | target enter | |
2068 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002069 // | simd | target exit | |
2070 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002071 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002072 // | simd | cancellation | |
2073 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002074 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002075 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002076 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002077 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002078 // | simd | distribute | |
2079 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002080 // | simd | distribute | |
2081 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002082 // | simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002083 // | simd | target parallel | |
2084 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002085 // | simd | target simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002086 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002087 // | for simd | parallel | |
2088 // | for simd | for | |
2089 // | for simd | for simd | |
2090 // | for simd | master | |
2091 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002092 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002093 // | for simd | sections | |
2094 // | for simd | section | |
2095 // | for simd | single | |
2096 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002097 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002098 // | for simd |parallel sections| |
2099 // | for simd | task | |
2100 // | for simd | taskyield | |
2101 // | for simd | barrier | |
2102 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002103 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002104 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002105 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002106 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002107 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002108 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002109 // | for simd | target parallel | |
2110 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002111 // | for simd | target enter | |
2112 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002113 // | for simd | target exit | |
2114 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002115 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002116 // | for simd | cancellation | |
2117 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002118 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002119 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002120 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002121 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002122 // | for simd | distribute | |
2123 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002124 // | for simd | distribute | |
2125 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002126 // | for simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002127 // | for simd | target parallel | |
2128 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002129 // | for simd | target simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002130 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002131 // | parallel for simd| parallel | |
2132 // | parallel for simd| for | |
2133 // | parallel for simd| for simd | |
2134 // | parallel for simd| master | |
2135 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002136 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002137 // | parallel for simd| sections | |
2138 // | parallel for simd| section | |
2139 // | parallel for simd| single | |
2140 // | parallel for simd| parallel for | |
2141 // | parallel for simd|parallel for simd| |
2142 // | parallel for simd|parallel sections| |
2143 // | parallel for simd| task | |
2144 // | parallel for simd| taskyield | |
2145 // | parallel for simd| barrier | |
2146 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002147 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002148 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002149 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002150 // | parallel for simd| atomic | |
2151 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002152 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002153 // | parallel for simd| target parallel | |
2154 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002155 // | parallel for simd| target enter | |
2156 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002157 // | parallel for simd| target exit | |
2158 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002159 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002160 // | parallel for simd| cancellation | |
2161 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002162 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002163 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002164 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002165 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002166 // | parallel for simd| distribute | |
2167 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002168 // | parallel for simd| distribute | |
2169 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002170 // | parallel for simd| distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002171 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002172 // | parallel for simd| target simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002173 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002174 // | sections | parallel | * |
2175 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002176 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002177 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002178 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002179 // | sections | simd | * |
2180 // | sections | sections | + |
2181 // | sections | section | * |
2182 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002183 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002184 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002185 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002186 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002187 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002188 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002189 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002190 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002191 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002192 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002193 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002194 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002195 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002196 // | sections | target parallel | * |
2197 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002198 // | sections | target enter | * |
2199 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002200 // | sections | target exit | * |
2201 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002202 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002203 // | sections | cancellation | |
2204 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002205 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002206 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002207 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002208 // | sections | distribute | + |
2209 // | sections | distribute | + |
2210 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002211 // | sections | distribute | + |
2212 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002213 // | sections | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002214 // | sections | target parallel | + |
2215 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002216 // | sections | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002217 // +------------------+-----------------+------------------------------------+
2218 // | section | parallel | * |
2219 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002220 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002221 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002222 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002223 // | section | simd | * |
2224 // | section | sections | + |
2225 // | section | section | + |
2226 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002227 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002228 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002229 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002230 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002231 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002232 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002233 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002234 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002235 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002236 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002237 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002238 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002239 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002240 // | section | target parallel | * |
2241 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002242 // | section | target enter | * |
2243 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002244 // | section | target exit | * |
2245 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002246 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002247 // | section | cancellation | |
2248 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002249 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002250 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002251 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002252 // | section | distribute | + |
2253 // | section | distribute | + |
2254 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002255 // | section | distribute | + |
2256 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002257 // | section | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002258 // | section | target parallel | + |
2259 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002260 // | section | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002261 // +------------------+-----------------+------------------------------------+
2262 // | single | parallel | * |
2263 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002264 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002265 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002266 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002267 // | single | simd | * |
2268 // | single | sections | + |
2269 // | single | section | + |
2270 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002271 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002272 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002273 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002274 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002275 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002276 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002277 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002278 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002279 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002280 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002281 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002282 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002283 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002284 // | single | target parallel | * |
2285 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002286 // | single | target enter | * |
2287 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002288 // | single | target exit | * |
2289 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002290 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002291 // | single | cancellation | |
2292 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002293 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002294 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002295 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002296 // | single | distribute | + |
2297 // | single | distribute | + |
2298 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002299 // | single | distribute | + |
2300 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002301 // | single | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002302 // | single | target parallel | + |
2303 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002304 // | single | target simd | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002305 // +------------------+-----------------+------------------------------------+
2306 // | parallel for | parallel | * |
2307 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002308 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002309 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002310 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002311 // | parallel for | simd | * |
2312 // | parallel for | sections | + |
2313 // | parallel for | section | + |
2314 // | parallel for | single | + |
2315 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002316 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002317 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002318 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002319 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002320 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002321 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002322 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002323 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002324 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002325 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002326 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002327 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002328 // | parallel for | target parallel | * |
2329 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002330 // | parallel for | target enter | * |
2331 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002332 // | parallel for | target exit | * |
2333 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002334 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002335 // | parallel for | cancellation | |
2336 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002337 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002338 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002339 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002340 // | parallel for | distribute | + |
2341 // | parallel for | distribute | + |
2342 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002343 // | parallel for | distribute | + |
2344 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002345 // | parallel for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002346 // | parallel for | target parallel | + |
2347 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002348 // | parallel for | target simd | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002349 // +------------------+-----------------+------------------------------------+
2350 // | parallel sections| parallel | * |
2351 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002352 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002353 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002354 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002355 // | parallel sections| simd | * |
2356 // | parallel sections| sections | + |
2357 // | parallel sections| section | * |
2358 // | parallel sections| single | + |
2359 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002360 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002361 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002362 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002363 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002364 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002365 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002366 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002367 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002368 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002369 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002370 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002371 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002372 // | parallel sections| target parallel | * |
2373 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002374 // | parallel sections| target enter | * |
2375 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002376 // | parallel sections| target exit | * |
2377 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002378 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002379 // | parallel sections| cancellation | |
2380 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002381 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002382 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002383 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002384 // | parallel sections| distribute | + |
2385 // | parallel sections| distribute | + |
2386 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002387 // | parallel sections| distribute | + |
2388 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002389 // | parallel sections| distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002390 // | parallel sections| target parallel | + |
2391 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002392 // | parallel sections| target simd | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002393 // +------------------+-----------------+------------------------------------+
2394 // | task | parallel | * |
2395 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002396 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002397 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002398 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002399 // | task | simd | * |
2400 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002401 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002402 // | task | single | + |
2403 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002404 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002405 // | task |parallel sections| * |
2406 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002407 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002408 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002409 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002410 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002411 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002412 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002413 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002414 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002415 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002416 // | task | target parallel | * |
2417 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002418 // | task | target enter | * |
2419 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002420 // | task | target exit | * |
2421 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002422 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002423 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002424 // | | point | ! |
2425 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002426 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002427 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002428 // | task | distribute | + |
2429 // | task | distribute | + |
2430 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002431 // | task | distribute | + |
2432 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002433 // | task | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002434 // | task | target parallel | + |
2435 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002436 // | task | target simd | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002437 // +------------------+-----------------+------------------------------------+
2438 // | ordered | parallel | * |
2439 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002440 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002441 // | ordered | master | * |
2442 // | ordered | critical | * |
2443 // | ordered | simd | * |
2444 // | ordered | sections | + |
2445 // | ordered | section | + |
2446 // | ordered | single | + |
2447 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002448 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002449 // | ordered |parallel sections| * |
2450 // | ordered | task | * |
2451 // | ordered | taskyield | * |
2452 // | ordered | barrier | + |
2453 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002454 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002455 // | ordered | flush | * |
2456 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002457 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002458 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002459 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002460 // | ordered | target parallel | * |
2461 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002462 // | ordered | target enter | * |
2463 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002464 // | ordered | target exit | * |
2465 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002466 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002467 // | ordered | cancellation | |
2468 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002469 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002470 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002471 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002472 // | ordered | distribute | + |
2473 // | ordered | distribute | + |
2474 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002475 // | ordered | distribute | + |
2476 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002477 // | ordered | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002478 // | ordered | target parallel | + |
2479 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002480 // | ordered | target simd | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002481 // +------------------+-----------------+------------------------------------+
2482 // | atomic | parallel | |
2483 // | atomic | for | |
2484 // | atomic | for simd | |
2485 // | atomic | master | |
2486 // | atomic | critical | |
2487 // | atomic | simd | |
2488 // | atomic | sections | |
2489 // | atomic | section | |
2490 // | atomic | single | |
2491 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002492 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002493 // | atomic |parallel sections| |
2494 // | atomic | task | |
2495 // | atomic | taskyield | |
2496 // | atomic | barrier | |
2497 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002498 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002499 // | atomic | flush | |
2500 // | atomic | ordered | |
2501 // | atomic | atomic | |
2502 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002503 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002504 // | atomic | target parallel | |
2505 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002506 // | atomic | target enter | |
2507 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002508 // | atomic | target exit | |
2509 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002510 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002511 // | atomic | cancellation | |
2512 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002513 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002514 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002515 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002516 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002517 // | atomic | distribute | |
2518 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002519 // | atomic | distribute | |
2520 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002521 // | atomic | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002522 // | atomic | target parallel | |
2523 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002524 // | atomic | target simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002525 // +------------------+-----------------+------------------------------------+
2526 // | target | parallel | * |
2527 // | target | for | * |
2528 // | target | for simd | * |
2529 // | target | master | * |
2530 // | target | critical | * |
2531 // | target | simd | * |
2532 // | target | sections | * |
2533 // | target | section | * |
2534 // | target | single | * |
2535 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002536 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002537 // | target |parallel sections| * |
2538 // | target | task | * |
2539 // | target | taskyield | * |
2540 // | target | barrier | * |
2541 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002542 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002543 // | target | flush | * |
2544 // | target | ordered | * |
2545 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002546 // | target | target | |
2547 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002548 // | target | target parallel | |
2549 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002550 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002551 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002552 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002553 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002554 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002555 // | target | cancellation | |
2556 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002557 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002558 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002559 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002560 // | target | distribute | + |
2561 // | target | distribute | + |
2562 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002563 // | target | distribute | + |
2564 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002565 // | target | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002566 // | target | target parallel | |
2567 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002568 // | target | target simd | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002569 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002570 // | target parallel | parallel | * |
2571 // | target parallel | for | * |
2572 // | target parallel | for simd | * |
2573 // | target parallel | master | * |
2574 // | target parallel | critical | * |
2575 // | target parallel | simd | * |
2576 // | target parallel | sections | * |
2577 // | target parallel | section | * |
2578 // | target parallel | single | * |
2579 // | target parallel | parallel for | * |
2580 // | target parallel |parallel for simd| * |
2581 // | target parallel |parallel sections| * |
2582 // | target parallel | task | * |
2583 // | target parallel | taskyield | * |
2584 // | target parallel | barrier | * |
2585 // | target parallel | taskwait | * |
2586 // | target parallel | taskgroup | * |
2587 // | target parallel | flush | * |
2588 // | target parallel | ordered | * |
2589 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002590 // | target parallel | target | |
2591 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002592 // | target parallel | target parallel | |
2593 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002594 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002595 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002596 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002597 // | | data | |
2598 // | target parallel | teams | |
2599 // | target parallel | cancellation | |
2600 // | | point | ! |
2601 // | target parallel | cancel | ! |
2602 // | target parallel | taskloop | * |
2603 // | target parallel | taskloop simd | * |
2604 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002605 // | target parallel | distribute | |
2606 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002607 // | target parallel | distribute | |
2608 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002609 // | target parallel | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002610 // | target parallel | target parallel | |
2611 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002612 // | target parallel | target simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002613 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002614 // | target parallel | parallel | * |
2615 // | for | | |
2616 // | target parallel | for | * |
2617 // | for | | |
2618 // | target parallel | for simd | * |
2619 // | for | | |
2620 // | target parallel | master | * |
2621 // | for | | |
2622 // | target parallel | critical | * |
2623 // | for | | |
2624 // | target parallel | simd | * |
2625 // | for | | |
2626 // | target parallel | sections | * |
2627 // | for | | |
2628 // | target parallel | section | * |
2629 // | for | | |
2630 // | target parallel | single | * |
2631 // | for | | |
2632 // | target parallel | parallel for | * |
2633 // | for | | |
2634 // | target parallel |parallel for simd| * |
2635 // | for | | |
2636 // | target parallel |parallel sections| * |
2637 // | for | | |
2638 // | target parallel | task | * |
2639 // | for | | |
2640 // | target parallel | taskyield | * |
2641 // | for | | |
2642 // | target parallel | barrier | * |
2643 // | for | | |
2644 // | target parallel | taskwait | * |
2645 // | for | | |
2646 // | target parallel | taskgroup | * |
2647 // | for | | |
2648 // | target parallel | flush | * |
2649 // | for | | |
2650 // | target parallel | ordered | * |
2651 // | for | | |
2652 // | target parallel | atomic | * |
2653 // | for | | |
2654 // | target parallel | target | |
2655 // | for | | |
2656 // | target parallel | target parallel | |
2657 // | for | | |
2658 // | target parallel | target parallel | |
2659 // | for | for | |
2660 // | target parallel | target enter | |
2661 // | for | data | |
2662 // | target parallel | target exit | |
2663 // | for | data | |
2664 // | target parallel | teams | |
2665 // | for | | |
2666 // | target parallel | cancellation | |
2667 // | for | point | ! |
2668 // | target parallel | cancel | ! |
2669 // | for | | |
2670 // | target parallel | taskloop | * |
2671 // | for | | |
2672 // | target parallel | taskloop simd | * |
2673 // | for | | |
2674 // | target parallel | distribute | |
2675 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002676 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002677 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002678 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002679 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002680 // | target parallel | distribute simd | |
2681 // | for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002682 // | target parallel | target parallel | |
2683 // | for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002684 // | target parallel | target simd | |
2685 // | for | | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002686 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002687 // | teams | parallel | * |
2688 // | teams | for | + |
2689 // | teams | for simd | + |
2690 // | teams | master | + |
2691 // | teams | critical | + |
2692 // | teams | simd | + |
2693 // | teams | sections | + |
2694 // | teams | section | + |
2695 // | teams | single | + |
2696 // | teams | parallel for | * |
2697 // | teams |parallel for simd| * |
2698 // | teams |parallel sections| * |
2699 // | teams | task | + |
2700 // | teams | taskyield | + |
2701 // | teams | barrier | + |
2702 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002703 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002704 // | teams | flush | + |
2705 // | teams | ordered | + |
2706 // | teams | atomic | + |
2707 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002708 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002709 // | teams | target parallel | + |
2710 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002711 // | teams | target enter | + |
2712 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002713 // | teams | target exit | + |
2714 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002715 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002716 // | teams | cancellation | |
2717 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002718 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002719 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002720 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002721 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002722 // | teams | distribute | ! |
2723 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002724 // | teams | distribute | ! |
2725 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002726 // | teams | distribute simd | ! |
Kelvin Lia579b912016-07-14 02:54:56 +00002727 // | teams | target parallel | + |
2728 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002729 // | teams | target simd | + |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002730 // +------------------+-----------------+------------------------------------+
2731 // | taskloop | parallel | * |
2732 // | taskloop | for | + |
2733 // | taskloop | for simd | + |
2734 // | taskloop | master | + |
2735 // | taskloop | critical | * |
2736 // | taskloop | simd | * |
2737 // | taskloop | sections | + |
2738 // | taskloop | section | + |
2739 // | taskloop | single | + |
2740 // | taskloop | parallel for | * |
2741 // | taskloop |parallel for simd| * |
2742 // | taskloop |parallel sections| * |
2743 // | taskloop | task | * |
2744 // | taskloop | taskyield | * |
2745 // | taskloop | barrier | + |
2746 // | taskloop | taskwait | * |
2747 // | taskloop | taskgroup | * |
2748 // | taskloop | flush | * |
2749 // | taskloop | ordered | + |
2750 // | taskloop | atomic | * |
2751 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002752 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002753 // | taskloop | target parallel | * |
2754 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002755 // | taskloop | target enter | * |
2756 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002757 // | taskloop | target exit | * |
2758 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002759 // | taskloop | teams | + |
2760 // | taskloop | cancellation | |
2761 // | | point | |
2762 // | taskloop | cancel | |
2763 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002764 // | taskloop | distribute | + |
2765 // | taskloop | distribute | + |
2766 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002767 // | taskloop | distribute | + |
2768 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002769 // | taskloop | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002770 // | taskloop | target parallel | * |
2771 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002772 // | taskloop | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002773 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002774 // | taskloop simd | parallel | |
2775 // | taskloop simd | for | |
2776 // | taskloop simd | for simd | |
2777 // | taskloop simd | master | |
2778 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002779 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002780 // | taskloop simd | sections | |
2781 // | taskloop simd | section | |
2782 // | taskloop simd | single | |
2783 // | taskloop simd | parallel for | |
2784 // | taskloop simd |parallel for simd| |
2785 // | taskloop simd |parallel sections| |
2786 // | taskloop simd | task | |
2787 // | taskloop simd | taskyield | |
2788 // | taskloop simd | barrier | |
2789 // | taskloop simd | taskwait | |
2790 // | taskloop simd | taskgroup | |
2791 // | taskloop simd | flush | |
2792 // | taskloop simd | ordered | + (with simd clause) |
2793 // | taskloop simd | atomic | |
2794 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002795 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002796 // | taskloop simd | target parallel | |
2797 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002798 // | taskloop simd | target enter | |
2799 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002800 // | taskloop simd | target exit | |
2801 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002802 // | taskloop simd | teams | |
2803 // | taskloop simd | cancellation | |
2804 // | | point | |
2805 // | taskloop simd | cancel | |
2806 // | taskloop simd | taskloop | |
2807 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002808 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002809 // | taskloop simd | distribute | |
2810 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002811 // | taskloop simd | distribute | |
2812 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002813 // | taskloop simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002814 // | taskloop simd | target parallel | |
2815 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002816 // | taskloop simd | target simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002817 // +------------------+-----------------+------------------------------------+
2818 // | distribute | parallel | * |
2819 // | distribute | for | * |
2820 // | distribute | for simd | * |
2821 // | distribute | master | * |
2822 // | distribute | critical | * |
2823 // | distribute | simd | * |
2824 // | distribute | sections | * |
2825 // | distribute | section | * |
2826 // | distribute | single | * |
2827 // | distribute | parallel for | * |
2828 // | distribute |parallel for simd| * |
2829 // | distribute |parallel sections| * |
2830 // | distribute | task | * |
2831 // | distribute | taskyield | * |
2832 // | distribute | barrier | * |
2833 // | distribute | taskwait | * |
2834 // | distribute | taskgroup | * |
2835 // | distribute | flush | * |
2836 // | distribute | ordered | + |
2837 // | distribute | atomic | * |
2838 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002839 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002840 // | distribute | target parallel | |
2841 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002842 // | distribute | target enter | |
2843 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002844 // | distribute | target exit | |
2845 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002846 // | distribute | teams | |
2847 // | distribute | cancellation | + |
2848 // | | point | |
2849 // | distribute | cancel | + |
2850 // | distribute | taskloop | * |
2851 // | distribute | taskloop simd | * |
2852 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002853 // | distribute | distribute | |
2854 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002855 // | distribute | distribute | |
2856 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002857 // | distribute | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002858 // | distribute | target parallel | |
2859 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002860 // | distribute | target simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002861 // +------------------+-----------------+------------------------------------+
2862 // | distribute | parallel | * |
2863 // | parallel for | | |
2864 // | distribute | for | * |
2865 // | parallel for | | |
2866 // | distribute | for simd | * |
2867 // | parallel for | | |
2868 // | distribute | master | * |
2869 // | parallel for | | |
2870 // | distribute | critical | * |
2871 // | parallel for | | |
2872 // | distribute | simd | * |
2873 // | parallel for | | |
2874 // | distribute | sections | * |
2875 // | parallel for | | |
2876 // | distribute | section | * |
2877 // | parallel for | | |
2878 // | distribute | single | * |
2879 // | parallel for | | |
2880 // | distribute | parallel for | * |
2881 // | parallel for | | |
2882 // | distribute |parallel for simd| * |
2883 // | parallel for | | |
2884 // | distribute |parallel sections| * |
2885 // | parallel for | | |
2886 // | distribute | task | * |
2887 // | parallel for | | |
2888 // | parallel for | | |
2889 // | distribute | taskyield | * |
2890 // | parallel for | | |
2891 // | distribute | barrier | * |
2892 // | parallel for | | |
2893 // | distribute | taskwait | * |
2894 // | parallel for | | |
2895 // | distribute | taskgroup | * |
2896 // | parallel for | | |
2897 // | distribute | flush | * |
2898 // | parallel for | | |
2899 // | distribute | ordered | + |
2900 // | parallel for | | |
2901 // | distribute | atomic | * |
2902 // | parallel for | | |
2903 // | distribute | target | |
2904 // | parallel for | | |
2905 // | distribute | target parallel | |
2906 // | parallel for | | |
2907 // | distribute | target parallel | |
2908 // | parallel for | for | |
2909 // | distribute | target enter | |
2910 // | parallel for | data | |
2911 // | distribute | target exit | |
2912 // | parallel for | data | |
2913 // | distribute | teams | |
2914 // | parallel for | | |
2915 // | distribute | cancellation | + |
2916 // | parallel for | point | |
2917 // | distribute | cancel | + |
2918 // | parallel for | | |
2919 // | distribute | taskloop | * |
2920 // | parallel for | | |
2921 // | distribute | taskloop simd | * |
2922 // | parallel for | | |
2923 // | distribute | distribute | |
2924 // | parallel for | | |
2925 // | distribute | distribute | |
2926 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002927 // | distribute | distribute | |
2928 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002929 // | distribute | distribute simd | |
2930 // | parallel for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002931 // | distribute | target parallel | |
2932 // | parallel for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002933 // | distribute | target simd | |
2934 // | parallel for | | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002935 // +------------------+-----------------+------------------------------------+
2936 // | distribute | parallel | * |
2937 // | parallel for simd| | |
2938 // | distribute | for | * |
2939 // | parallel for simd| | |
2940 // | distribute | for simd | * |
2941 // | parallel for simd| | |
2942 // | distribute | master | * |
2943 // | parallel for simd| | |
2944 // | distribute | critical | * |
2945 // | parallel for simd| | |
2946 // | distribute | simd | * |
2947 // | parallel for simd| | |
2948 // | distribute | sections | * |
2949 // | parallel for simd| | |
2950 // | distribute | section | * |
2951 // | parallel for simd| | |
2952 // | distribute | single | * |
2953 // | parallel for simd| | |
2954 // | distribute | parallel for | * |
2955 // | parallel for simd| | |
2956 // | distribute |parallel for simd| * |
2957 // | parallel for simd| | |
2958 // | distribute |parallel sections| * |
2959 // | parallel for simd| | |
2960 // | distribute | task | * |
2961 // | parallel for simd| | |
2962 // | distribute | taskyield | * |
2963 // | parallel for simd| | |
2964 // | distribute | barrier | * |
2965 // | parallel for simd| | |
2966 // | distribute | taskwait | * |
2967 // | parallel for simd| | |
2968 // | distribute | taskgroup | * |
2969 // | parallel for simd| | |
2970 // | distribute | flush | * |
2971 // | parallel for simd| | |
2972 // | distribute | ordered | + |
2973 // | parallel for simd| | |
2974 // | distribute | atomic | * |
2975 // | parallel for simd| | |
2976 // | distribute | target | |
2977 // | parallel for simd| | |
2978 // | distribute | target parallel | |
2979 // | parallel for simd| | |
2980 // | distribute | target parallel | |
2981 // | parallel for simd| for | |
2982 // | distribute | target enter | |
2983 // | parallel for simd| data | |
2984 // | distribute | target exit | |
2985 // | parallel for simd| data | |
2986 // | distribute | teams | |
2987 // | parallel for simd| | |
2988 // | distribute | cancellation | + |
2989 // | parallel for simd| point | |
2990 // | distribute | cancel | + |
2991 // | parallel for simd| | |
2992 // | distribute | taskloop | * |
2993 // | parallel for simd| | |
2994 // | distribute | taskloop simd | * |
2995 // | parallel for simd| | |
2996 // | distribute | distribute | |
2997 // | parallel for simd| | |
2998 // | distribute | distribute | * |
2999 // | parallel for simd| parallel for | |
3000 // | distribute | distribute | * |
3001 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003002 // | distribute | distribute simd | * |
3003 // | parallel for simd| | |
Kelvin Lia579b912016-07-14 02:54:56 +00003004 // | distribute | target parallel | |
3005 // | parallel for simd| for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003006 // | distribute | target simd | |
3007 // | parallel for simd| | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003008 // +------------------+-----------------+------------------------------------+
3009 // | distribute simd | parallel | * |
3010 // | distribute simd | for | * |
3011 // | distribute simd | for simd | * |
3012 // | distribute simd | master | * |
3013 // | distribute simd | critical | * |
3014 // | distribute simd | simd | * |
3015 // | distribute simd | sections | * |
3016 // | distribute simd | section | * |
3017 // | distribute simd | single | * |
3018 // | distribute simd | parallel for | * |
3019 // | distribute simd |parallel for simd| * |
3020 // | distribute simd |parallel sections| * |
3021 // | distribute simd | task | * |
3022 // | distribute simd | taskyield | * |
3023 // | distribute simd | barrier | * |
3024 // | distribute simd | taskwait | * |
3025 // | distribute simd | taskgroup | * |
3026 // | distribute simd | flush | * |
3027 // | distribute simd | ordered | + |
3028 // | distribute simd | atomic | * |
3029 // | distribute simd | target | * |
3030 // | distribute simd | target parallel | * |
3031 // | distribute simd | target parallel | * |
3032 // | | for | |
3033 // | distribute simd | target enter | * |
3034 // | | data | |
3035 // | distribute simd | target exit | * |
3036 // | | data | |
3037 // | distribute simd | teams | * |
3038 // | distribute simd | cancellation | + |
3039 // | | point | |
3040 // | distribute simd | cancel | + |
3041 // | distribute simd | taskloop | * |
3042 // | distribute simd | taskloop simd | * |
3043 // | distribute simd | distribute | |
3044 // | distribute simd | distribute | * |
3045 // | | parallel for | |
3046 // | distribute simd | distribute | * |
3047 // | |parallel for simd| |
3048 // | distribute simd | distribute simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003049 // | distribute simd | target parallel | * |
3050 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003051 // | distribute simd | target simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003052 // +------------------+-----------------+------------------------------------+
3053 // | target parallel | parallel | * |
3054 // | for simd | | |
3055 // | target parallel | for | * |
3056 // | for simd | | |
3057 // | target parallel | for simd | * |
3058 // | for simd | | |
3059 // | target parallel | master | * |
3060 // | for simd | | |
3061 // | target parallel | critical | * |
3062 // | for simd | | |
3063 // | target parallel | simd | ! |
3064 // | for simd | | |
3065 // | target parallel | sections | * |
3066 // | for simd | | |
3067 // | target parallel | section | * |
3068 // | for simd | | |
3069 // | target parallel | single | * |
3070 // | for simd | | |
3071 // | target parallel | parallel for | * |
3072 // | for simd | | |
3073 // | target parallel |parallel for simd| * |
3074 // | for simd | | |
3075 // | target parallel |parallel sections| * |
3076 // | for simd | | |
3077 // | target parallel | task | * |
3078 // | for simd | | |
3079 // | target parallel | taskyield | * |
3080 // | for simd | | |
3081 // | target parallel | barrier | * |
3082 // | for simd | | |
3083 // | target parallel | taskwait | * |
3084 // | for simd | | |
3085 // | target parallel | taskgroup | * |
3086 // | for simd | | |
3087 // | target parallel | flush | * |
3088 // | for simd | | |
3089 // | target parallel | ordered | + (with simd clause) |
3090 // | for simd | | |
3091 // | target parallel | atomic | * |
3092 // | for simd | | |
3093 // | target parallel | target | * |
3094 // | for simd | | |
3095 // | target parallel | target parallel | * |
3096 // | for simd | | |
3097 // | target parallel | target parallel | * |
3098 // | for simd | for | |
3099 // | target parallel | target enter | * |
3100 // | for simd | data | |
3101 // | target parallel | target exit | * |
3102 // | for simd | data | |
3103 // | target parallel | teams | * |
3104 // | for simd | | |
3105 // | target parallel | cancellation | * |
3106 // | for simd | point | |
3107 // | target parallel | cancel | * |
3108 // | for simd | | |
3109 // | target parallel | taskloop | * |
3110 // | for simd | | |
3111 // | target parallel | taskloop simd | * |
3112 // | for simd | | |
3113 // | target parallel | distribute | * |
3114 // | for simd | | |
3115 // | target parallel | distribute | * |
3116 // | for simd | parallel for | |
3117 // | target parallel | distribute | * |
3118 // | for simd |parallel for simd| |
3119 // | target parallel | distribute simd | * |
3120 // | for simd | | |
3121 // | target parallel | target parallel | * |
3122 // | for simd | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003123 // | target parallel | target simd | * |
3124 // | for simd | | |
3125 // +------------------+-----------------+------------------------------------+
3126 // | target simd | parallel | |
3127 // | target simd | for | |
3128 // | target simd | for simd | |
3129 // | target simd | master | |
3130 // | target simd | critical | |
3131 // | target simd | simd | |
3132 // | target simd | sections | |
3133 // | target simd | section | |
3134 // | target simd | single | |
3135 // | target simd | parallel for | |
3136 // | target simd |parallel for simd| |
3137 // | target simd |parallel sections| |
3138 // | target simd | task | |
3139 // | target simd | taskyield | |
3140 // | target simd | barrier | |
3141 // | target simd | taskwait | |
3142 // | target simd | taskgroup | |
3143 // | target simd | flush | |
3144 // | target simd | ordered | + (with simd clause) |
3145 // | target simd | atomic | |
3146 // | target simd | target | |
3147 // | target simd | target parallel | |
3148 // | target simd | target parallel | |
3149 // | | for | |
3150 // | target simd | target enter | |
3151 // | | data | |
3152 // | target simd | target exit | |
3153 // | | data | |
3154 // | target simd | teams | |
3155 // | target simd | cancellation | |
3156 // | | point | |
3157 // | target simd | cancel | |
3158 // | target simd | taskloop | |
3159 // | target simd | taskloop simd | |
3160 // | target simd | distribute | |
3161 // | target simd | distribute | |
3162 // | | parallel for | |
3163 // | target simd | distribute | |
3164 // | |parallel for simd| |
3165 // | target simd | distribute simd | |
3166 // | target simd | target parallel | |
3167 // | | for simd | |
3168 // | target simd | target simd | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003169 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003170 if (Stack->getCurScope()) {
3171 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003172 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003173 bool NestingProhibited = false;
3174 bool CloseNesting = true;
Kelvin Li2b51f722016-07-26 04:32:50 +00003175 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003176 enum {
3177 NoRecommend,
3178 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003179 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003180 ShouldBeInTargetRegion,
3181 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003182 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003183 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003184 // OpenMP [2.16, Nesting of Regions]
3185 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003186 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003187 // An ordered construct with the simd clause is the only OpenMP
3188 // construct that can appear in the simd region.
3189 // Allowing a SIMD consruct nested in another SIMD construct is an
3190 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3191 // message.
3192 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3193 ? diag::err_omp_prohibited_region_simd
3194 : diag::warn_omp_nesting_simd);
3195 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003196 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003197 if (ParentRegion == OMPD_atomic) {
3198 // OpenMP [2.16, Nesting of Regions]
3199 // OpenMP constructs may not be nested inside an atomic region.
3200 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3201 return true;
3202 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003203 if (CurrentRegion == OMPD_section) {
3204 // OpenMP [2.7.2, sections Construct, Restrictions]
3205 // Orphaned section directives are prohibited. That is, the section
3206 // directives must appear within the sections construct and must not be
3207 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003208 if (ParentRegion != OMPD_sections &&
3209 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003210 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3211 << (ParentRegion != OMPD_unknown)
3212 << getOpenMPDirectiveName(ParentRegion);
3213 return true;
3214 }
3215 return false;
3216 }
Kelvin Li2b51f722016-07-26 04:32:50 +00003217 // Allow some constructs (except teams) to be orphaned (they could be
3218 // used in functions, called from OpenMP regions with the required
3219 // preconditions).
3220 if (ParentRegion == OMPD_unknown && !isOpenMPTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003221 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003222 if (CurrentRegion == OMPD_cancellation_point ||
3223 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003224 // OpenMP [2.16, Nesting of Regions]
3225 // A cancellation point construct for which construct-type-clause is
3226 // taskgroup must be nested inside a task construct. A cancellation
3227 // point construct for which construct-type-clause is not taskgroup must
3228 // be closely nested inside an OpenMP construct that matches the type
3229 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003230 // A cancel construct for which construct-type-clause is taskgroup must be
3231 // nested inside a task construct. A cancel construct for which
3232 // construct-type-clause is not taskgroup must be closely nested inside an
3233 // OpenMP construct that matches the type specified in
3234 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003235 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003236 !((CancelRegion == OMPD_parallel &&
3237 (ParentRegion == OMPD_parallel ||
3238 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003239 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003240 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3241 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003242 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3243 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003244 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3245 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003246 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003247 // OpenMP [2.16, Nesting of Regions]
3248 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003249 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003250 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003251 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003252 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3253 // OpenMP [2.16, Nesting of Regions]
3254 // A critical region may not be nested (closely or otherwise) inside a
3255 // critical region with the same name. Note that this restriction is not
3256 // sufficient to prevent deadlock.
3257 SourceLocation PreviousCriticalLoc;
3258 bool DeadLock =
3259 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3260 OpenMPDirectiveKind K,
3261 const DeclarationNameInfo &DNI,
3262 SourceLocation Loc)
3263 ->bool {
3264 if (K == OMPD_critical &&
3265 DNI.getName() == CurrentName.getName()) {
3266 PreviousCriticalLoc = Loc;
3267 return true;
3268 } else
3269 return false;
3270 },
3271 false /* skip top directive */);
3272 if (DeadLock) {
3273 SemaRef.Diag(StartLoc,
3274 diag::err_omp_prohibited_region_critical_same_name)
3275 << CurrentName.getName();
3276 if (PreviousCriticalLoc.isValid())
3277 SemaRef.Diag(PreviousCriticalLoc,
3278 diag::note_omp_previous_critical_region);
3279 return true;
3280 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003281 } else if (CurrentRegion == OMPD_barrier) {
3282 // OpenMP [2.16, Nesting of Regions]
3283 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003284 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003285 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3286 isOpenMPTaskingDirective(ParentRegion) ||
3287 ParentRegion == OMPD_master ||
3288 ParentRegion == OMPD_critical ||
3289 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003290 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003291 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003292 // OpenMP [2.16, Nesting of Regions]
3293 // A worksharing region may not be closely nested inside a worksharing,
3294 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003295 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3296 isOpenMPTaskingDirective(ParentRegion) ||
3297 ParentRegion == OMPD_master ||
3298 ParentRegion == OMPD_critical ||
3299 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003300 Recommend = ShouldBeInParallelRegion;
3301 } else if (CurrentRegion == OMPD_ordered) {
3302 // OpenMP [2.16, Nesting of Regions]
3303 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003304 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003305 // An ordered region must be closely nested inside a loop region (or
3306 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003307 // OpenMP [2.8.1,simd Construct, Restrictions]
3308 // An ordered construct with the simd clause is the only OpenMP construct
3309 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003310 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003311 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003312 !(isOpenMPSimdDirective(ParentRegion) ||
3313 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003314 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003315 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3316 // OpenMP [2.16, Nesting of Regions]
3317 // If specified, a teams construct must be contained within a target
3318 // construct.
3319 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003320 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003321 Recommend = ShouldBeInTargetRegion;
3322 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3323 }
3324 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3325 // OpenMP [2.16, Nesting of Regions]
3326 // distribute, parallel, parallel sections, parallel workshare, and the
3327 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3328 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003329 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3330 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003331 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003332 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003333 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3334 // OpenMP 4.5 [2.17 Nesting of Regions]
3335 // The region associated with the distribute construct must be strictly
3336 // nested inside a teams region
3337 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3338 Recommend = ShouldBeInTeamsRegion;
3339 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003340 if (!NestingProhibited &&
3341 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3342 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3343 // OpenMP 4.5 [2.17 Nesting of Regions]
3344 // If a target, target update, target data, target enter data, or
3345 // target exit data construct is encountered during execution of a
3346 // target region, the behavior is unspecified.
3347 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003348 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3349 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003350 if (isOpenMPTargetExecutionDirective(K)) {
3351 OffendingRegion = K;
3352 return true;
3353 } else
3354 return false;
3355 },
3356 false /* don't skip top directive */);
3357 CloseNesting = false;
3358 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003359 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003360 if (OrphanSeen) {
3361 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3362 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3363 } else {
3364 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3365 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3366 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3367 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003368 return true;
3369 }
3370 }
3371 return false;
3372}
3373
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003374static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3375 ArrayRef<OMPClause *> Clauses,
3376 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3377 bool ErrorFound = false;
3378 unsigned NamedModifiersNumber = 0;
3379 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3380 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003381 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003382 for (const auto *C : Clauses) {
3383 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3384 // At most one if clause without a directive-name-modifier can appear on
3385 // the directive.
3386 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3387 if (FoundNameModifiers[CurNM]) {
3388 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3389 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3390 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3391 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003392 } else if (CurNM != OMPD_unknown) {
3393 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003394 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003395 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003396 FoundNameModifiers[CurNM] = IC;
3397 if (CurNM == OMPD_unknown)
3398 continue;
3399 // Check if the specified name modifier is allowed for the current
3400 // directive.
3401 // At most one if clause with the particular directive-name-modifier can
3402 // appear on the directive.
3403 bool MatchFound = false;
3404 for (auto NM : AllowedNameModifiers) {
3405 if (CurNM == NM) {
3406 MatchFound = true;
3407 break;
3408 }
3409 }
3410 if (!MatchFound) {
3411 S.Diag(IC->getNameModifierLoc(),
3412 diag::err_omp_wrong_if_directive_name_modifier)
3413 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3414 ErrorFound = true;
3415 }
3416 }
3417 }
3418 // If any if clause on the directive includes a directive-name-modifier then
3419 // all if clauses on the directive must include a directive-name-modifier.
3420 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3421 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3422 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3423 diag::err_omp_no_more_if_clause);
3424 } else {
3425 std::string Values;
3426 std::string Sep(", ");
3427 unsigned AllowedCnt = 0;
3428 unsigned TotalAllowedNum =
3429 AllowedNameModifiers.size() - NamedModifiersNumber;
3430 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3431 ++Cnt) {
3432 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3433 if (!FoundNameModifiers[NM]) {
3434 Values += "'";
3435 Values += getOpenMPDirectiveName(NM);
3436 Values += "'";
3437 if (AllowedCnt + 2 == TotalAllowedNum)
3438 Values += " or ";
3439 else if (AllowedCnt + 1 != TotalAllowedNum)
3440 Values += Sep;
3441 ++AllowedCnt;
3442 }
3443 }
3444 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3445 diag::err_omp_unnamed_if_clause)
3446 << (TotalAllowedNum > 1) << Values;
3447 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003448 for (auto Loc : NameModifierLoc) {
3449 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3450 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003451 ErrorFound = true;
3452 }
3453 return ErrorFound;
3454}
3455
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003456StmtResult Sema::ActOnOpenMPExecutableDirective(
3457 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3458 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3459 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003460 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003461 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3462 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003463 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003464
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003465 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003466 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003467 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003468 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003469 if (AStmt) {
3470 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3471
3472 // Check default data sharing attributes for referenced variables.
3473 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3474 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3475 if (DSAChecker.isErrorFound())
3476 return StmtError();
3477 // Generate list of implicitly defined firstprivate variables.
3478 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003479
3480 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3481 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3482 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3483 SourceLocation(), SourceLocation())) {
3484 ClausesWithImplicit.push_back(Implicit);
3485 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3486 DSAChecker.getImplicitFirstprivate().size();
3487 } else
3488 ErrorFound = true;
3489 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003490 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003491
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003492 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003493 switch (Kind) {
3494 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003495 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3496 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003497 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003498 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003499 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003500 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3501 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003502 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003503 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003504 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3505 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003506 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003507 case OMPD_for_simd:
3508 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3509 EndLoc, VarsWithInheritedDSA);
3510 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003511 case OMPD_sections:
3512 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3513 EndLoc);
3514 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003515 case OMPD_section:
3516 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003517 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003518 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3519 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003520 case OMPD_single:
3521 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3522 EndLoc);
3523 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003524 case OMPD_master:
3525 assert(ClausesWithImplicit.empty() &&
3526 "No clauses are allowed for 'omp master' directive");
3527 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3528 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003529 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003530 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3531 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003532 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003533 case OMPD_parallel_for:
3534 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3535 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003536 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003537 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003538 case OMPD_parallel_for_simd:
3539 Res = ActOnOpenMPParallelForSimdDirective(
3540 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003541 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003542 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003543 case OMPD_parallel_sections:
3544 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3545 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003546 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003547 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003548 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003549 Res =
3550 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003551 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003552 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003553 case OMPD_taskyield:
3554 assert(ClausesWithImplicit.empty() &&
3555 "No clauses are allowed for 'omp taskyield' directive");
3556 assert(AStmt == nullptr &&
3557 "No associated statement allowed for 'omp taskyield' directive");
3558 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3559 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003560 case OMPD_barrier:
3561 assert(ClausesWithImplicit.empty() &&
3562 "No clauses are allowed for 'omp barrier' directive");
3563 assert(AStmt == nullptr &&
3564 "No associated statement allowed for 'omp barrier' directive");
3565 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3566 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003567 case OMPD_taskwait:
3568 assert(ClausesWithImplicit.empty() &&
3569 "No clauses are allowed for 'omp taskwait' directive");
3570 assert(AStmt == nullptr &&
3571 "No associated statement allowed for 'omp taskwait' directive");
3572 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3573 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003574 case OMPD_taskgroup:
3575 assert(ClausesWithImplicit.empty() &&
3576 "No clauses are allowed for 'omp taskgroup' directive");
3577 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3578 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003579 case OMPD_flush:
3580 assert(AStmt == nullptr &&
3581 "No associated statement allowed for 'omp flush' directive");
3582 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3583 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003584 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003585 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3586 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003587 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003588 case OMPD_atomic:
3589 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3590 EndLoc);
3591 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003592 case OMPD_teams:
3593 Res =
3594 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3595 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003596 case OMPD_target:
3597 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3598 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003599 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003600 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003601 case OMPD_target_parallel:
3602 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3603 StartLoc, EndLoc);
3604 AllowedNameModifiers.push_back(OMPD_target);
3605 AllowedNameModifiers.push_back(OMPD_parallel);
3606 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003607 case OMPD_target_parallel_for:
3608 Res = ActOnOpenMPTargetParallelForDirective(
3609 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3610 AllowedNameModifiers.push_back(OMPD_target);
3611 AllowedNameModifiers.push_back(OMPD_parallel);
3612 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003613 case OMPD_cancellation_point:
3614 assert(ClausesWithImplicit.empty() &&
3615 "No clauses are allowed for 'omp cancellation point' directive");
3616 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3617 "cancellation point' directive");
3618 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3619 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003620 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003621 assert(AStmt == nullptr &&
3622 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003623 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3624 CancelRegion);
3625 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003626 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003627 case OMPD_target_data:
3628 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3629 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003630 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003631 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003632 case OMPD_target_enter_data:
3633 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3634 EndLoc);
3635 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3636 break;
Samuel Antao72590762016-01-19 20:04:50 +00003637 case OMPD_target_exit_data:
3638 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3639 EndLoc);
3640 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3641 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003642 case OMPD_taskloop:
3643 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3644 EndLoc, VarsWithInheritedDSA);
3645 AllowedNameModifiers.push_back(OMPD_taskloop);
3646 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003647 case OMPD_taskloop_simd:
3648 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3649 EndLoc, VarsWithInheritedDSA);
3650 AllowedNameModifiers.push_back(OMPD_taskloop);
3651 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003652 case OMPD_distribute:
3653 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3654 EndLoc, VarsWithInheritedDSA);
3655 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003656 case OMPD_target_update:
3657 assert(!AStmt && "Statement is not allowed for target update");
3658 Res =
3659 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3660 AllowedNameModifiers.push_back(OMPD_target_update);
3661 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003662 case OMPD_distribute_parallel_for:
3663 Res = ActOnOpenMPDistributeParallelForDirective(
3664 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3665 AllowedNameModifiers.push_back(OMPD_parallel);
3666 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003667 case OMPD_distribute_parallel_for_simd:
3668 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3669 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3670 AllowedNameModifiers.push_back(OMPD_parallel);
3671 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003672 case OMPD_distribute_simd:
3673 Res = ActOnOpenMPDistributeSimdDirective(
3674 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3675 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003676 case OMPD_target_parallel_for_simd:
3677 Res = ActOnOpenMPTargetParallelForSimdDirective(
3678 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3679 AllowedNameModifiers.push_back(OMPD_target);
3680 AllowedNameModifiers.push_back(OMPD_parallel);
3681 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003682 case OMPD_target_simd:
3683 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3684 EndLoc, VarsWithInheritedDSA);
3685 AllowedNameModifiers.push_back(OMPD_target);
3686 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003687 case OMPD_declare_target:
3688 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003689 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003690 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003691 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003692 llvm_unreachable("OpenMP Directive is not allowed");
3693 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003694 llvm_unreachable("Unknown OpenMP directive");
3695 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003696
Alexey Bataev4acb8592014-07-07 13:01:15 +00003697 for (auto P : VarsWithInheritedDSA) {
3698 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3699 << P.first << P.second->getSourceRange();
3700 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003701 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3702
3703 if (!AllowedNameModifiers.empty())
3704 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3705 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003706
Alexey Bataeved09d242014-05-28 05:53:51 +00003707 if (ErrorFound)
3708 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003709 return Res;
3710}
3711
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003712Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3713 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003714 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003715 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3716 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003717 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003718 assert(Linears.size() == LinModifiers.size());
3719 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003720 if (!DG || DG.get().isNull())
3721 return DeclGroupPtrTy();
3722
3723 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003724 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003725 return DG;
3726 }
3727 auto *ADecl = DG.get().getSingleDecl();
3728 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3729 ADecl = FTD->getTemplatedDecl();
3730
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003731 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3732 if (!FD) {
3733 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003734 return DeclGroupPtrTy();
3735 }
3736
Alexey Bataev2af33e32016-04-07 12:45:37 +00003737 // OpenMP [2.8.2, declare simd construct, Description]
3738 // The parameter of the simdlen clause must be a constant positive integer
3739 // expression.
3740 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003741 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003742 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003743 // OpenMP [2.8.2, declare simd construct, Description]
3744 // The special this pointer can be used as if was one of the arguments to the
3745 // function in any of the linear, aligned, or uniform clauses.
3746 // The uniform clause declares one or more arguments to have an invariant
3747 // value for all concurrent invocations of the function in the execution of a
3748 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003749 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3750 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003751 for (auto *E : Uniforms) {
3752 E = E->IgnoreParenImpCasts();
3753 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3754 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3755 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3756 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003757 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3758 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003759 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003760 }
3761 if (isa<CXXThisExpr>(E)) {
3762 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003763 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003764 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003765 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3766 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003767 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003768 // OpenMP [2.8.2, declare simd construct, Description]
3769 // The aligned clause declares that the object to which each list item points
3770 // is aligned to the number of bytes expressed in the optional parameter of
3771 // the aligned clause.
3772 // The special this pointer can be used as if was one of the arguments to the
3773 // function in any of the linear, aligned, or uniform clauses.
3774 // The type of list items appearing in the aligned clause must be array,
3775 // pointer, reference to array, or reference to pointer.
3776 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3777 Expr *AlignedThis = nullptr;
3778 for (auto *E : Aligneds) {
3779 E = E->IgnoreParenImpCasts();
3780 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3781 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3782 auto *CanonPVD = PVD->getCanonicalDecl();
3783 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3784 FD->getParamDecl(PVD->getFunctionScopeIndex())
3785 ->getCanonicalDecl() == CanonPVD) {
3786 // OpenMP [2.8.1, simd construct, Restrictions]
3787 // A list-item cannot appear in more than one aligned clause.
3788 if (AlignedArgs.count(CanonPVD) > 0) {
3789 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3790 << 1 << E->getSourceRange();
3791 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3792 diag::note_omp_explicit_dsa)
3793 << getOpenMPClauseName(OMPC_aligned);
3794 continue;
3795 }
3796 AlignedArgs[CanonPVD] = E;
3797 QualType QTy = PVD->getType()
3798 .getNonReferenceType()
3799 .getUnqualifiedType()
3800 .getCanonicalType();
3801 const Type *Ty = QTy.getTypePtrOrNull();
3802 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3803 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3804 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3805 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3806 }
3807 continue;
3808 }
3809 }
3810 if (isa<CXXThisExpr>(E)) {
3811 if (AlignedThis) {
3812 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3813 << 2 << E->getSourceRange();
3814 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3815 << getOpenMPClauseName(OMPC_aligned);
3816 }
3817 AlignedThis = E;
3818 continue;
3819 }
3820 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3821 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3822 }
3823 // The optional parameter of the aligned clause, alignment, must be a constant
3824 // positive integer expression. If no optional parameter is specified,
3825 // implementation-defined default alignments for SIMD instructions on the
3826 // target platforms are assumed.
3827 SmallVector<Expr *, 4> NewAligns;
3828 for (auto *E : Alignments) {
3829 ExprResult Align;
3830 if (E)
3831 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3832 NewAligns.push_back(Align.get());
3833 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003834 // OpenMP [2.8.2, declare simd construct, Description]
3835 // The linear clause declares one or more list items to be private to a SIMD
3836 // lane and to have a linear relationship with respect to the iteration space
3837 // of a loop.
3838 // The special this pointer can be used as if was one of the arguments to the
3839 // function in any of the linear, aligned, or uniform clauses.
3840 // When a linear-step expression is specified in a linear clause it must be
3841 // either a constant integer expression or an integer-typed parameter that is
3842 // specified in a uniform clause on the directive.
3843 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3844 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3845 auto MI = LinModifiers.begin();
3846 for (auto *E : Linears) {
3847 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3848 ++MI;
3849 E = E->IgnoreParenImpCasts();
3850 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3851 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3852 auto *CanonPVD = PVD->getCanonicalDecl();
3853 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3854 FD->getParamDecl(PVD->getFunctionScopeIndex())
3855 ->getCanonicalDecl() == CanonPVD) {
3856 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3857 // A list-item cannot appear in more than one linear clause.
3858 if (LinearArgs.count(CanonPVD) > 0) {
3859 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3860 << getOpenMPClauseName(OMPC_linear)
3861 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3862 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3863 diag::note_omp_explicit_dsa)
3864 << getOpenMPClauseName(OMPC_linear);
3865 continue;
3866 }
3867 // Each argument can appear in at most one uniform or linear clause.
3868 if (UniformedArgs.count(CanonPVD) > 0) {
3869 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3870 << getOpenMPClauseName(OMPC_linear)
3871 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3872 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3873 diag::note_omp_explicit_dsa)
3874 << getOpenMPClauseName(OMPC_uniform);
3875 continue;
3876 }
3877 LinearArgs[CanonPVD] = E;
3878 if (E->isValueDependent() || E->isTypeDependent() ||
3879 E->isInstantiationDependent() ||
3880 E->containsUnexpandedParameterPack())
3881 continue;
3882 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3883 PVD->getOriginalType());
3884 continue;
3885 }
3886 }
3887 if (isa<CXXThisExpr>(E)) {
3888 if (UniformedLinearThis) {
3889 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3890 << getOpenMPClauseName(OMPC_linear)
3891 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3892 << E->getSourceRange();
3893 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3894 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3895 : OMPC_linear);
3896 continue;
3897 }
3898 UniformedLinearThis = E;
3899 if (E->isValueDependent() || E->isTypeDependent() ||
3900 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3901 continue;
3902 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3903 E->getType());
3904 continue;
3905 }
3906 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3907 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3908 }
3909 Expr *Step = nullptr;
3910 Expr *NewStep = nullptr;
3911 SmallVector<Expr *, 4> NewSteps;
3912 for (auto *E : Steps) {
3913 // Skip the same step expression, it was checked already.
3914 if (Step == E || !E) {
3915 NewSteps.push_back(E ? NewStep : nullptr);
3916 continue;
3917 }
3918 Step = E;
3919 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3920 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3921 auto *CanonPVD = PVD->getCanonicalDecl();
3922 if (UniformedArgs.count(CanonPVD) == 0) {
3923 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3924 << Step->getSourceRange();
3925 } else if (E->isValueDependent() || E->isTypeDependent() ||
3926 E->isInstantiationDependent() ||
3927 E->containsUnexpandedParameterPack() ||
3928 CanonPVD->getType()->hasIntegerRepresentation())
3929 NewSteps.push_back(Step);
3930 else {
3931 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3932 << Step->getSourceRange();
3933 }
3934 continue;
3935 }
3936 NewStep = Step;
3937 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3938 !Step->isInstantiationDependent() &&
3939 !Step->containsUnexpandedParameterPack()) {
3940 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3941 .get();
3942 if (NewStep)
3943 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3944 }
3945 NewSteps.push_back(NewStep);
3946 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003947 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3948 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003949 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003950 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3951 const_cast<Expr **>(Linears.data()), Linears.size(),
3952 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3953 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003954 ADecl->addAttr(NewAttr);
3955 return ConvertDeclToDeclGroup(ADecl);
3956}
3957
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003958StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3959 Stmt *AStmt,
3960 SourceLocation StartLoc,
3961 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003962 if (!AStmt)
3963 return StmtError();
3964
Alexey Bataev9959db52014-05-06 10:08:46 +00003965 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3966 // 1.2.2 OpenMP Language Terminology
3967 // Structured block - An executable statement with a single entry at the
3968 // top and a single exit at the bottom.
3969 // The point of exit cannot be a branch out of the structured block.
3970 // longjmp() and throw() must not violate the entry/exit criteria.
3971 CS->getCapturedDecl()->setNothrow();
3972
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003973 getCurFunction()->setHasBranchProtectedScope();
3974
Alexey Bataev25e5b442015-09-15 12:52:43 +00003975 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3976 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003977}
3978
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003979namespace {
3980/// \brief Helper class for checking canonical form of the OpenMP loops and
3981/// extracting iteration space of each loop in the loop nest, that will be used
3982/// for IR generation.
3983class OpenMPIterationSpaceChecker {
3984 /// \brief Reference to Sema.
3985 Sema &SemaRef;
3986 /// \brief A location for diagnostics (when there is no some better location).
3987 SourceLocation DefaultLoc;
3988 /// \brief A location for diagnostics (when increment is not compatible).
3989 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003990 /// \brief A source location for referring to loop init later.
3991 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003992 /// \brief A source location for referring to condition later.
3993 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003994 /// \brief A source location for referring to increment later.
3995 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003996 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003997 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003998 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003999 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004000 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004001 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004002 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004003 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004004 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004005 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004006 /// \brief This flag is true when condition is one of:
4007 /// Var < UB
4008 /// Var <= UB
4009 /// UB > Var
4010 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004011 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004012 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004013 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004014 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004015 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004016
4017public:
4018 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004019 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004020 /// \brief Check init-expr for canonical loop form and save loop counter
4021 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00004022 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004023 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
4024 /// for less/greater and for strict/non-strict comparison.
4025 bool CheckCond(Expr *S);
4026 /// \brief Check incr-expr for canonical loop form and return true if it
4027 /// does not conform, otherwise save loop step (#Step).
4028 bool CheckInc(Expr *S);
4029 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004030 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004031 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004032 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004033 /// \brief Source range of the loop init.
4034 SourceRange GetInitSrcRange() const { return InitSrcRange; }
4035 /// \brief Source range of the loop condition.
4036 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
4037 /// \brief Source range of the loop increment.
4038 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
4039 /// \brief True if the step should be subtracted.
4040 bool ShouldSubtractStep() const { return SubtractStep; }
4041 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004042 Expr *
4043 BuildNumIterations(Scope *S, const bool LimitedType,
4044 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00004045 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004046 Expr *BuildPreCond(Scope *S, Expr *Cond,
4047 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004048 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004049 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
4050 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00004051 /// \brief Build reference expression to the private counter be used for
4052 /// codegen.
4053 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004054 /// \brief Build initization of the counter be used for codegen.
4055 Expr *BuildCounterInit() const;
4056 /// \brief Build step of the counter be used for codegen.
4057 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004058 /// \brief Return true if any expression is dependent.
4059 bool Dependent() const;
4060
4061private:
4062 /// \brief Check the right-hand side of an assignment in the increment
4063 /// expression.
4064 bool CheckIncRHS(Expr *RHS);
4065 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004066 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004067 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00004068 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00004069 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004070 /// \brief Helper to set loop increment.
4071 bool SetStep(Expr *NewStep, bool Subtract);
4072};
4073
4074bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004075 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004076 assert(!LB && !UB && !Step);
4077 return false;
4078 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004079 return LCDecl->getType()->isDependentType() ||
4080 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4081 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004082}
4083
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004084static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004085 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4086 E = ExprTemp->getSubExpr();
4087
4088 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4089 E = MTE->GetTemporaryExpr();
4090
4091 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4092 E = Binder->getSubExpr();
4093
4094 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4095 E = ICE->getSubExprAsWritten();
4096 return E->IgnoreParens();
4097}
4098
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004099bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4100 Expr *NewLCRefExpr,
4101 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004102 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004103 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004104 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004105 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004106 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004107 LCDecl = getCanonicalDecl(NewLCDecl);
4108 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004109 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4110 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004111 if ((Ctor->isCopyOrMoveConstructor() ||
4112 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4113 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004114 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004115 LB = NewLB;
4116 return false;
4117}
4118
4119bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004120 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004121 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004122 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4123 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004124 if (!NewUB)
4125 return true;
4126 UB = NewUB;
4127 TestIsLessOp = LessOp;
4128 TestIsStrictOp = StrictOp;
4129 ConditionSrcRange = SR;
4130 ConditionLoc = SL;
4131 return false;
4132}
4133
4134bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4135 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004136 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004137 if (!NewStep)
4138 return true;
4139 if (!NewStep->isValueDependent()) {
4140 // Check that the step is integer expression.
4141 SourceLocation StepLoc = NewStep->getLocStart();
4142 ExprResult Val =
4143 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4144 if (Val.isInvalid())
4145 return true;
4146 NewStep = Val.get();
4147
4148 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4149 // If test-expr is of form var relational-op b and relational-op is < or
4150 // <= then incr-expr must cause var to increase on each iteration of the
4151 // loop. If test-expr is of form var relational-op b and relational-op is
4152 // > or >= then incr-expr must cause var to decrease on each iteration of
4153 // the loop.
4154 // If test-expr is of form b relational-op var and relational-op is < or
4155 // <= then incr-expr must cause var to decrease on each iteration of the
4156 // loop. If test-expr is of form b relational-op var and relational-op is
4157 // > or >= then incr-expr must cause var to increase on each iteration of
4158 // the loop.
4159 llvm::APSInt Result;
4160 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4161 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4162 bool IsConstNeg =
4163 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004164 bool IsConstPos =
4165 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004166 bool IsConstZero = IsConstant && !Result.getBoolValue();
4167 if (UB && (IsConstZero ||
4168 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004169 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004170 SemaRef.Diag(NewStep->getExprLoc(),
4171 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004172 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004173 SemaRef.Diag(ConditionLoc,
4174 diag::note_omp_loop_cond_requres_compatible_incr)
4175 << TestIsLessOp << ConditionSrcRange;
4176 return true;
4177 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004178 if (TestIsLessOp == Subtract) {
4179 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4180 NewStep).get();
4181 Subtract = !Subtract;
4182 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004183 }
4184
4185 Step = NewStep;
4186 SubtractStep = Subtract;
4187 return false;
4188}
4189
Alexey Bataev9c821032015-04-30 04:23:23 +00004190bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004191 // Check init-expr for canonical loop form and save loop counter
4192 // variable - #Var and its initialization value - #LB.
4193 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4194 // var = lb
4195 // integer-type var = lb
4196 // random-access-iterator-type var = lb
4197 // pointer-type var = lb
4198 //
4199 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004200 if (EmitDiags) {
4201 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4202 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004203 return true;
4204 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004205 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4206 if (!ExprTemp->cleanupsHaveSideEffects())
4207 S = ExprTemp->getSubExpr();
4208
Alexander Musmana5f070a2014-10-01 06:03:56 +00004209 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004210 if (Expr *E = dyn_cast<Expr>(S))
4211 S = E->IgnoreParens();
4212 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004213 if (BO->getOpcode() == BO_Assign) {
4214 auto *LHS = BO->getLHS()->IgnoreParens();
4215 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4216 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4217 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4218 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4219 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4220 }
4221 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4222 if (ME->isArrow() &&
4223 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4224 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4225 }
4226 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004227 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4228 if (DS->isSingleDecl()) {
4229 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004230 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004231 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004232 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004233 SemaRef.Diag(S->getLocStart(),
4234 diag::ext_omp_loop_not_canonical_init)
4235 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004236 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004237 }
4238 }
4239 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004240 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4241 if (CE->getOperator() == OO_Equal) {
4242 auto *LHS = CE->getArg(0);
4243 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4244 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4245 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4246 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4247 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4248 }
4249 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4250 if (ME->isArrow() &&
4251 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4252 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4253 }
4254 }
4255 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004256
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004257 if (Dependent() || SemaRef.CurContext->isDependentContext())
4258 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004259 if (EmitDiags) {
4260 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4261 << S->getSourceRange();
4262 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004263 return true;
4264}
4265
Alexey Bataev23b69422014-06-18 07:08:49 +00004266/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004267/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004268static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004269 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004270 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004271 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004272 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4273 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004274 if ((Ctor->isCopyOrMoveConstructor() ||
4275 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4276 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004277 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004278 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4279 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4280 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4281 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4282 return getCanonicalDecl(ME->getMemberDecl());
4283 return getCanonicalDecl(VD);
4284 }
4285 }
4286 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4287 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4288 return getCanonicalDecl(ME->getMemberDecl());
4289 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004290}
4291
4292bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4293 // Check test-expr for canonical form, save upper-bound UB, flags for
4294 // less/greater and for strict/non-strict comparison.
4295 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4296 // var relational-op b
4297 // b relational-op var
4298 //
4299 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004300 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004301 return true;
4302 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004303 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004304 SourceLocation CondLoc = S->getLocStart();
4305 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4306 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004307 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004308 return SetUB(BO->getRHS(),
4309 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4310 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4311 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004312 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004313 return SetUB(BO->getLHS(),
4314 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4315 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4316 BO->getSourceRange(), BO->getOperatorLoc());
4317 }
4318 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4319 if (CE->getNumArgs() == 2) {
4320 auto Op = CE->getOperator();
4321 switch (Op) {
4322 case OO_Greater:
4323 case OO_GreaterEqual:
4324 case OO_Less:
4325 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004326 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004327 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4328 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4329 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004330 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004331 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4332 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4333 CE->getOperatorLoc());
4334 break;
4335 default:
4336 break;
4337 }
4338 }
4339 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004340 if (Dependent() || SemaRef.CurContext->isDependentContext())
4341 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004342 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004343 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004344 return true;
4345}
4346
4347bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4348 // RHS of canonical loop form increment can be:
4349 // var + incr
4350 // incr + var
4351 // var - incr
4352 //
4353 RHS = RHS->IgnoreParenImpCasts();
4354 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4355 if (BO->isAdditiveOp()) {
4356 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004357 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004358 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004359 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004360 return SetStep(BO->getLHS(), false);
4361 }
4362 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4363 bool IsAdd = CE->getOperator() == OO_Plus;
4364 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004365 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004366 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004367 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004368 return SetStep(CE->getArg(0), false);
4369 }
4370 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004371 if (Dependent() || SemaRef.CurContext->isDependentContext())
4372 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004373 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004374 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004375 return true;
4376}
4377
4378bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4379 // Check incr-expr for canonical loop form and return true if it
4380 // does not conform.
4381 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4382 // ++var
4383 // var++
4384 // --var
4385 // var--
4386 // var += incr
4387 // var -= incr
4388 // var = var + incr
4389 // var = incr + var
4390 // var = var - incr
4391 //
4392 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004393 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004394 return true;
4395 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004396 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4397 if (!ExprTemp->cleanupsHaveSideEffects())
4398 S = ExprTemp->getSubExpr();
4399
Alexander Musmana5f070a2014-10-01 06:03:56 +00004400 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004401 S = S->IgnoreParens();
4402 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004403 if (UO->isIncrementDecrementOp() &&
4404 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004405 return SetStep(
4406 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4407 (UO->isDecrementOp() ? -1 : 1)).get(),
4408 false);
4409 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4410 switch (BO->getOpcode()) {
4411 case BO_AddAssign:
4412 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004413 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004414 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4415 break;
4416 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004417 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004418 return CheckIncRHS(BO->getRHS());
4419 break;
4420 default:
4421 break;
4422 }
4423 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4424 switch (CE->getOperator()) {
4425 case OO_PlusPlus:
4426 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004427 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004428 return SetStep(
4429 SemaRef.ActOnIntegerConstant(
4430 CE->getLocStart(),
4431 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4432 false);
4433 break;
4434 case OO_PlusEqual:
4435 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004436 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004437 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4438 break;
4439 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004440 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004441 return CheckIncRHS(CE->getArg(1));
4442 break;
4443 default:
4444 break;
4445 }
4446 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004447 if (Dependent() || SemaRef.CurContext->isDependentContext())
4448 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004449 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004450 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004451 return true;
4452}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004453
Alexey Bataev5a3af132016-03-29 08:58:54 +00004454static ExprResult
4455tryBuildCapture(Sema &SemaRef, Expr *Capture,
4456 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004457 if (SemaRef.CurContext->isDependentContext())
4458 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004459 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4460 return SemaRef.PerformImplicitConversion(
4461 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4462 /*AllowExplicit=*/true);
4463 auto I = Captures.find(Capture);
4464 if (I != Captures.end())
4465 return buildCapture(SemaRef, Capture, I->second);
4466 DeclRefExpr *Ref = nullptr;
4467 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4468 Captures[Capture] = Ref;
4469 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004470}
4471
Alexander Musmana5f070a2014-10-01 06:03:56 +00004472/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004473Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4474 Scope *S, const bool LimitedType,
4475 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004476 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004477 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004478 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004479 SemaRef.getLangOpts().CPlusPlus) {
4480 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004481 auto *UBExpr = TestIsLessOp ? UB : LB;
4482 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004483 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4484 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004485 if (!Upper || !Lower)
4486 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004487
4488 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4489
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004490 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004491 // BuildBinOp already emitted error, this one is to point user to upper
4492 // and lower bound, and to tell what is passed to 'operator-'.
4493 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4494 << Upper->getSourceRange() << Lower->getSourceRange();
4495 return nullptr;
4496 }
4497 }
4498
4499 if (!Diff.isUsable())
4500 return nullptr;
4501
4502 // Upper - Lower [- 1]
4503 if (TestIsStrictOp)
4504 Diff = SemaRef.BuildBinOp(
4505 S, DefaultLoc, BO_Sub, Diff.get(),
4506 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4507 if (!Diff.isUsable())
4508 return nullptr;
4509
4510 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004511 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4512 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004513 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004514 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004515 if (!Diff.isUsable())
4516 return nullptr;
4517
4518 // Parentheses (for dumping/debugging purposes only).
4519 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4520 if (!Diff.isUsable())
4521 return nullptr;
4522
4523 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004524 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004525 if (!Diff.isUsable())
4526 return nullptr;
4527
Alexander Musman174b3ca2014-10-06 11:16:29 +00004528 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004529 QualType Type = Diff.get()->getType();
4530 auto &C = SemaRef.Context;
4531 bool UseVarType = VarType->hasIntegerRepresentation() &&
4532 C.getTypeSize(Type) > C.getTypeSize(VarType);
4533 if (!Type->isIntegerType() || UseVarType) {
4534 unsigned NewSize =
4535 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4536 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4537 : Type->hasSignedIntegerRepresentation();
4538 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004539 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4540 Diff = SemaRef.PerformImplicitConversion(
4541 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4542 if (!Diff.isUsable())
4543 return nullptr;
4544 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004545 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004546 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004547 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4548 if (NewSize != C.getTypeSize(Type)) {
4549 if (NewSize < C.getTypeSize(Type)) {
4550 assert(NewSize == 64 && "incorrect loop var size");
4551 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4552 << InitSrcRange << ConditionSrcRange;
4553 }
4554 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004555 NewSize, Type->hasSignedIntegerRepresentation() ||
4556 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004557 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4558 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4559 Sema::AA_Converting, true);
4560 if (!Diff.isUsable())
4561 return nullptr;
4562 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004563 }
4564 }
4565
Alexander Musmana5f070a2014-10-01 06:03:56 +00004566 return Diff.get();
4567}
4568
Alexey Bataev5a3af132016-03-29 08:58:54 +00004569Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4570 Scope *S, Expr *Cond,
4571 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004572 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4573 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4574 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004575
Alexey Bataev5a3af132016-03-29 08:58:54 +00004576 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4577 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4578 if (!NewLB.isUsable() || !NewUB.isUsable())
4579 return nullptr;
4580
Alexey Bataev62dbb972015-04-22 11:59:37 +00004581 auto CondExpr = SemaRef.BuildBinOp(
4582 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4583 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004584 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004585 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004586 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4587 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004588 CondExpr = SemaRef.PerformImplicitConversion(
4589 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4590 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004591 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004592 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4593 // Otherwise use original loop conditon and evaluate it in runtime.
4594 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4595}
4596
Alexander Musmana5f070a2014-10-01 06:03:56 +00004597/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004598DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004599 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004600 auto *VD = dyn_cast<VarDecl>(LCDecl);
4601 if (!VD) {
4602 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4603 auto *Ref = buildDeclRefExpr(
4604 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004605 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4606 // If the loop control decl is explicitly marked as private, do not mark it
4607 // as captured again.
4608 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4609 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004610 return Ref;
4611 }
4612 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004613 DefaultLoc);
4614}
4615
4616Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004617 if (LCDecl && !LCDecl->isInvalidDecl()) {
4618 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004619 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004620 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4621 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004622 if (PrivateVar->isInvalidDecl())
4623 return nullptr;
4624 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4625 }
4626 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004627}
4628
4629/// \brief Build initization of the counter be used for codegen.
4630Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4631
4632/// \brief Build step of the counter be used for codegen.
4633Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4634
4635/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004636struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004637 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004638 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004639 /// \brief This expression calculates the number of iterations in the loop.
4640 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004641 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004642 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004643 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004644 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004645 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004646 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004647 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004648 /// \brief This is step for the #CounterVar used to generate its update:
4649 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004650 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004651 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004652 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004653 /// \brief Source range of the loop init.
4654 SourceRange InitSrcRange;
4655 /// \brief Source range of the loop condition.
4656 SourceRange CondSrcRange;
4657 /// \brief Source range of the loop increment.
4658 SourceRange IncSrcRange;
4659};
4660
Alexey Bataev23b69422014-06-18 07:08:49 +00004661} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004662
Alexey Bataev9c821032015-04-30 04:23:23 +00004663void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4664 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4665 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004666 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4667 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004668 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4669 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004670 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4671 if (auto *D = ISC.GetLoopDecl()) {
4672 auto *VD = dyn_cast<VarDecl>(D);
4673 if (!VD) {
4674 if (auto *Private = IsOpenMPCapturedDecl(D))
4675 VD = Private;
4676 else {
4677 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4678 /*WithInit=*/false);
4679 VD = cast<VarDecl>(Ref->getDecl());
4680 }
4681 }
4682 DSAStack->addLoopControlVariable(D, VD);
4683 }
4684 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004685 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004686 }
4687}
4688
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004689/// \brief Called on a for stmt to check and extract its iteration space
4690/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004691static bool CheckOpenMPIterationSpace(
4692 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4693 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004694 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004695 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004696 LoopIterationSpace &ResultIterSpace,
4697 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004698 // OpenMP [2.6, Canonical Loop Form]
4699 // for (init-expr; test-expr; incr-expr) structured-block
4700 auto For = dyn_cast_or_null<ForStmt>(S);
4701 if (!For) {
4702 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004703 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4704 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4705 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4706 if (NestedLoopCount > 1) {
4707 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4708 SemaRef.Diag(DSA.getConstructLoc(),
4709 diag::note_omp_collapse_ordered_expr)
4710 << 2 << CollapseLoopCountExpr->getSourceRange()
4711 << OrderedLoopCountExpr->getSourceRange();
4712 else if (CollapseLoopCountExpr)
4713 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4714 diag::note_omp_collapse_ordered_expr)
4715 << 0 << CollapseLoopCountExpr->getSourceRange();
4716 else
4717 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4718 diag::note_omp_collapse_ordered_expr)
4719 << 1 << OrderedLoopCountExpr->getSourceRange();
4720 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004721 return true;
4722 }
4723 assert(For->getBody());
4724
4725 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4726
4727 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004728 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004729 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004730 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004731
4732 bool HasErrors = false;
4733
4734 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004735 if (auto *LCDecl = ISC.GetLoopDecl()) {
4736 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004737
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004738 // OpenMP [2.6, Canonical Loop Form]
4739 // Var is one of the following:
4740 // A variable of signed or unsigned integer type.
4741 // For C++, a variable of a random access iterator type.
4742 // For C, a variable of a pointer type.
4743 auto VarType = LCDecl->getType().getNonReferenceType();
4744 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4745 !VarType->isPointerType() &&
4746 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4747 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4748 << SemaRef.getLangOpts().CPlusPlus;
4749 HasErrors = true;
4750 }
4751
4752 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4753 // a Construct
4754 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4755 // parallel for construct is (are) private.
4756 // The loop iteration variable in the associated for-loop of a simd
4757 // construct with just one associated for-loop is linear with a
4758 // constant-linear-step that is the increment of the associated for-loop.
4759 // Exclude loop var from the list of variables with implicitly defined data
4760 // sharing attributes.
4761 VarsWithImplicitDSA.erase(LCDecl);
4762
4763 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4764 // in a Construct, C/C++].
4765 // The loop iteration variable in the associated for-loop of a simd
4766 // construct with just one associated for-loop may be listed in a linear
4767 // clause with a constant-linear-step that is the increment of the
4768 // associated for-loop.
4769 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4770 // parallel for construct may be listed in a private or lastprivate clause.
4771 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4772 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4773 // declared in the loop and it is predetermined as a private.
4774 auto PredeterminedCKind =
4775 isOpenMPSimdDirective(DKind)
4776 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4777 : OMPC_private;
4778 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4779 DVar.CKind != PredeterminedCKind) ||
4780 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4781 isOpenMPDistributeDirective(DKind)) &&
4782 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4783 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4784 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4785 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4786 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4787 << getOpenMPClauseName(PredeterminedCKind);
4788 if (DVar.RefExpr == nullptr)
4789 DVar.CKind = PredeterminedCKind;
4790 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4791 HasErrors = true;
4792 } else if (LoopDeclRefExpr != nullptr) {
4793 // Make the loop iteration variable private (for worksharing constructs),
4794 // linear (for simd directives with the only one associated loop) or
4795 // lastprivate (for simd directives with several collapsed or ordered
4796 // loops).
4797 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004798 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4799 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004800 /*FromParent=*/false);
4801 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4802 }
4803
4804 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4805
4806 // Check test-expr.
4807 HasErrors |= ISC.CheckCond(For->getCond());
4808
4809 // Check incr-expr.
4810 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004811 }
4812
Alexander Musmana5f070a2014-10-01 06:03:56 +00004813 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004814 return HasErrors;
4815
Alexander Musmana5f070a2014-10-01 06:03:56 +00004816 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004817 ResultIterSpace.PreCond =
4818 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004819 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004820 DSA.getCurScope(),
4821 (isOpenMPWorksharingDirective(DKind) ||
4822 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4823 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004824 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004825 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004826 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4827 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4828 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4829 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4830 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4831 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4832
Alexey Bataev62dbb972015-04-22 11:59:37 +00004833 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4834 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004835 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004836 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004837 ResultIterSpace.CounterInit == nullptr ||
4838 ResultIterSpace.CounterStep == nullptr);
4839
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004840 return HasErrors;
4841}
4842
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004843/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004844static ExprResult
4845BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4846 ExprResult Start,
4847 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004848 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004849 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4850 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004851 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004852 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004853 VarRef.get()->getType())) {
4854 NewStart = SemaRef.PerformImplicitConversion(
4855 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4856 /*AllowExplicit=*/true);
4857 if (!NewStart.isUsable())
4858 return ExprError();
4859 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004860
4861 auto Init =
4862 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4863 return Init;
4864}
4865
Alexander Musmana5f070a2014-10-01 06:03:56 +00004866/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004867static ExprResult
4868BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4869 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4870 ExprResult Step, bool Subtract,
4871 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004872 // Add parentheses (for debugging purposes only).
4873 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4874 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4875 !Step.isUsable())
4876 return ExprError();
4877
Alexey Bataev5a3af132016-03-29 08:58:54 +00004878 ExprResult NewStep = Step;
4879 if (Captures)
4880 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004881 if (NewStep.isInvalid())
4882 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004883 ExprResult Update =
4884 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004885 if (!Update.isUsable())
4886 return ExprError();
4887
Alexey Bataevc0214e02016-02-16 12:13:49 +00004888 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4889 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004890 ExprResult NewStart = Start;
4891 if (Captures)
4892 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004893 if (NewStart.isInvalid())
4894 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004895
Alexey Bataevc0214e02016-02-16 12:13:49 +00004896 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4897 ExprResult SavedUpdate = Update;
4898 ExprResult UpdateVal;
4899 if (VarRef.get()->getType()->isOverloadableType() ||
4900 NewStart.get()->getType()->isOverloadableType() ||
4901 Update.get()->getType()->isOverloadableType()) {
4902 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4903 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4904 Update =
4905 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4906 if (Update.isUsable()) {
4907 UpdateVal =
4908 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4909 VarRef.get(), SavedUpdate.get());
4910 if (UpdateVal.isUsable()) {
4911 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4912 UpdateVal.get());
4913 }
4914 }
4915 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4916 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004917
Alexey Bataevc0214e02016-02-16 12:13:49 +00004918 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4919 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4920 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4921 NewStart.get(), SavedUpdate.get());
4922 if (!Update.isUsable())
4923 return ExprError();
4924
Alexey Bataev11481f52016-02-17 10:29:05 +00004925 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4926 VarRef.get()->getType())) {
4927 Update = SemaRef.PerformImplicitConversion(
4928 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4929 if (!Update.isUsable())
4930 return ExprError();
4931 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004932
4933 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4934 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004935 return Update;
4936}
4937
4938/// \brief Convert integer expression \a E to make it have at least \a Bits
4939/// bits.
4940static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4941 Sema &SemaRef) {
4942 if (E == nullptr)
4943 return ExprError();
4944 auto &C = SemaRef.Context;
4945 QualType OldType = E->getType();
4946 unsigned HasBits = C.getTypeSize(OldType);
4947 if (HasBits >= Bits)
4948 return ExprResult(E);
4949 // OK to convert to signed, because new type has more bits than old.
4950 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4951 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4952 true);
4953}
4954
4955/// \brief Check if the given expression \a E is a constant integer that fits
4956/// into \a Bits bits.
4957static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4958 if (E == nullptr)
4959 return false;
4960 llvm::APSInt Result;
4961 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4962 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4963 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004964}
4965
Alexey Bataev5a3af132016-03-29 08:58:54 +00004966/// Build preinits statement for the given declarations.
4967static Stmt *buildPreInits(ASTContext &Context,
4968 SmallVectorImpl<Decl *> &PreInits) {
4969 if (!PreInits.empty()) {
4970 return new (Context) DeclStmt(
4971 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4972 SourceLocation(), SourceLocation());
4973 }
4974 return nullptr;
4975}
4976
4977/// Build preinits statement for the given declarations.
4978static Stmt *buildPreInits(ASTContext &Context,
4979 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4980 if (!Captures.empty()) {
4981 SmallVector<Decl *, 16> PreInits;
4982 for (auto &Pair : Captures)
4983 PreInits.push_back(Pair.second->getDecl());
4984 return buildPreInits(Context, PreInits);
4985 }
4986 return nullptr;
4987}
4988
4989/// Build postupdate expression for the given list of postupdates expressions.
4990static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4991 Expr *PostUpdate = nullptr;
4992 if (!PostUpdates.empty()) {
4993 for (auto *E : PostUpdates) {
4994 Expr *ConvE = S.BuildCStyleCastExpr(
4995 E->getExprLoc(),
4996 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4997 E->getExprLoc(), E)
4998 .get();
4999 PostUpdate = PostUpdate
5000 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5001 PostUpdate, ConvE)
5002 .get()
5003 : ConvE;
5004 }
5005 }
5006 return PostUpdate;
5007}
5008
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005009/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005010/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5011/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005012static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00005013CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
5014 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5015 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005016 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005017 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005018 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005019 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005020 // Found 'collapse' clause - calculate collapse number.
5021 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005022 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005023 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005024 }
5025 if (OrderedLoopCountExpr) {
5026 // Found 'ordered' clause - calculate collapse number.
5027 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005028 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
5029 if (Result.getLimitedValue() < NestedLoopCount) {
5030 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5031 diag::err_omp_wrong_ordered_loop_count)
5032 << OrderedLoopCountExpr->getSourceRange();
5033 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5034 diag::note_collapse_loop_count)
5035 << CollapseLoopCountExpr->getSourceRange();
5036 }
5037 NestedLoopCount = Result.getLimitedValue();
5038 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005039 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005040 // This is helper routine for loop directives (e.g., 'for', 'simd',
5041 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00005042 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005043 SmallVector<LoopIterationSpace, 4> IterSpaces;
5044 IterSpaces.resize(NestedLoopCount);
5045 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005046 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005047 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005048 NestedLoopCount, CollapseLoopCountExpr,
5049 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005050 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005051 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005052 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005053 // OpenMP [2.8.1, simd construct, Restrictions]
5054 // All loops associated with the construct must be perfectly nested; that
5055 // is, there must be no intervening code nor any OpenMP directive between
5056 // any two loops.
5057 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005058 }
5059
Alexander Musmana5f070a2014-10-01 06:03:56 +00005060 Built.clear(/* size */ NestedLoopCount);
5061
5062 if (SemaRef.CurContext->isDependentContext())
5063 return NestedLoopCount;
5064
5065 // An example of what is generated for the following code:
5066 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005067 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005068 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005069 // for (k = 0; k < NK; ++k)
5070 // for (j = J0; j < NJ; j+=2) {
5071 // <loop body>
5072 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005073 //
5074 // We generate the code below.
5075 // Note: the loop body may be outlined in CodeGen.
5076 // Note: some counters may be C++ classes, operator- is used to find number of
5077 // iterations and operator+= to calculate counter value.
5078 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5079 // or i64 is currently supported).
5080 //
5081 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5082 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5083 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5084 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5085 // // similar updates for vars in clauses (e.g. 'linear')
5086 // <loop body (using local i and j)>
5087 // }
5088 // i = NI; // assign final values of counters
5089 // j = NJ;
5090 //
5091
5092 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5093 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005094 // Precondition tests if there is at least one iteration (all conditions are
5095 // true).
5096 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005097 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005098 ExprResult LastIteration32 = WidenIterationCount(
5099 32 /* Bits */, SemaRef.PerformImplicitConversion(
5100 N0->IgnoreImpCasts(), N0->getType(),
5101 Sema::AA_Converting, /*AllowExplicit=*/true)
5102 .get(),
5103 SemaRef);
5104 ExprResult LastIteration64 = WidenIterationCount(
5105 64 /* Bits */, SemaRef.PerformImplicitConversion(
5106 N0->IgnoreImpCasts(), N0->getType(),
5107 Sema::AA_Converting, /*AllowExplicit=*/true)
5108 .get(),
5109 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005110
5111 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5112 return NestedLoopCount;
5113
5114 auto &C = SemaRef.Context;
5115 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5116
5117 Scope *CurScope = DSA.getCurScope();
5118 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005119 if (PreCond.isUsable()) {
5120 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5121 PreCond.get(), IterSpaces[Cnt].PreCond);
5122 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005123 auto N = IterSpaces[Cnt].NumIterations;
5124 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5125 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005126 LastIteration32 = SemaRef.BuildBinOp(
5127 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5128 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5129 Sema::AA_Converting,
5130 /*AllowExplicit=*/true)
5131 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005132 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005133 LastIteration64 = SemaRef.BuildBinOp(
5134 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5135 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5136 Sema::AA_Converting,
5137 /*AllowExplicit=*/true)
5138 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005139 }
5140
5141 // Choose either the 32-bit or 64-bit version.
5142 ExprResult LastIteration = LastIteration64;
5143 if (LastIteration32.isUsable() &&
5144 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5145 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5146 FitsInto(
5147 32 /* Bits */,
5148 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5149 LastIteration64.get(), SemaRef)))
5150 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005151 QualType VType = LastIteration.get()->getType();
5152 QualType RealVType = VType;
5153 QualType StrideVType = VType;
5154 if (isOpenMPTaskLoopDirective(DKind)) {
5155 VType =
5156 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5157 StrideVType =
5158 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5159 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005160
5161 if (!LastIteration.isUsable())
5162 return 0;
5163
5164 // Save the number of iterations.
5165 ExprResult NumIterations = LastIteration;
5166 {
5167 LastIteration = SemaRef.BuildBinOp(
5168 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5169 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5170 if (!LastIteration.isUsable())
5171 return 0;
5172 }
5173
5174 // Calculate the last iteration number beforehand instead of doing this on
5175 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5176 llvm::APSInt Result;
5177 bool IsConstant =
5178 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5179 ExprResult CalcLastIteration;
5180 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005181 ExprResult SaveRef =
5182 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005183 LastIteration = SaveRef;
5184
5185 // Prepare SaveRef + 1.
5186 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005187 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005188 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5189 if (!NumIterations.isUsable())
5190 return 0;
5191 }
5192
5193 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5194
Alexander Musmanc6388682014-12-15 07:07:06 +00005195 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005196 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005197 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5198 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005199 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005200 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5201 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005202 SemaRef.AddInitializerToDecl(
5203 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5204 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5205
5206 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005207 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5208 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005209 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5210 /*DirectInit*/ false,
5211 /*TypeMayContainAuto*/ false);
5212
5213 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5214 // This will be used to implement clause 'lastprivate'.
5215 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005216 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5217 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005218 SemaRef.AddInitializerToDecl(
5219 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5220 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5221
5222 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005223 VarDecl *STDecl =
5224 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5225 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005226 SemaRef.AddInitializerToDecl(
5227 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5228 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5229
5230 // Build expression: UB = min(UB, LastIteration)
5231 // It is nesessary for CodeGen of directives with static scheduling.
5232 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5233 UB.get(), LastIteration.get());
5234 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5235 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5236 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5237 CondOp.get());
5238 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005239
5240 // If we have a combined directive that combines 'distribute', 'for' or
5241 // 'simd' we need to be able to access the bounds of the schedule of the
5242 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5243 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5244 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5245 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5246
5247 // We expect to have at least 2 more parameters than the 'parallel'
5248 // directive does - the lower and upper bounds of the previous schedule.
5249 assert(CD->getNumParams() >= 4 &&
5250 "Unexpected number of parameters in loop combined directive");
5251
5252 // Set the proper type for the bounds given what we learned from the
5253 // enclosed loops.
5254 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5255 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5256
5257 // Previous lower and upper bounds are obtained from the region
5258 // parameters.
5259 PrevLB =
5260 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5261 PrevUB =
5262 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5263 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005264 }
5265
5266 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005267 ExprResult IV;
5268 ExprResult Init;
5269 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005270 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5271 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005272 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005273 isOpenMPTaskLoopDirective(DKind) ||
5274 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005275 ? LB.get()
5276 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5277 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5278 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005279 }
5280
Alexander Musmanc6388682014-12-15 07:07:06 +00005281 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005282 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005283 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005284 (isOpenMPWorksharingDirective(DKind) ||
5285 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005286 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5287 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5288 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005289
5290 // Loop increment (IV = IV + 1)
5291 SourceLocation IncLoc;
5292 ExprResult Inc =
5293 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5294 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5295 if (!Inc.isUsable())
5296 return 0;
5297 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005298 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5299 if (!Inc.isUsable())
5300 return 0;
5301
5302 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5303 // Used for directives with static scheduling.
5304 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005305 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5306 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005307 // LB + ST
5308 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5309 if (!NextLB.isUsable())
5310 return 0;
5311 // LB = LB + ST
5312 NextLB =
5313 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5314 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5315 if (!NextLB.isUsable())
5316 return 0;
5317 // UB + ST
5318 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5319 if (!NextUB.isUsable())
5320 return 0;
5321 // UB = UB + ST
5322 NextUB =
5323 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5324 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5325 if (!NextUB.isUsable())
5326 return 0;
5327 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005328
5329 // Build updates and final values of the loop counters.
5330 bool HasErrors = false;
5331 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005332 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005333 Built.Updates.resize(NestedLoopCount);
5334 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005335 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005336 {
5337 ExprResult Div;
5338 // Go from inner nested loop to outer.
5339 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5340 LoopIterationSpace &IS = IterSpaces[Cnt];
5341 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5342 // Build: Iter = (IV / Div) % IS.NumIters
5343 // where Div is product of previous iterations' IS.NumIters.
5344 ExprResult Iter;
5345 if (Div.isUsable()) {
5346 Iter =
5347 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5348 } else {
5349 Iter = IV;
5350 assert((Cnt == (int)NestedLoopCount - 1) &&
5351 "unusable div expected on first iteration only");
5352 }
5353
5354 if (Cnt != 0 && Iter.isUsable())
5355 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5356 IS.NumIterations);
5357 if (!Iter.isUsable()) {
5358 HasErrors = true;
5359 break;
5360 }
5361
Alexey Bataev39f915b82015-05-08 10:41:21 +00005362 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005363 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5364 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5365 IS.CounterVar->getExprLoc(),
5366 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005367 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005368 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005369 if (!Init.isUsable()) {
5370 HasErrors = true;
5371 break;
5372 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005373 ExprResult Update = BuildCounterUpdate(
5374 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5375 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005376 if (!Update.isUsable()) {
5377 HasErrors = true;
5378 break;
5379 }
5380
5381 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5382 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005383 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005384 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005385 if (!Final.isUsable()) {
5386 HasErrors = true;
5387 break;
5388 }
5389
5390 // Build Div for the next iteration: Div <- Div * IS.NumIters
5391 if (Cnt != 0) {
5392 if (Div.isUnset())
5393 Div = IS.NumIterations;
5394 else
5395 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5396 IS.NumIterations);
5397
5398 // Add parentheses (for debugging purposes only).
5399 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005400 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005401 if (!Div.isUsable()) {
5402 HasErrors = true;
5403 break;
5404 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005405 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005406 }
5407 if (!Update.isUsable() || !Final.isUsable()) {
5408 HasErrors = true;
5409 break;
5410 }
5411 // Save results
5412 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005413 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005414 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005415 Built.Updates[Cnt] = Update.get();
5416 Built.Finals[Cnt] = Final.get();
5417 }
5418 }
5419
5420 if (HasErrors)
5421 return 0;
5422
5423 // Save results
5424 Built.IterationVarRef = IV.get();
5425 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005426 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005427 Built.CalcLastIteration =
5428 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005429 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005430 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005431 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005432 Built.Init = Init.get();
5433 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005434 Built.LB = LB.get();
5435 Built.UB = UB.get();
5436 Built.IL = IL.get();
5437 Built.ST = ST.get();
5438 Built.EUB = EUB.get();
5439 Built.NLB = NextLB.get();
5440 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005441 Built.PrevLB = PrevLB.get();
5442 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005443
Alexey Bataev8b427062016-05-25 12:36:08 +00005444 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5445 // Fill data for doacross depend clauses.
5446 for (auto Pair : DSA.getDoacrossDependClauses()) {
5447 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5448 Pair.first->setCounterValue(CounterVal);
5449 else {
5450 if (NestedLoopCount != Pair.second.size() ||
5451 NestedLoopCount != LoopMultipliers.size() + 1) {
5452 // Erroneous case - clause has some problems.
5453 Pair.first->setCounterValue(CounterVal);
5454 continue;
5455 }
5456 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5457 auto I = Pair.second.rbegin();
5458 auto IS = IterSpaces.rbegin();
5459 auto ILM = LoopMultipliers.rbegin();
5460 Expr *UpCounterVal = CounterVal;
5461 Expr *Multiplier = nullptr;
5462 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5463 if (I->first) {
5464 assert(IS->CounterStep);
5465 Expr *NormalizedOffset =
5466 SemaRef
5467 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5468 I->first, IS->CounterStep)
5469 .get();
5470 if (Multiplier) {
5471 NormalizedOffset =
5472 SemaRef
5473 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5474 NormalizedOffset, Multiplier)
5475 .get();
5476 }
5477 assert(I->second == OO_Plus || I->second == OO_Minus);
5478 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5479 UpCounterVal =
5480 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5481 UpCounterVal, NormalizedOffset).get();
5482 }
5483 Multiplier = *ILM;
5484 ++I;
5485 ++IS;
5486 ++ILM;
5487 }
5488 Pair.first->setCounterValue(UpCounterVal);
5489 }
5490 }
5491
Alexey Bataevabfc0692014-06-25 06:52:00 +00005492 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005493}
5494
Alexey Bataev10e775f2015-07-30 11:36:16 +00005495static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005496 auto CollapseClauses =
5497 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5498 if (CollapseClauses.begin() != CollapseClauses.end())
5499 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005500 return nullptr;
5501}
5502
Alexey Bataev10e775f2015-07-30 11:36:16 +00005503static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005504 auto OrderedClauses =
5505 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5506 if (OrderedClauses.begin() != OrderedClauses.end())
5507 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005508 return nullptr;
5509}
5510
Kelvin Lic5609492016-07-15 04:39:07 +00005511static bool checkSimdlenSafelenSpecified(Sema &S,
5512 const ArrayRef<OMPClause *> Clauses) {
5513 OMPSafelenClause *Safelen = nullptr;
5514 OMPSimdlenClause *Simdlen = nullptr;
5515
5516 for (auto *Clause : Clauses) {
5517 if (Clause->getClauseKind() == OMPC_safelen)
5518 Safelen = cast<OMPSafelenClause>(Clause);
5519 else if (Clause->getClauseKind() == OMPC_simdlen)
5520 Simdlen = cast<OMPSimdlenClause>(Clause);
5521 if (Safelen && Simdlen)
5522 break;
5523 }
5524
5525 if (Simdlen && Safelen) {
5526 llvm::APSInt SimdlenRes, SafelenRes;
5527 auto SimdlenLength = Simdlen->getSimdlen();
5528 auto SafelenLength = Safelen->getSafelen();
5529 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5530 SimdlenLength->isInstantiationDependent() ||
5531 SimdlenLength->containsUnexpandedParameterPack())
5532 return false;
5533 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5534 SafelenLength->isInstantiationDependent() ||
5535 SafelenLength->containsUnexpandedParameterPack())
5536 return false;
5537 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5538 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5539 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5540 // If both simdlen and safelen clauses are specified, the value of the
5541 // simdlen parameter must be less than or equal to the value of the safelen
5542 // parameter.
5543 if (SimdlenRes > SafelenRes) {
5544 S.Diag(SimdlenLength->getExprLoc(),
5545 diag::err_omp_wrong_simdlen_safelen_values)
5546 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5547 return true;
5548 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005549 }
5550 return false;
5551}
5552
Alexey Bataev4acb8592014-07-07 13:01:15 +00005553StmtResult Sema::ActOnOpenMPSimdDirective(
5554 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5555 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005556 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005557 if (!AStmt)
5558 return StmtError();
5559
5560 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005561 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005562 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5563 // define the nested loops number.
5564 unsigned NestedLoopCount = CheckOpenMPLoop(
5565 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5566 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005567 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005568 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005569
Alexander Musmana5f070a2014-10-01 06:03:56 +00005570 assert((CurContext->isDependentContext() || B.builtAll()) &&
5571 "omp simd loop exprs were not built");
5572
Alexander Musman3276a272015-03-21 10:12:56 +00005573 if (!CurContext->isDependentContext()) {
5574 // Finalize the clauses that need pre-built expressions for CodeGen.
5575 for (auto C : Clauses) {
5576 if (auto LC = dyn_cast<OMPLinearClause>(C))
5577 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005578 B.NumIterations, *this, CurScope,
5579 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005580 return StmtError();
5581 }
5582 }
5583
Kelvin Lic5609492016-07-15 04:39:07 +00005584 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005585 return StmtError();
5586
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005587 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005588 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5589 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005590}
5591
Alexey Bataev4acb8592014-07-07 13:01:15 +00005592StmtResult Sema::ActOnOpenMPForDirective(
5593 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5594 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005595 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005596 if (!AStmt)
5597 return StmtError();
5598
5599 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005600 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005601 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5602 // define the nested loops number.
5603 unsigned NestedLoopCount = CheckOpenMPLoop(
5604 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5605 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005606 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005607 return StmtError();
5608
Alexander Musmana5f070a2014-10-01 06:03:56 +00005609 assert((CurContext->isDependentContext() || B.builtAll()) &&
5610 "omp for loop exprs were not built");
5611
Alexey Bataev54acd402015-08-04 11:18:19 +00005612 if (!CurContext->isDependentContext()) {
5613 // Finalize the clauses that need pre-built expressions for CodeGen.
5614 for (auto C : Clauses) {
5615 if (auto LC = dyn_cast<OMPLinearClause>(C))
5616 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005617 B.NumIterations, *this, CurScope,
5618 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005619 return StmtError();
5620 }
5621 }
5622
Alexey Bataevf29276e2014-06-18 04:14:57 +00005623 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005624 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005625 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005626}
5627
Alexander Musmanf82886e2014-09-18 05:12:34 +00005628StmtResult Sema::ActOnOpenMPForSimdDirective(
5629 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5630 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005631 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005632 if (!AStmt)
5633 return StmtError();
5634
5635 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005636 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005637 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5638 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005639 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005640 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5641 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5642 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005643 if (NestedLoopCount == 0)
5644 return StmtError();
5645
Alexander Musmanc6388682014-12-15 07:07:06 +00005646 assert((CurContext->isDependentContext() || B.builtAll()) &&
5647 "omp for simd loop exprs were not built");
5648
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005649 if (!CurContext->isDependentContext()) {
5650 // Finalize the clauses that need pre-built expressions for CodeGen.
5651 for (auto C : Clauses) {
5652 if (auto LC = dyn_cast<OMPLinearClause>(C))
5653 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005654 B.NumIterations, *this, CurScope,
5655 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005656 return StmtError();
5657 }
5658 }
5659
Kelvin Lic5609492016-07-15 04:39:07 +00005660 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005661 return StmtError();
5662
Alexander Musmanf82886e2014-09-18 05:12:34 +00005663 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005664 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5665 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005666}
5667
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005668StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5669 Stmt *AStmt,
5670 SourceLocation StartLoc,
5671 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005672 if (!AStmt)
5673 return StmtError();
5674
5675 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005676 auto BaseStmt = AStmt;
5677 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5678 BaseStmt = CS->getCapturedStmt();
5679 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5680 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005681 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005682 return StmtError();
5683 // All associated statements must be '#pragma omp section' except for
5684 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005685 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005686 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5687 if (SectionStmt)
5688 Diag(SectionStmt->getLocStart(),
5689 diag::err_omp_sections_substmt_not_section);
5690 return StmtError();
5691 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005692 cast<OMPSectionDirective>(SectionStmt)
5693 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005694 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005695 } else {
5696 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5697 return StmtError();
5698 }
5699
5700 getCurFunction()->setHasBranchProtectedScope();
5701
Alexey Bataev25e5b442015-09-15 12:52:43 +00005702 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5703 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005704}
5705
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005706StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5707 SourceLocation StartLoc,
5708 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005709 if (!AStmt)
5710 return StmtError();
5711
5712 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005713
5714 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005715 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005716
Alexey Bataev25e5b442015-09-15 12:52:43 +00005717 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5718 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005719}
5720
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005721StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5722 Stmt *AStmt,
5723 SourceLocation StartLoc,
5724 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005725 if (!AStmt)
5726 return StmtError();
5727
5728 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005729
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005730 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005731
Alexey Bataev3255bf32015-01-19 05:20:46 +00005732 // OpenMP [2.7.3, single Construct, Restrictions]
5733 // The copyprivate clause must not be used with the nowait clause.
5734 OMPClause *Nowait = nullptr;
5735 OMPClause *Copyprivate = nullptr;
5736 for (auto *Clause : Clauses) {
5737 if (Clause->getClauseKind() == OMPC_nowait)
5738 Nowait = Clause;
5739 else if (Clause->getClauseKind() == OMPC_copyprivate)
5740 Copyprivate = Clause;
5741 if (Copyprivate && Nowait) {
5742 Diag(Copyprivate->getLocStart(),
5743 diag::err_omp_single_copyprivate_with_nowait);
5744 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5745 return StmtError();
5746 }
5747 }
5748
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005749 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5750}
5751
Alexander Musman80c22892014-07-17 08:54:58 +00005752StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5753 SourceLocation StartLoc,
5754 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005755 if (!AStmt)
5756 return StmtError();
5757
5758 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005759
5760 getCurFunction()->setHasBranchProtectedScope();
5761
5762 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5763}
5764
Alexey Bataev28c75412015-12-15 08:19:24 +00005765StmtResult Sema::ActOnOpenMPCriticalDirective(
5766 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5767 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005768 if (!AStmt)
5769 return StmtError();
5770
5771 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005772
Alexey Bataev28c75412015-12-15 08:19:24 +00005773 bool ErrorFound = false;
5774 llvm::APSInt Hint;
5775 SourceLocation HintLoc;
5776 bool DependentHint = false;
5777 for (auto *C : Clauses) {
5778 if (C->getClauseKind() == OMPC_hint) {
5779 if (!DirName.getName()) {
5780 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5781 ErrorFound = true;
5782 }
5783 Expr *E = cast<OMPHintClause>(C)->getHint();
5784 if (E->isTypeDependent() || E->isValueDependent() ||
5785 E->isInstantiationDependent())
5786 DependentHint = true;
5787 else {
5788 Hint = E->EvaluateKnownConstInt(Context);
5789 HintLoc = C->getLocStart();
5790 }
5791 }
5792 }
5793 if (ErrorFound)
5794 return StmtError();
5795 auto Pair = DSAStack->getCriticalWithHint(DirName);
5796 if (Pair.first && DirName.getName() && !DependentHint) {
5797 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5798 Diag(StartLoc, diag::err_omp_critical_with_hint);
5799 if (HintLoc.isValid()) {
5800 Diag(HintLoc, diag::note_omp_critical_hint_here)
5801 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5802 } else
5803 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5804 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5805 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5806 << 1
5807 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5808 /*Radix=*/10, /*Signed=*/false);
5809 } else
5810 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5811 }
5812 }
5813
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005814 getCurFunction()->setHasBranchProtectedScope();
5815
Alexey Bataev28c75412015-12-15 08:19:24 +00005816 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5817 Clauses, AStmt);
5818 if (!Pair.first && DirName.getName() && !DependentHint)
5819 DSAStack->addCriticalWithHint(Dir, Hint);
5820 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005821}
5822
Alexey Bataev4acb8592014-07-07 13:01:15 +00005823StmtResult Sema::ActOnOpenMPParallelForDirective(
5824 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5825 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005826 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005827 if (!AStmt)
5828 return StmtError();
5829
Alexey Bataev4acb8592014-07-07 13:01:15 +00005830 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5831 // 1.2.2 OpenMP Language Terminology
5832 // Structured block - An executable statement with a single entry at the
5833 // top and a single exit at the bottom.
5834 // The point of exit cannot be a branch out of the structured block.
5835 // longjmp() and throw() must not violate the entry/exit criteria.
5836 CS->getCapturedDecl()->setNothrow();
5837
Alexander Musmanc6388682014-12-15 07:07:06 +00005838 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005839 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5840 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005841 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005842 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5843 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5844 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005845 if (NestedLoopCount == 0)
5846 return StmtError();
5847
Alexander Musmana5f070a2014-10-01 06:03:56 +00005848 assert((CurContext->isDependentContext() || B.builtAll()) &&
5849 "omp parallel for loop exprs were not built");
5850
Alexey Bataev54acd402015-08-04 11:18:19 +00005851 if (!CurContext->isDependentContext()) {
5852 // Finalize the clauses that need pre-built expressions for CodeGen.
5853 for (auto C : Clauses) {
5854 if (auto LC = dyn_cast<OMPLinearClause>(C))
5855 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005856 B.NumIterations, *this, CurScope,
5857 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005858 return StmtError();
5859 }
5860 }
5861
Alexey Bataev4acb8592014-07-07 13:01:15 +00005862 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005863 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005864 NestedLoopCount, Clauses, AStmt, B,
5865 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005866}
5867
Alexander Musmane4e893b2014-09-23 09:33:00 +00005868StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5869 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5870 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005871 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005872 if (!AStmt)
5873 return StmtError();
5874
Alexander Musmane4e893b2014-09-23 09:33:00 +00005875 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5876 // 1.2.2 OpenMP Language Terminology
5877 // Structured block - An executable statement with a single entry at the
5878 // top and a single exit at the bottom.
5879 // The point of exit cannot be a branch out of the structured block.
5880 // longjmp() and throw() must not violate the entry/exit criteria.
5881 CS->getCapturedDecl()->setNothrow();
5882
Alexander Musmanc6388682014-12-15 07:07:06 +00005883 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005884 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5885 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005886 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005887 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5888 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5889 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005890 if (NestedLoopCount == 0)
5891 return StmtError();
5892
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005893 if (!CurContext->isDependentContext()) {
5894 // Finalize the clauses that need pre-built expressions for CodeGen.
5895 for (auto C : Clauses) {
5896 if (auto LC = dyn_cast<OMPLinearClause>(C))
5897 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005898 B.NumIterations, *this, CurScope,
5899 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005900 return StmtError();
5901 }
5902 }
5903
Kelvin Lic5609492016-07-15 04:39:07 +00005904 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005905 return StmtError();
5906
Alexander Musmane4e893b2014-09-23 09:33:00 +00005907 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005908 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005909 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005910}
5911
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005912StmtResult
5913Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5914 Stmt *AStmt, SourceLocation StartLoc,
5915 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005916 if (!AStmt)
5917 return StmtError();
5918
5919 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005920 auto BaseStmt = AStmt;
5921 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5922 BaseStmt = CS->getCapturedStmt();
5923 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5924 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005925 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005926 return StmtError();
5927 // All associated statements must be '#pragma omp section' except for
5928 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005929 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005930 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5931 if (SectionStmt)
5932 Diag(SectionStmt->getLocStart(),
5933 diag::err_omp_parallel_sections_substmt_not_section);
5934 return StmtError();
5935 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005936 cast<OMPSectionDirective>(SectionStmt)
5937 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005938 }
5939 } else {
5940 Diag(AStmt->getLocStart(),
5941 diag::err_omp_parallel_sections_not_compound_stmt);
5942 return StmtError();
5943 }
5944
5945 getCurFunction()->setHasBranchProtectedScope();
5946
Alexey Bataev25e5b442015-09-15 12:52:43 +00005947 return OMPParallelSectionsDirective::Create(
5948 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005949}
5950
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005951StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5952 Stmt *AStmt, SourceLocation StartLoc,
5953 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005954 if (!AStmt)
5955 return StmtError();
5956
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005957 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5958 // 1.2.2 OpenMP Language Terminology
5959 // Structured block - An executable statement with a single entry at the
5960 // top and a single exit at the bottom.
5961 // The point of exit cannot be a branch out of the structured block.
5962 // longjmp() and throw() must not violate the entry/exit criteria.
5963 CS->getCapturedDecl()->setNothrow();
5964
5965 getCurFunction()->setHasBranchProtectedScope();
5966
Alexey Bataev25e5b442015-09-15 12:52:43 +00005967 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5968 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005969}
5970
Alexey Bataev68446b72014-07-18 07:47:19 +00005971StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5972 SourceLocation EndLoc) {
5973 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5974}
5975
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005976StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5977 SourceLocation EndLoc) {
5978 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5979}
5980
Alexey Bataev2df347a2014-07-18 10:17:07 +00005981StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5982 SourceLocation EndLoc) {
5983 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5984}
5985
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005986StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5987 SourceLocation StartLoc,
5988 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005989 if (!AStmt)
5990 return StmtError();
5991
5992 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005993
5994 getCurFunction()->setHasBranchProtectedScope();
5995
5996 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5997}
5998
Alexey Bataev6125da92014-07-21 11:26:11 +00005999StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6000 SourceLocation StartLoc,
6001 SourceLocation EndLoc) {
6002 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6003 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6004}
6005
Alexey Bataev346265e2015-09-25 10:37:12 +00006006StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6007 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006008 SourceLocation StartLoc,
6009 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006010 OMPClause *DependFound = nullptr;
6011 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006012 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006013 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00006014 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006015 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006016 for (auto *C : Clauses) {
6017 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6018 DependFound = C;
6019 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6020 if (DependSourceClause) {
6021 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
6022 << getOpenMPDirectiveName(OMPD_ordered)
6023 << getOpenMPClauseName(OMPC_depend) << 2;
6024 ErrorFound = true;
6025 } else
6026 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006027 if (DependSinkClause) {
6028 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6029 << 0;
6030 ErrorFound = true;
6031 }
6032 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6033 if (DependSourceClause) {
6034 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6035 << 1;
6036 ErrorFound = true;
6037 }
6038 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006039 }
6040 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00006041 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006042 else if (C->getClauseKind() == OMPC_simd)
6043 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00006044 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006045 if (!ErrorFound && !SC &&
6046 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006047 // OpenMP [2.8.1,simd Construct, Restrictions]
6048 // An ordered construct with the simd clause is the only OpenMP construct
6049 // that can appear in the simd region.
6050 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006051 ErrorFound = true;
6052 } else if (DependFound && (TC || SC)) {
6053 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6054 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6055 ErrorFound = true;
6056 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6057 Diag(DependFound->getLocStart(),
6058 diag::err_omp_ordered_directive_without_param);
6059 ErrorFound = true;
6060 } else if (TC || Clauses.empty()) {
6061 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6062 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6063 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6064 << (TC != nullptr);
6065 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6066 ErrorFound = true;
6067 }
6068 }
6069 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006070 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006071
6072 if (AStmt) {
6073 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6074
6075 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006076 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006077
6078 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006079}
6080
Alexey Bataev1d160b12015-03-13 12:27:31 +00006081namespace {
6082/// \brief Helper class for checking expression in 'omp atomic [update]'
6083/// construct.
6084class OpenMPAtomicUpdateChecker {
6085 /// \brief Error results for atomic update expressions.
6086 enum ExprAnalysisErrorCode {
6087 /// \brief A statement is not an expression statement.
6088 NotAnExpression,
6089 /// \brief Expression is not builtin binary or unary operation.
6090 NotABinaryOrUnaryExpression,
6091 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6092 NotAnUnaryIncDecExpression,
6093 /// \brief An expression is not of scalar type.
6094 NotAScalarType,
6095 /// \brief A binary operation is not an assignment operation.
6096 NotAnAssignmentOp,
6097 /// \brief RHS part of the binary operation is not a binary expression.
6098 NotABinaryExpression,
6099 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6100 /// expression.
6101 NotABinaryOperator,
6102 /// \brief RHS binary operation does not have reference to the updated LHS
6103 /// part.
6104 NotAnUpdateExpression,
6105 /// \brief No errors is found.
6106 NoError
6107 };
6108 /// \brief Reference to Sema.
6109 Sema &SemaRef;
6110 /// \brief A location for note diagnostics (when error is found).
6111 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006112 /// \brief 'x' lvalue part of the source atomic expression.
6113 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006114 /// \brief 'expr' rvalue part of the source atomic expression.
6115 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006116 /// \brief Helper expression of the form
6117 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6118 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6119 Expr *UpdateExpr;
6120 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6121 /// important for non-associative operations.
6122 bool IsXLHSInRHSPart;
6123 BinaryOperatorKind Op;
6124 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006125 /// \brief true if the source expression is a postfix unary operation, false
6126 /// if it is a prefix unary operation.
6127 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006128
6129public:
6130 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006131 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006132 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006133 /// \brief Check specified statement that it is suitable for 'atomic update'
6134 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006135 /// expression. If DiagId and NoteId == 0, then only check is performed
6136 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006137 /// \param DiagId Diagnostic which should be emitted if error is found.
6138 /// \param NoteId Diagnostic note for the main error message.
6139 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006140 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006141 /// \brief Return the 'x' lvalue part of the source atomic expression.
6142 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006143 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6144 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006145 /// \brief Return the update expression used in calculation of the updated
6146 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6147 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6148 Expr *getUpdateExpr() const { return UpdateExpr; }
6149 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6150 /// false otherwise.
6151 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6152
Alexey Bataevb78ca832015-04-01 03:33:17 +00006153 /// \brief true if the source expression is a postfix unary operation, false
6154 /// if it is a prefix unary operation.
6155 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6156
Alexey Bataev1d160b12015-03-13 12:27:31 +00006157private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006158 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6159 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006160};
6161} // namespace
6162
6163bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6164 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6165 ExprAnalysisErrorCode ErrorFound = NoError;
6166 SourceLocation ErrorLoc, NoteLoc;
6167 SourceRange ErrorRange, NoteRange;
6168 // Allowed constructs are:
6169 // x = x binop expr;
6170 // x = expr binop x;
6171 if (AtomicBinOp->getOpcode() == BO_Assign) {
6172 X = AtomicBinOp->getLHS();
6173 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6174 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6175 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6176 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6177 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006178 Op = AtomicInnerBinOp->getOpcode();
6179 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006180 auto *LHS = AtomicInnerBinOp->getLHS();
6181 auto *RHS = AtomicInnerBinOp->getRHS();
6182 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6183 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6184 /*Canonical=*/true);
6185 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6186 /*Canonical=*/true);
6187 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6188 /*Canonical=*/true);
6189 if (XId == LHSId) {
6190 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006191 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006192 } else if (XId == RHSId) {
6193 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006194 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006195 } else {
6196 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6197 ErrorRange = AtomicInnerBinOp->getSourceRange();
6198 NoteLoc = X->getExprLoc();
6199 NoteRange = X->getSourceRange();
6200 ErrorFound = NotAnUpdateExpression;
6201 }
6202 } else {
6203 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6204 ErrorRange = AtomicInnerBinOp->getSourceRange();
6205 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6206 NoteRange = SourceRange(NoteLoc, NoteLoc);
6207 ErrorFound = NotABinaryOperator;
6208 }
6209 } else {
6210 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6211 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6212 ErrorFound = NotABinaryExpression;
6213 }
6214 } else {
6215 ErrorLoc = AtomicBinOp->getExprLoc();
6216 ErrorRange = AtomicBinOp->getSourceRange();
6217 NoteLoc = AtomicBinOp->getOperatorLoc();
6218 NoteRange = SourceRange(NoteLoc, NoteLoc);
6219 ErrorFound = NotAnAssignmentOp;
6220 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006221 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006222 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6223 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6224 return true;
6225 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006226 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006227 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006228}
6229
6230bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6231 unsigned NoteId) {
6232 ExprAnalysisErrorCode ErrorFound = NoError;
6233 SourceLocation ErrorLoc, NoteLoc;
6234 SourceRange ErrorRange, NoteRange;
6235 // Allowed constructs are:
6236 // x++;
6237 // x--;
6238 // ++x;
6239 // --x;
6240 // x binop= expr;
6241 // x = x binop expr;
6242 // x = expr binop x;
6243 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6244 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6245 if (AtomicBody->getType()->isScalarType() ||
6246 AtomicBody->isInstantiationDependent()) {
6247 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6248 AtomicBody->IgnoreParenImpCasts())) {
6249 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006250 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006251 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006252 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006253 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006254 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006255 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006256 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6257 AtomicBody->IgnoreParenImpCasts())) {
6258 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006259 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6260 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006261 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006262 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6263 // Check for Unary Operation
6264 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006265 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006266 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6267 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006268 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006269 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6270 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006271 } else {
6272 ErrorFound = NotAnUnaryIncDecExpression;
6273 ErrorLoc = AtomicUnaryOp->getExprLoc();
6274 ErrorRange = AtomicUnaryOp->getSourceRange();
6275 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6276 NoteRange = SourceRange(NoteLoc, NoteLoc);
6277 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006278 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006279 ErrorFound = NotABinaryOrUnaryExpression;
6280 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6281 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6282 }
6283 } else {
6284 ErrorFound = NotAScalarType;
6285 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6286 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6287 }
6288 } else {
6289 ErrorFound = NotAnExpression;
6290 NoteLoc = ErrorLoc = S->getLocStart();
6291 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6292 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006293 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006294 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6295 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6296 return true;
6297 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006298 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006299 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006300 // Build an update expression of form 'OpaqueValueExpr(x) binop
6301 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6302 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6303 auto *OVEX = new (SemaRef.getASTContext())
6304 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6305 auto *OVEExpr = new (SemaRef.getASTContext())
6306 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6307 auto Update =
6308 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6309 IsXLHSInRHSPart ? OVEExpr : OVEX);
6310 if (Update.isInvalid())
6311 return true;
6312 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6313 Sema::AA_Casting);
6314 if (Update.isInvalid())
6315 return true;
6316 UpdateExpr = Update.get();
6317 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006318 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006319}
6320
Alexey Bataev0162e452014-07-22 10:10:35 +00006321StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6322 Stmt *AStmt,
6323 SourceLocation StartLoc,
6324 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006325 if (!AStmt)
6326 return StmtError();
6327
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006328 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006329 // 1.2.2 OpenMP Language Terminology
6330 // Structured block - An executable statement with a single entry at the
6331 // top and a single exit at the bottom.
6332 // The point of exit cannot be a branch out of the structured block.
6333 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006334 OpenMPClauseKind AtomicKind = OMPC_unknown;
6335 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006336 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006337 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006338 C->getClauseKind() == OMPC_update ||
6339 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006340 if (AtomicKind != OMPC_unknown) {
6341 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6342 << SourceRange(C->getLocStart(), C->getLocEnd());
6343 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6344 << getOpenMPClauseName(AtomicKind);
6345 } else {
6346 AtomicKind = C->getClauseKind();
6347 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006348 }
6349 }
6350 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006351
Alexey Bataev459dec02014-07-24 06:46:57 +00006352 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006353 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6354 Body = EWC->getSubExpr();
6355
Alexey Bataev62cec442014-11-18 10:14:22 +00006356 Expr *X = nullptr;
6357 Expr *V = nullptr;
6358 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006359 Expr *UE = nullptr;
6360 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006361 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006362 // OpenMP [2.12.6, atomic Construct]
6363 // In the next expressions:
6364 // * x and v (as applicable) are both l-value expressions with scalar type.
6365 // * During the execution of an atomic region, multiple syntactic
6366 // occurrences of x must designate the same storage location.
6367 // * Neither of v and expr (as applicable) may access the storage location
6368 // designated by x.
6369 // * Neither of x and expr (as applicable) may access the storage location
6370 // designated by v.
6371 // * expr is an expression with scalar type.
6372 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6373 // * binop, binop=, ++, and -- are not overloaded operators.
6374 // * The expression x binop expr must be numerically equivalent to x binop
6375 // (expr). This requirement is satisfied if the operators in expr have
6376 // precedence greater than binop, or by using parentheses around expr or
6377 // subexpressions of expr.
6378 // * The expression expr binop x must be numerically equivalent to (expr)
6379 // binop x. This requirement is satisfied if the operators in expr have
6380 // precedence equal to or greater than binop, or by using parentheses around
6381 // expr or subexpressions of expr.
6382 // * For forms that allow multiple occurrences of x, the number of times
6383 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006384 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006385 enum {
6386 NotAnExpression,
6387 NotAnAssignmentOp,
6388 NotAScalarType,
6389 NotAnLValue,
6390 NoError
6391 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006392 SourceLocation ErrorLoc, NoteLoc;
6393 SourceRange ErrorRange, NoteRange;
6394 // If clause is read:
6395 // v = x;
6396 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6397 auto AtomicBinOp =
6398 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6399 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6400 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6401 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6402 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6403 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6404 if (!X->isLValue() || !V->isLValue()) {
6405 auto NotLValueExpr = X->isLValue() ? V : X;
6406 ErrorFound = NotAnLValue;
6407 ErrorLoc = AtomicBinOp->getExprLoc();
6408 ErrorRange = AtomicBinOp->getSourceRange();
6409 NoteLoc = NotLValueExpr->getExprLoc();
6410 NoteRange = NotLValueExpr->getSourceRange();
6411 }
6412 } else if (!X->isInstantiationDependent() ||
6413 !V->isInstantiationDependent()) {
6414 auto NotScalarExpr =
6415 (X->isInstantiationDependent() || X->getType()->isScalarType())
6416 ? V
6417 : X;
6418 ErrorFound = NotAScalarType;
6419 ErrorLoc = AtomicBinOp->getExprLoc();
6420 ErrorRange = AtomicBinOp->getSourceRange();
6421 NoteLoc = NotScalarExpr->getExprLoc();
6422 NoteRange = NotScalarExpr->getSourceRange();
6423 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006424 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006425 ErrorFound = NotAnAssignmentOp;
6426 ErrorLoc = AtomicBody->getExprLoc();
6427 ErrorRange = AtomicBody->getSourceRange();
6428 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6429 : AtomicBody->getExprLoc();
6430 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6431 : AtomicBody->getSourceRange();
6432 }
6433 } else {
6434 ErrorFound = NotAnExpression;
6435 NoteLoc = ErrorLoc = Body->getLocStart();
6436 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006437 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006438 if (ErrorFound != NoError) {
6439 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6440 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006441 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6442 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006443 return StmtError();
6444 } else if (CurContext->isDependentContext())
6445 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006446 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006447 enum {
6448 NotAnExpression,
6449 NotAnAssignmentOp,
6450 NotAScalarType,
6451 NotAnLValue,
6452 NoError
6453 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006454 SourceLocation ErrorLoc, NoteLoc;
6455 SourceRange ErrorRange, NoteRange;
6456 // If clause is write:
6457 // x = expr;
6458 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6459 auto AtomicBinOp =
6460 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6461 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006462 X = AtomicBinOp->getLHS();
6463 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006464 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6465 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6466 if (!X->isLValue()) {
6467 ErrorFound = NotAnLValue;
6468 ErrorLoc = AtomicBinOp->getExprLoc();
6469 ErrorRange = AtomicBinOp->getSourceRange();
6470 NoteLoc = X->getExprLoc();
6471 NoteRange = X->getSourceRange();
6472 }
6473 } else if (!X->isInstantiationDependent() ||
6474 !E->isInstantiationDependent()) {
6475 auto NotScalarExpr =
6476 (X->isInstantiationDependent() || X->getType()->isScalarType())
6477 ? E
6478 : X;
6479 ErrorFound = NotAScalarType;
6480 ErrorLoc = AtomicBinOp->getExprLoc();
6481 ErrorRange = AtomicBinOp->getSourceRange();
6482 NoteLoc = NotScalarExpr->getExprLoc();
6483 NoteRange = NotScalarExpr->getSourceRange();
6484 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006485 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006486 ErrorFound = NotAnAssignmentOp;
6487 ErrorLoc = AtomicBody->getExprLoc();
6488 ErrorRange = AtomicBody->getSourceRange();
6489 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6490 : AtomicBody->getExprLoc();
6491 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6492 : AtomicBody->getSourceRange();
6493 }
6494 } else {
6495 ErrorFound = NotAnExpression;
6496 NoteLoc = ErrorLoc = Body->getLocStart();
6497 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006498 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006499 if (ErrorFound != NoError) {
6500 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6501 << ErrorRange;
6502 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6503 << NoteRange;
6504 return StmtError();
6505 } else if (CurContext->isDependentContext())
6506 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006507 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006508 // If clause is update:
6509 // x++;
6510 // x--;
6511 // ++x;
6512 // --x;
6513 // x binop= expr;
6514 // x = x binop expr;
6515 // x = expr binop x;
6516 OpenMPAtomicUpdateChecker Checker(*this);
6517 if (Checker.checkStatement(
6518 Body, (AtomicKind == OMPC_update)
6519 ? diag::err_omp_atomic_update_not_expression_statement
6520 : diag::err_omp_atomic_not_expression_statement,
6521 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006522 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006523 if (!CurContext->isDependentContext()) {
6524 E = Checker.getExpr();
6525 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006526 UE = Checker.getUpdateExpr();
6527 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006528 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006529 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006530 enum {
6531 NotAnAssignmentOp,
6532 NotACompoundStatement,
6533 NotTwoSubstatements,
6534 NotASpecificExpression,
6535 NoError
6536 } ErrorFound = NoError;
6537 SourceLocation ErrorLoc, NoteLoc;
6538 SourceRange ErrorRange, NoteRange;
6539 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6540 // If clause is a capture:
6541 // v = x++;
6542 // v = x--;
6543 // v = ++x;
6544 // v = --x;
6545 // v = x binop= expr;
6546 // v = x = x binop expr;
6547 // v = x = expr binop x;
6548 auto *AtomicBinOp =
6549 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6550 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6551 V = AtomicBinOp->getLHS();
6552 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6553 OpenMPAtomicUpdateChecker Checker(*this);
6554 if (Checker.checkStatement(
6555 Body, diag::err_omp_atomic_capture_not_expression_statement,
6556 diag::note_omp_atomic_update))
6557 return StmtError();
6558 E = Checker.getExpr();
6559 X = Checker.getX();
6560 UE = Checker.getUpdateExpr();
6561 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6562 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006563 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006564 ErrorLoc = AtomicBody->getExprLoc();
6565 ErrorRange = AtomicBody->getSourceRange();
6566 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6567 : AtomicBody->getExprLoc();
6568 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6569 : AtomicBody->getSourceRange();
6570 ErrorFound = NotAnAssignmentOp;
6571 }
6572 if (ErrorFound != NoError) {
6573 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6574 << ErrorRange;
6575 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6576 return StmtError();
6577 } else if (CurContext->isDependentContext()) {
6578 UE = V = E = X = nullptr;
6579 }
6580 } else {
6581 // If clause is a capture:
6582 // { v = x; x = expr; }
6583 // { v = x; x++; }
6584 // { v = x; x--; }
6585 // { v = x; ++x; }
6586 // { v = x; --x; }
6587 // { v = x; x binop= expr; }
6588 // { v = x; x = x binop expr; }
6589 // { v = x; x = expr binop x; }
6590 // { x++; v = x; }
6591 // { x--; v = x; }
6592 // { ++x; v = x; }
6593 // { --x; v = x; }
6594 // { x binop= expr; v = x; }
6595 // { x = x binop expr; v = x; }
6596 // { x = expr binop x; v = x; }
6597 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6598 // Check that this is { expr1; expr2; }
6599 if (CS->size() == 2) {
6600 auto *First = CS->body_front();
6601 auto *Second = CS->body_back();
6602 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6603 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6604 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6605 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6606 // Need to find what subexpression is 'v' and what is 'x'.
6607 OpenMPAtomicUpdateChecker Checker(*this);
6608 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6609 BinaryOperator *BinOp = nullptr;
6610 if (IsUpdateExprFound) {
6611 BinOp = dyn_cast<BinaryOperator>(First);
6612 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6613 }
6614 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6615 // { v = x; x++; }
6616 // { v = x; x--; }
6617 // { v = x; ++x; }
6618 // { v = x; --x; }
6619 // { v = x; x binop= expr; }
6620 // { v = x; x = x binop expr; }
6621 // { v = x; x = expr binop x; }
6622 // Check that the first expression has form v = x.
6623 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6624 llvm::FoldingSetNodeID XId, PossibleXId;
6625 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6626 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6627 IsUpdateExprFound = XId == PossibleXId;
6628 if (IsUpdateExprFound) {
6629 V = BinOp->getLHS();
6630 X = Checker.getX();
6631 E = Checker.getExpr();
6632 UE = Checker.getUpdateExpr();
6633 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006634 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006635 }
6636 }
6637 if (!IsUpdateExprFound) {
6638 IsUpdateExprFound = !Checker.checkStatement(First);
6639 BinOp = nullptr;
6640 if (IsUpdateExprFound) {
6641 BinOp = dyn_cast<BinaryOperator>(Second);
6642 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6643 }
6644 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6645 // { x++; v = x; }
6646 // { x--; v = x; }
6647 // { ++x; v = x; }
6648 // { --x; v = x; }
6649 // { x binop= expr; v = x; }
6650 // { x = x binop expr; v = x; }
6651 // { x = expr binop x; v = x; }
6652 // Check that the second expression has form v = x.
6653 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6654 llvm::FoldingSetNodeID XId, PossibleXId;
6655 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6656 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6657 IsUpdateExprFound = XId == PossibleXId;
6658 if (IsUpdateExprFound) {
6659 V = BinOp->getLHS();
6660 X = Checker.getX();
6661 E = Checker.getExpr();
6662 UE = Checker.getUpdateExpr();
6663 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006664 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006665 }
6666 }
6667 }
6668 if (!IsUpdateExprFound) {
6669 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006670 auto *FirstExpr = dyn_cast<Expr>(First);
6671 auto *SecondExpr = dyn_cast<Expr>(Second);
6672 if (!FirstExpr || !SecondExpr ||
6673 !(FirstExpr->isInstantiationDependent() ||
6674 SecondExpr->isInstantiationDependent())) {
6675 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6676 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006677 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006678 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6679 : First->getLocStart();
6680 NoteRange = ErrorRange = FirstBinOp
6681 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006682 : SourceRange(ErrorLoc, ErrorLoc);
6683 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006684 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6685 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6686 ErrorFound = NotAnAssignmentOp;
6687 NoteLoc = ErrorLoc = SecondBinOp
6688 ? SecondBinOp->getOperatorLoc()
6689 : Second->getLocStart();
6690 NoteRange = ErrorRange =
6691 SecondBinOp ? SecondBinOp->getSourceRange()
6692 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006693 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006694 auto *PossibleXRHSInFirst =
6695 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6696 auto *PossibleXLHSInSecond =
6697 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6698 llvm::FoldingSetNodeID X1Id, X2Id;
6699 PossibleXRHSInFirst->Profile(X1Id, Context,
6700 /*Canonical=*/true);
6701 PossibleXLHSInSecond->Profile(X2Id, Context,
6702 /*Canonical=*/true);
6703 IsUpdateExprFound = X1Id == X2Id;
6704 if (IsUpdateExprFound) {
6705 V = FirstBinOp->getLHS();
6706 X = SecondBinOp->getLHS();
6707 E = SecondBinOp->getRHS();
6708 UE = nullptr;
6709 IsXLHSInRHSPart = false;
6710 IsPostfixUpdate = true;
6711 } else {
6712 ErrorFound = NotASpecificExpression;
6713 ErrorLoc = FirstBinOp->getExprLoc();
6714 ErrorRange = FirstBinOp->getSourceRange();
6715 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6716 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6717 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006718 }
6719 }
6720 }
6721 }
6722 } else {
6723 NoteLoc = ErrorLoc = Body->getLocStart();
6724 NoteRange = ErrorRange =
6725 SourceRange(Body->getLocStart(), Body->getLocStart());
6726 ErrorFound = NotTwoSubstatements;
6727 }
6728 } else {
6729 NoteLoc = ErrorLoc = Body->getLocStart();
6730 NoteRange = ErrorRange =
6731 SourceRange(Body->getLocStart(), Body->getLocStart());
6732 ErrorFound = NotACompoundStatement;
6733 }
6734 if (ErrorFound != NoError) {
6735 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6736 << ErrorRange;
6737 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6738 return StmtError();
6739 } else if (CurContext->isDependentContext()) {
6740 UE = V = E = X = nullptr;
6741 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006742 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006743 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006744
6745 getCurFunction()->setHasBranchProtectedScope();
6746
Alexey Bataev62cec442014-11-18 10:14:22 +00006747 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006748 X, V, E, UE, IsXLHSInRHSPart,
6749 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006750}
6751
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006752StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6753 Stmt *AStmt,
6754 SourceLocation StartLoc,
6755 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006756 if (!AStmt)
6757 return StmtError();
6758
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006759 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6760 // 1.2.2 OpenMP Language Terminology
6761 // Structured block - An executable statement with a single entry at the
6762 // top and a single exit at the bottom.
6763 // The point of exit cannot be a branch out of the structured block.
6764 // longjmp() and throw() must not violate the entry/exit criteria.
6765 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006766
Alexey Bataev13314bf2014-10-09 04:18:56 +00006767 // OpenMP [2.16, Nesting of Regions]
6768 // If specified, a teams construct must be contained within a target
6769 // construct. That target construct must contain no statements or directives
6770 // outside of the teams construct.
6771 if (DSAStack->hasInnerTeamsRegion()) {
6772 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6773 bool OMPTeamsFound = true;
6774 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6775 auto I = CS->body_begin();
6776 while (I != CS->body_end()) {
6777 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6778 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6779 OMPTeamsFound = false;
6780 break;
6781 }
6782 ++I;
6783 }
6784 assert(I != CS->body_end() && "Not found statement");
6785 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006786 } else {
6787 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6788 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006789 }
6790 if (!OMPTeamsFound) {
6791 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6792 Diag(DSAStack->getInnerTeamsRegionLoc(),
6793 diag::note_omp_nested_teams_construct_here);
6794 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6795 << isa<OMPExecutableDirective>(S);
6796 return StmtError();
6797 }
6798 }
6799
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006800 getCurFunction()->setHasBranchProtectedScope();
6801
6802 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6803}
6804
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006805StmtResult
6806Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6807 Stmt *AStmt, SourceLocation StartLoc,
6808 SourceLocation EndLoc) {
6809 if (!AStmt)
6810 return StmtError();
6811
6812 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6813 // 1.2.2 OpenMP Language Terminology
6814 // Structured block - An executable statement with a single entry at the
6815 // top and a single exit at the bottom.
6816 // The point of exit cannot be a branch out of the structured block.
6817 // longjmp() and throw() must not violate the entry/exit criteria.
6818 CS->getCapturedDecl()->setNothrow();
6819
6820 getCurFunction()->setHasBranchProtectedScope();
6821
6822 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6823 AStmt);
6824}
6825
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006826StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6827 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6828 SourceLocation EndLoc,
6829 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6830 if (!AStmt)
6831 return StmtError();
6832
6833 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6834 // 1.2.2 OpenMP Language Terminology
6835 // Structured block - An executable statement with a single entry at the
6836 // top and a single exit at the bottom.
6837 // The point of exit cannot be a branch out of the structured block.
6838 // longjmp() and throw() must not violate the entry/exit criteria.
6839 CS->getCapturedDecl()->setNothrow();
6840
6841 OMPLoopDirective::HelperExprs B;
6842 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6843 // define the nested loops number.
6844 unsigned NestedLoopCount =
6845 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6846 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6847 VarsWithImplicitDSA, B);
6848 if (NestedLoopCount == 0)
6849 return StmtError();
6850
6851 assert((CurContext->isDependentContext() || B.builtAll()) &&
6852 "omp target parallel for loop exprs were not built");
6853
6854 if (!CurContext->isDependentContext()) {
6855 // Finalize the clauses that need pre-built expressions for CodeGen.
6856 for (auto C : Clauses) {
6857 if (auto LC = dyn_cast<OMPLinearClause>(C))
6858 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006859 B.NumIterations, *this, CurScope,
6860 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006861 return StmtError();
6862 }
6863 }
6864
6865 getCurFunction()->setHasBranchProtectedScope();
6866 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6867 NestedLoopCount, Clauses, AStmt,
6868 B, DSAStack->isCancelRegion());
6869}
6870
Samuel Antaodf67fc42016-01-19 19:15:56 +00006871/// \brief Check for existence of a map clause in the list of clauses.
6872static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6873 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6874 I != E; ++I) {
6875 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6876 return true;
6877 }
6878 }
6879
6880 return false;
6881}
6882
Michael Wong65f367f2015-07-21 13:44:28 +00006883StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6884 Stmt *AStmt,
6885 SourceLocation StartLoc,
6886 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006887 if (!AStmt)
6888 return StmtError();
6889
6890 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6891
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006892 // OpenMP [2.10.1, Restrictions, p. 97]
6893 // At least one map clause must appear on the directive.
6894 if (!HasMapClause(Clauses)) {
6895 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6896 getOpenMPDirectiveName(OMPD_target_data);
6897 return StmtError();
6898 }
6899
Michael Wong65f367f2015-07-21 13:44:28 +00006900 getCurFunction()->setHasBranchProtectedScope();
6901
6902 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6903 AStmt);
6904}
6905
Samuel Antaodf67fc42016-01-19 19:15:56 +00006906StmtResult
6907Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6908 SourceLocation StartLoc,
6909 SourceLocation EndLoc) {
6910 // OpenMP [2.10.2, Restrictions, p. 99]
6911 // At least one map clause must appear on the directive.
6912 if (!HasMapClause(Clauses)) {
6913 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6914 << getOpenMPDirectiveName(OMPD_target_enter_data);
6915 return StmtError();
6916 }
6917
6918 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6919 Clauses);
6920}
6921
Samuel Antao72590762016-01-19 20:04:50 +00006922StmtResult
6923Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6924 SourceLocation StartLoc,
6925 SourceLocation EndLoc) {
6926 // OpenMP [2.10.3, Restrictions, p. 102]
6927 // At least one map clause must appear on the directive.
6928 if (!HasMapClause(Clauses)) {
6929 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6930 << getOpenMPDirectiveName(OMPD_target_exit_data);
6931 return StmtError();
6932 }
6933
6934 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6935}
6936
Samuel Antao686c70c2016-05-26 17:30:50 +00006937StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6938 SourceLocation StartLoc,
6939 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006940 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00006941 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00006942 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00006943 seenMotionClause = true;
6944 }
Samuel Antao686c70c2016-05-26 17:30:50 +00006945 if (!seenMotionClause) {
6946 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6947 return StmtError();
6948 }
6949 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6950}
6951
Alexey Bataev13314bf2014-10-09 04:18:56 +00006952StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6953 Stmt *AStmt, SourceLocation StartLoc,
6954 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006955 if (!AStmt)
6956 return StmtError();
6957
Alexey Bataev13314bf2014-10-09 04:18:56 +00006958 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6959 // 1.2.2 OpenMP Language Terminology
6960 // Structured block - An executable statement with a single entry at the
6961 // top and a single exit at the bottom.
6962 // The point of exit cannot be a branch out of the structured block.
6963 // longjmp() and throw() must not violate the entry/exit criteria.
6964 CS->getCapturedDecl()->setNothrow();
6965
6966 getCurFunction()->setHasBranchProtectedScope();
6967
6968 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6969}
6970
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006971StmtResult
6972Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6973 SourceLocation EndLoc,
6974 OpenMPDirectiveKind CancelRegion) {
6975 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6976 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6977 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6978 << getOpenMPDirectiveName(CancelRegion);
6979 return StmtError();
6980 }
6981 if (DSAStack->isParentNowaitRegion()) {
6982 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6983 return StmtError();
6984 }
6985 if (DSAStack->isParentOrderedRegion()) {
6986 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6987 return StmtError();
6988 }
6989 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6990 CancelRegion);
6991}
6992
Alexey Bataev87933c72015-09-18 08:07:34 +00006993StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6994 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006995 SourceLocation EndLoc,
6996 OpenMPDirectiveKind CancelRegion) {
6997 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6998 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6999 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7000 << getOpenMPDirectiveName(CancelRegion);
7001 return StmtError();
7002 }
7003 if (DSAStack->isParentNowaitRegion()) {
7004 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7005 return StmtError();
7006 }
7007 if (DSAStack->isParentOrderedRegion()) {
7008 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7009 return StmtError();
7010 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007011 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007012 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7013 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007014}
7015
Alexey Bataev382967a2015-12-08 12:06:20 +00007016static bool checkGrainsizeNumTasksClauses(Sema &S,
7017 ArrayRef<OMPClause *> Clauses) {
7018 OMPClause *PrevClause = nullptr;
7019 bool ErrorFound = false;
7020 for (auto *C : Clauses) {
7021 if (C->getClauseKind() == OMPC_grainsize ||
7022 C->getClauseKind() == OMPC_num_tasks) {
7023 if (!PrevClause)
7024 PrevClause = C;
7025 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
7026 S.Diag(C->getLocStart(),
7027 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7028 << getOpenMPClauseName(C->getClauseKind())
7029 << getOpenMPClauseName(PrevClause->getClauseKind());
7030 S.Diag(PrevClause->getLocStart(),
7031 diag::note_omp_previous_grainsize_num_tasks)
7032 << getOpenMPClauseName(PrevClause->getClauseKind());
7033 ErrorFound = true;
7034 }
7035 }
7036 }
7037 return ErrorFound;
7038}
7039
Alexey Bataev49f6e782015-12-01 04:18:41 +00007040StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7041 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7042 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007043 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007044 if (!AStmt)
7045 return StmtError();
7046
7047 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7048 OMPLoopDirective::HelperExprs B;
7049 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7050 // define the nested loops number.
7051 unsigned NestedLoopCount =
7052 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007053 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007054 VarsWithImplicitDSA, B);
7055 if (NestedLoopCount == 0)
7056 return StmtError();
7057
7058 assert((CurContext->isDependentContext() || B.builtAll()) &&
7059 "omp for loop exprs were not built");
7060
Alexey Bataev382967a2015-12-08 12:06:20 +00007061 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7062 // The grainsize clause and num_tasks clause are mutually exclusive and may
7063 // not appear on the same taskloop directive.
7064 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7065 return StmtError();
7066
Alexey Bataev49f6e782015-12-01 04:18:41 +00007067 getCurFunction()->setHasBranchProtectedScope();
7068 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7069 NestedLoopCount, Clauses, AStmt, B);
7070}
7071
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007072StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7073 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7074 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007075 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007076 if (!AStmt)
7077 return StmtError();
7078
7079 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7080 OMPLoopDirective::HelperExprs B;
7081 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7082 // define the nested loops number.
7083 unsigned NestedLoopCount =
7084 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7085 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7086 VarsWithImplicitDSA, B);
7087 if (NestedLoopCount == 0)
7088 return StmtError();
7089
7090 assert((CurContext->isDependentContext() || B.builtAll()) &&
7091 "omp for loop exprs were not built");
7092
Alexey Bataev5a3af132016-03-29 08:58:54 +00007093 if (!CurContext->isDependentContext()) {
7094 // Finalize the clauses that need pre-built expressions for CodeGen.
7095 for (auto C : Clauses) {
7096 if (auto LC = dyn_cast<OMPLinearClause>(C))
7097 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007098 B.NumIterations, *this, CurScope,
7099 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007100 return StmtError();
7101 }
7102 }
7103
Alexey Bataev382967a2015-12-08 12:06:20 +00007104 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7105 // The grainsize clause and num_tasks clause are mutually exclusive and may
7106 // not appear on the same taskloop directive.
7107 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7108 return StmtError();
7109
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007110 getCurFunction()->setHasBranchProtectedScope();
7111 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7112 NestedLoopCount, Clauses, AStmt, B);
7113}
7114
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007115StmtResult Sema::ActOnOpenMPDistributeDirective(
7116 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7117 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007118 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007119 if (!AStmt)
7120 return StmtError();
7121
7122 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7123 OMPLoopDirective::HelperExprs B;
7124 // In presence of clause 'collapse' with number of loops, it will
7125 // define the nested loops number.
7126 unsigned NestedLoopCount =
7127 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7128 nullptr /*ordered not a clause on distribute*/, AStmt,
7129 *this, *DSAStack, VarsWithImplicitDSA, B);
7130 if (NestedLoopCount == 0)
7131 return StmtError();
7132
7133 assert((CurContext->isDependentContext() || B.builtAll()) &&
7134 "omp for loop exprs were not built");
7135
7136 getCurFunction()->setHasBranchProtectedScope();
7137 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7138 NestedLoopCount, Clauses, AStmt, B);
7139}
7140
Carlo Bertolli9925f152016-06-27 14:55:37 +00007141StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7142 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7143 SourceLocation EndLoc,
7144 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7145 if (!AStmt)
7146 return StmtError();
7147
7148 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7149 // 1.2.2 OpenMP Language Terminology
7150 // Structured block - An executable statement with a single entry at the
7151 // top and a single exit at the bottom.
7152 // The point of exit cannot be a branch out of the structured block.
7153 // longjmp() and throw() must not violate the entry/exit criteria.
7154 CS->getCapturedDecl()->setNothrow();
7155
7156 OMPLoopDirective::HelperExprs B;
7157 // In presence of clause 'collapse' with number of loops, it will
7158 // define the nested loops number.
7159 unsigned NestedLoopCount = CheckOpenMPLoop(
7160 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7161 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7162 VarsWithImplicitDSA, B);
7163 if (NestedLoopCount == 0)
7164 return StmtError();
7165
7166 assert((CurContext->isDependentContext() || B.builtAll()) &&
7167 "omp for loop exprs were not built");
7168
7169 getCurFunction()->setHasBranchProtectedScope();
7170 return OMPDistributeParallelForDirective::Create(
7171 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7172}
7173
Kelvin Li4a39add2016-07-05 05:00:15 +00007174StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7175 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7176 SourceLocation EndLoc,
7177 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7178 if (!AStmt)
7179 return StmtError();
7180
7181 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7182 // 1.2.2 OpenMP Language Terminology
7183 // Structured block - An executable statement with a single entry at the
7184 // top and a single exit at the bottom.
7185 // The point of exit cannot be a branch out of the structured block.
7186 // longjmp() and throw() must not violate the entry/exit criteria.
7187 CS->getCapturedDecl()->setNothrow();
7188
7189 OMPLoopDirective::HelperExprs B;
7190 // In presence of clause 'collapse' with number of loops, it will
7191 // define the nested loops number.
7192 unsigned NestedLoopCount = CheckOpenMPLoop(
7193 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7194 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7195 VarsWithImplicitDSA, B);
7196 if (NestedLoopCount == 0)
7197 return StmtError();
7198
7199 assert((CurContext->isDependentContext() || B.builtAll()) &&
7200 "omp for loop exprs were not built");
7201
Kelvin Lic5609492016-07-15 04:39:07 +00007202 if (checkSimdlenSafelenSpecified(*this, Clauses))
7203 return StmtError();
7204
Kelvin Li4a39add2016-07-05 05:00:15 +00007205 getCurFunction()->setHasBranchProtectedScope();
7206 return OMPDistributeParallelForSimdDirective::Create(
7207 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7208}
7209
Kelvin Li787f3fc2016-07-06 04:45:38 +00007210StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7211 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7212 SourceLocation EndLoc,
7213 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7214 if (!AStmt)
7215 return StmtError();
7216
7217 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7218 // 1.2.2 OpenMP Language Terminology
7219 // Structured block - An executable statement with a single entry at the
7220 // top and a single exit at the bottom.
7221 // The point of exit cannot be a branch out of the structured block.
7222 // longjmp() and throw() must not violate the entry/exit criteria.
7223 CS->getCapturedDecl()->setNothrow();
7224
7225 OMPLoopDirective::HelperExprs B;
7226 // In presence of clause 'collapse' with number of loops, it will
7227 // define the nested loops number.
7228 unsigned NestedLoopCount =
7229 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7230 nullptr /*ordered not a clause on distribute*/, AStmt,
7231 *this, *DSAStack, VarsWithImplicitDSA, B);
7232 if (NestedLoopCount == 0)
7233 return StmtError();
7234
7235 assert((CurContext->isDependentContext() || B.builtAll()) &&
7236 "omp for loop exprs were not built");
7237
Kelvin Lic5609492016-07-15 04:39:07 +00007238 if (checkSimdlenSafelenSpecified(*this, Clauses))
7239 return StmtError();
7240
Kelvin Li787f3fc2016-07-06 04:45:38 +00007241 getCurFunction()->setHasBranchProtectedScope();
7242 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7243 NestedLoopCount, Clauses, AStmt, B);
7244}
7245
Kelvin Lia579b912016-07-14 02:54:56 +00007246StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7247 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7248 SourceLocation EndLoc,
7249 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7250 if (!AStmt)
7251 return StmtError();
7252
7253 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7254 // 1.2.2 OpenMP Language Terminology
7255 // Structured block - An executable statement with a single entry at the
7256 // top and a single exit at the bottom.
7257 // The point of exit cannot be a branch out of the structured block.
7258 // longjmp() and throw() must not violate the entry/exit criteria.
7259 CS->getCapturedDecl()->setNothrow();
7260
7261 OMPLoopDirective::HelperExprs B;
7262 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7263 // define the nested loops number.
7264 unsigned NestedLoopCount = CheckOpenMPLoop(
7265 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7266 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7267 VarsWithImplicitDSA, B);
7268 if (NestedLoopCount == 0)
7269 return StmtError();
7270
7271 assert((CurContext->isDependentContext() || B.builtAll()) &&
7272 "omp target parallel for simd loop exprs were not built");
7273
7274 if (!CurContext->isDependentContext()) {
7275 // Finalize the clauses that need pre-built expressions for CodeGen.
7276 for (auto C : Clauses) {
7277 if (auto LC = dyn_cast<OMPLinearClause>(C))
7278 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7279 B.NumIterations, *this, CurScope,
7280 DSAStack))
7281 return StmtError();
7282 }
7283 }
Kelvin Lic5609492016-07-15 04:39:07 +00007284 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007285 return StmtError();
7286
7287 getCurFunction()->setHasBranchProtectedScope();
7288 return OMPTargetParallelForSimdDirective::Create(
7289 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7290}
7291
Kelvin Li986330c2016-07-20 22:57:10 +00007292StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7293 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7294 SourceLocation EndLoc,
7295 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7296 if (!AStmt)
7297 return StmtError();
7298
7299 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7300 // 1.2.2 OpenMP Language Terminology
7301 // Structured block - An executable statement with a single entry at the
7302 // top and a single exit at the bottom.
7303 // The point of exit cannot be a branch out of the structured block.
7304 // longjmp() and throw() must not violate the entry/exit criteria.
7305 CS->getCapturedDecl()->setNothrow();
7306
7307 OMPLoopDirective::HelperExprs B;
7308 // In presence of clause 'collapse' with number of loops, it will define the
7309 // nested loops number.
7310 unsigned NestedLoopCount =
7311 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7312 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7313 VarsWithImplicitDSA, B);
7314 if (NestedLoopCount == 0)
7315 return StmtError();
7316
7317 assert((CurContext->isDependentContext() || B.builtAll()) &&
7318 "omp target simd loop exprs were not built");
7319
7320 if (!CurContext->isDependentContext()) {
7321 // Finalize the clauses that need pre-built expressions for CodeGen.
7322 for (auto C : Clauses) {
7323 if (auto LC = dyn_cast<OMPLinearClause>(C))
7324 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7325 B.NumIterations, *this, CurScope,
7326 DSAStack))
7327 return StmtError();
7328 }
7329 }
7330
7331 if (checkSimdlenSafelenSpecified(*this, Clauses))
7332 return StmtError();
7333
7334 getCurFunction()->setHasBranchProtectedScope();
7335 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7336 NestedLoopCount, Clauses, AStmt, B);
7337}
7338
Alexey Bataeved09d242014-05-28 05:53:51 +00007339OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007340 SourceLocation StartLoc,
7341 SourceLocation LParenLoc,
7342 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007343 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007344 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007345 case OMPC_final:
7346 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7347 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007348 case OMPC_num_threads:
7349 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7350 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007351 case OMPC_safelen:
7352 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7353 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007354 case OMPC_simdlen:
7355 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7356 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007357 case OMPC_collapse:
7358 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7359 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007360 case OMPC_ordered:
7361 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7362 break;
Michael Wonge710d542015-08-07 16:16:36 +00007363 case OMPC_device:
7364 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7365 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007366 case OMPC_num_teams:
7367 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7368 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007369 case OMPC_thread_limit:
7370 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7371 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007372 case OMPC_priority:
7373 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7374 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007375 case OMPC_grainsize:
7376 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7377 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007378 case OMPC_num_tasks:
7379 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7380 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007381 case OMPC_hint:
7382 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7383 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007384 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007385 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007386 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007387 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007388 case OMPC_private:
7389 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007390 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007391 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007392 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007393 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007394 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007395 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007396 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007397 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007398 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007399 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007400 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007401 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007402 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007403 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007404 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007405 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007406 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007407 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007408 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007409 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007410 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007411 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007412 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007413 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007414 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007415 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007416 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007417 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007418 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007419 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007420 llvm_unreachable("Clause is not allowed.");
7421 }
7422 return Res;
7423}
7424
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007425OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7426 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007427 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007428 SourceLocation NameModifierLoc,
7429 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007430 SourceLocation EndLoc) {
7431 Expr *ValExpr = Condition;
7432 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7433 !Condition->isInstantiationDependent() &&
7434 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007435 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007436 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007437 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007438
Richard Smith03a4aa32016-06-23 19:02:52 +00007439 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007440 }
7441
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007442 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7443 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007444}
7445
Alexey Bataev3778b602014-07-17 07:32:53 +00007446OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7447 SourceLocation StartLoc,
7448 SourceLocation LParenLoc,
7449 SourceLocation EndLoc) {
7450 Expr *ValExpr = Condition;
7451 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7452 !Condition->isInstantiationDependent() &&
7453 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007454 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007455 if (Val.isInvalid())
7456 return nullptr;
7457
Richard Smith03a4aa32016-06-23 19:02:52 +00007458 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007459 }
7460
7461 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7462}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007463ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7464 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007465 if (!Op)
7466 return ExprError();
7467
7468 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7469 public:
7470 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007471 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007472 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7473 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007474 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7475 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007476 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7477 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007478 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7479 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007480 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7481 QualType T,
7482 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007483 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7484 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007485 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7486 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007487 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007488 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007489 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007490 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7491 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007492 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7493 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007494 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7495 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007496 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007497 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007498 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007499 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7500 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007501 llvm_unreachable("conversion functions are permitted");
7502 }
7503 } ConvertDiagnoser;
7504 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7505}
7506
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007507static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007508 OpenMPClauseKind CKind,
7509 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007510 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7511 !ValExpr->isInstantiationDependent()) {
7512 SourceLocation Loc = ValExpr->getExprLoc();
7513 ExprResult Value =
7514 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7515 if (Value.isInvalid())
7516 return false;
7517
7518 ValExpr = Value.get();
7519 // The expression must evaluate to a non-negative integer value.
7520 llvm::APSInt Result;
7521 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007522 Result.isSigned() &&
7523 !((!StrictlyPositive && Result.isNonNegative()) ||
7524 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007525 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007526 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7527 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007528 return false;
7529 }
7530 }
7531 return true;
7532}
7533
Alexey Bataev568a8332014-03-06 06:15:19 +00007534OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7535 SourceLocation StartLoc,
7536 SourceLocation LParenLoc,
7537 SourceLocation EndLoc) {
7538 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007539
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007540 // OpenMP [2.5, Restrictions]
7541 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007542 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7543 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007544 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007545
Alexey Bataeved09d242014-05-28 05:53:51 +00007546 return new (Context)
7547 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007548}
7549
Alexey Bataev62c87d22014-03-21 04:51:18 +00007550ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007551 OpenMPClauseKind CKind,
7552 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007553 if (!E)
7554 return ExprError();
7555 if (E->isValueDependent() || E->isTypeDependent() ||
7556 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007557 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007558 llvm::APSInt Result;
7559 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7560 if (ICE.isInvalid())
7561 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007562 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7563 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007564 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007565 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7566 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007567 return ExprError();
7568 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007569 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7570 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7571 << E->getSourceRange();
7572 return ExprError();
7573 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007574 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7575 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007576 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007577 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007578 return ICE;
7579}
7580
7581OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7582 SourceLocation LParenLoc,
7583 SourceLocation EndLoc) {
7584 // OpenMP [2.8.1, simd construct, Description]
7585 // The parameter of the safelen clause must be a constant
7586 // positive integer expression.
7587 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7588 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007589 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007590 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007591 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007592}
7593
Alexey Bataev66b15b52015-08-21 11:14:16 +00007594OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7595 SourceLocation LParenLoc,
7596 SourceLocation EndLoc) {
7597 // OpenMP [2.8.1, simd construct, Description]
7598 // The parameter of the simdlen clause must be a constant
7599 // positive integer expression.
7600 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7601 if (Simdlen.isInvalid())
7602 return nullptr;
7603 return new (Context)
7604 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7605}
7606
Alexander Musman64d33f12014-06-04 07:53:32 +00007607OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7608 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007609 SourceLocation LParenLoc,
7610 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007611 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007612 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007613 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007614 // The parameter of the collapse clause must be a constant
7615 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007616 ExprResult NumForLoopsResult =
7617 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7618 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007619 return nullptr;
7620 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007621 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007622}
7623
Alexey Bataev10e775f2015-07-30 11:36:16 +00007624OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7625 SourceLocation EndLoc,
7626 SourceLocation LParenLoc,
7627 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007628 // OpenMP [2.7.1, loop construct, Description]
7629 // OpenMP [2.8.1, simd construct, Description]
7630 // OpenMP [2.9.6, distribute construct, Description]
7631 // The parameter of the ordered clause must be a constant
7632 // positive integer expression if any.
7633 if (NumForLoops && LParenLoc.isValid()) {
7634 ExprResult NumForLoopsResult =
7635 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7636 if (NumForLoopsResult.isInvalid())
7637 return nullptr;
7638 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007639 } else
7640 NumForLoops = nullptr;
7641 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007642 return new (Context)
7643 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7644}
7645
Alexey Bataeved09d242014-05-28 05:53:51 +00007646OMPClause *Sema::ActOnOpenMPSimpleClause(
7647 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7648 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007649 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007650 switch (Kind) {
7651 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007652 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007653 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7654 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007655 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007656 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007657 Res = ActOnOpenMPProcBindClause(
7658 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7659 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007660 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007661 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007662 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007663 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007664 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007665 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007666 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007667 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007668 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007669 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007670 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007671 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007672 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007673 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007674 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007675 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007676 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007677 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007678 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007679 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007680 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007681 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007682 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007683 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007684 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007685 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007686 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007687 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007688 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007689 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007690 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007691 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007692 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007693 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007694 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007695 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007696 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007697 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007698 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007699 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007700 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007701 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007702 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007703 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007704 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007705 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007706 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007707 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007708 llvm_unreachable("Clause is not allowed.");
7709 }
7710 return Res;
7711}
7712
Alexey Bataev6402bca2015-12-28 07:25:51 +00007713static std::string
7714getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7715 ArrayRef<unsigned> Exclude = llvm::None) {
7716 std::string Values;
7717 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7718 unsigned Skipped = Exclude.size();
7719 auto S = Exclude.begin(), E = Exclude.end();
7720 for (unsigned i = First; i < Last; ++i) {
7721 if (std::find(S, E, i) != E) {
7722 --Skipped;
7723 continue;
7724 }
7725 Values += "'";
7726 Values += getOpenMPSimpleClauseTypeName(K, i);
7727 Values += "'";
7728 if (i == Bound - Skipped)
7729 Values += " or ";
7730 else if (i != Bound + 1 - Skipped)
7731 Values += ", ";
7732 }
7733 return Values;
7734}
7735
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007736OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7737 SourceLocation KindKwLoc,
7738 SourceLocation StartLoc,
7739 SourceLocation LParenLoc,
7740 SourceLocation EndLoc) {
7741 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007742 static_assert(OMPC_DEFAULT_unknown > 0,
7743 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007744 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007745 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7746 /*Last=*/OMPC_DEFAULT_unknown)
7747 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007748 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007749 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007750 switch (Kind) {
7751 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007752 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007753 break;
7754 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007755 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007756 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007757 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007758 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007759 break;
7760 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007761 return new (Context)
7762 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007763}
7764
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007765OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7766 SourceLocation KindKwLoc,
7767 SourceLocation StartLoc,
7768 SourceLocation LParenLoc,
7769 SourceLocation EndLoc) {
7770 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007771 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007772 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7773 /*Last=*/OMPC_PROC_BIND_unknown)
7774 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007775 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007776 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007777 return new (Context)
7778 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007779}
7780
Alexey Bataev56dafe82014-06-20 07:16:17 +00007781OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007782 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007783 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007784 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007785 SourceLocation EndLoc) {
7786 OMPClause *Res = nullptr;
7787 switch (Kind) {
7788 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007789 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7790 assert(Argument.size() == NumberOfElements &&
7791 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007792 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007793 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7794 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7795 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7796 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7797 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007798 break;
7799 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007800 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7801 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7802 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7803 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007804 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007805 case OMPC_dist_schedule:
7806 Res = ActOnOpenMPDistScheduleClause(
7807 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7808 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7809 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007810 case OMPC_defaultmap:
7811 enum { Modifier, DefaultmapKind };
7812 Res = ActOnOpenMPDefaultmapClause(
7813 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7814 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7815 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7816 ArgumentLoc[DefaultmapKind], EndLoc);
7817 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007818 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007819 case OMPC_num_threads:
7820 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007821 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007822 case OMPC_collapse:
7823 case OMPC_default:
7824 case OMPC_proc_bind:
7825 case OMPC_private:
7826 case OMPC_firstprivate:
7827 case OMPC_lastprivate:
7828 case OMPC_shared:
7829 case OMPC_reduction:
7830 case OMPC_linear:
7831 case OMPC_aligned:
7832 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007833 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007834 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007835 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007836 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007837 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007838 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007839 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007840 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007841 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007842 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007843 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007844 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007845 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007846 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007847 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007848 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007849 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007850 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007851 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007852 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007853 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007854 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007855 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007856 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007857 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007858 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007859 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007860 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007861 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007862 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007863 llvm_unreachable("Clause is not allowed.");
7864 }
7865 return Res;
7866}
7867
Alexey Bataev6402bca2015-12-28 07:25:51 +00007868static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7869 OpenMPScheduleClauseModifier M2,
7870 SourceLocation M1Loc, SourceLocation M2Loc) {
7871 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7872 SmallVector<unsigned, 2> Excluded;
7873 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7874 Excluded.push_back(M2);
7875 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7876 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7877 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7878 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7879 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7880 << getListOfPossibleValues(OMPC_schedule,
7881 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7882 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7883 Excluded)
7884 << getOpenMPClauseName(OMPC_schedule);
7885 return true;
7886 }
7887 return false;
7888}
7889
Alexey Bataev56dafe82014-06-20 07:16:17 +00007890OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007891 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007892 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007893 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7894 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7895 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7896 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7897 return nullptr;
7898 // OpenMP, 2.7.1, Loop Construct, Restrictions
7899 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7900 // but not both.
7901 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7902 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7903 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7904 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7905 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7906 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7907 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7908 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7909 return nullptr;
7910 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007911 if (Kind == OMPC_SCHEDULE_unknown) {
7912 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007913 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7914 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7915 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7916 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7917 Exclude);
7918 } else {
7919 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7920 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007921 }
7922 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7923 << Values << getOpenMPClauseName(OMPC_schedule);
7924 return nullptr;
7925 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007926 // OpenMP, 2.7.1, Loop Construct, Restrictions
7927 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7928 // schedule(guided).
7929 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7930 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7931 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7932 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7933 diag::err_omp_schedule_nonmonotonic_static);
7934 return nullptr;
7935 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007936 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007937 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007938 if (ChunkSize) {
7939 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7940 !ChunkSize->isInstantiationDependent() &&
7941 !ChunkSize->containsUnexpandedParameterPack()) {
7942 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7943 ExprResult Val =
7944 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7945 if (Val.isInvalid())
7946 return nullptr;
7947
7948 ValExpr = Val.get();
7949
7950 // OpenMP [2.7.1, Restrictions]
7951 // chunk_size must be a loop invariant integer expression with a positive
7952 // value.
7953 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007954 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7955 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7956 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007957 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007958 return nullptr;
7959 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007960 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7961 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007962 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7963 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7964 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007965 }
7966 }
7967 }
7968
Alexey Bataev6402bca2015-12-28 07:25:51 +00007969 return new (Context)
7970 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007971 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007972}
7973
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007974OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7975 SourceLocation StartLoc,
7976 SourceLocation EndLoc) {
7977 OMPClause *Res = nullptr;
7978 switch (Kind) {
7979 case OMPC_ordered:
7980 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7981 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007982 case OMPC_nowait:
7983 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7984 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007985 case OMPC_untied:
7986 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7987 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007988 case OMPC_mergeable:
7989 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7990 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007991 case OMPC_read:
7992 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7993 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007994 case OMPC_write:
7995 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7996 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007997 case OMPC_update:
7998 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7999 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00008000 case OMPC_capture:
8001 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8002 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008003 case OMPC_seq_cst:
8004 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8005 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00008006 case OMPC_threads:
8007 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8008 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008009 case OMPC_simd:
8010 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8011 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00008012 case OMPC_nogroup:
8013 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8014 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008015 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008016 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008017 case OMPC_num_threads:
8018 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008019 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008020 case OMPC_collapse:
8021 case OMPC_schedule:
8022 case OMPC_private:
8023 case OMPC_firstprivate:
8024 case OMPC_lastprivate:
8025 case OMPC_shared:
8026 case OMPC_reduction:
8027 case OMPC_linear:
8028 case OMPC_aligned:
8029 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008030 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008031 case OMPC_default:
8032 case OMPC_proc_bind:
8033 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008034 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008035 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008036 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008037 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008038 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008039 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008040 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008041 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00008042 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008043 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008044 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008045 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008046 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008047 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008048 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008049 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008050 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008051 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008052 llvm_unreachable("Clause is not allowed.");
8053 }
8054 return Res;
8055}
8056
Alexey Bataev236070f2014-06-20 11:19:47 +00008057OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
8058 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008059 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00008060 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
8061}
8062
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008063OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
8064 SourceLocation EndLoc) {
8065 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
8066}
8067
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008068OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
8069 SourceLocation EndLoc) {
8070 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
8071}
8072
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008073OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
8074 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008075 return new (Context) OMPReadClause(StartLoc, EndLoc);
8076}
8077
Alexey Bataevdea47612014-07-23 07:46:59 +00008078OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
8079 SourceLocation EndLoc) {
8080 return new (Context) OMPWriteClause(StartLoc, EndLoc);
8081}
8082
Alexey Bataev67a4f222014-07-23 10:25:33 +00008083OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
8084 SourceLocation EndLoc) {
8085 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
8086}
8087
Alexey Bataev459dec02014-07-24 06:46:57 +00008088OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
8089 SourceLocation EndLoc) {
8090 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
8091}
8092
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008093OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8094 SourceLocation EndLoc) {
8095 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8096}
8097
Alexey Bataev346265e2015-09-25 10:37:12 +00008098OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8099 SourceLocation EndLoc) {
8100 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8101}
8102
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008103OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8104 SourceLocation EndLoc) {
8105 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8106}
8107
Alexey Bataevb825de12015-12-07 10:51:44 +00008108OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8109 SourceLocation EndLoc) {
8110 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8111}
8112
Alexey Bataevc5e02582014-06-16 07:08:35 +00008113OMPClause *Sema::ActOnOpenMPVarListClause(
8114 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8115 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8116 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008117 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00008118 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8119 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8120 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008121 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008122 switch (Kind) {
8123 case OMPC_private:
8124 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8125 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008126 case OMPC_firstprivate:
8127 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8128 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008129 case OMPC_lastprivate:
8130 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8131 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008132 case OMPC_shared:
8133 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8134 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008135 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008136 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8137 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008138 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008139 case OMPC_linear:
8140 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008141 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008142 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008143 case OMPC_aligned:
8144 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8145 ColonLoc, EndLoc);
8146 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008147 case OMPC_copyin:
8148 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8149 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008150 case OMPC_copyprivate:
8151 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8152 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008153 case OMPC_flush:
8154 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8155 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008156 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008157 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8158 StartLoc, LParenLoc, EndLoc);
8159 break;
8160 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008161 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8162 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8163 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008164 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008165 case OMPC_to:
8166 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8167 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008168 case OMPC_from:
8169 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8170 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008171 case OMPC_use_device_ptr:
8172 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8173 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008174 case OMPC_is_device_ptr:
8175 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8176 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008177 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008178 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008179 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008180 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008181 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008182 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008183 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008184 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008185 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008186 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008187 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008188 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008189 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008190 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008191 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008192 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008193 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008194 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008195 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008196 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008197 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008198 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008199 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008200 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008201 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008202 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008203 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008204 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008205 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008206 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008207 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008208 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008209 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008210 llvm_unreachable("Clause is not allowed.");
8211 }
8212 return Res;
8213}
8214
Alexey Bataev90c228f2016-02-08 09:29:13 +00008215ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008216 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008217 ExprResult Res = BuildDeclRefExpr(
8218 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8219 if (!Res.isUsable())
8220 return ExprError();
8221 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8222 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8223 if (!Res.isUsable())
8224 return ExprError();
8225 }
8226 if (VK != VK_LValue && Res.get()->isGLValue()) {
8227 Res = DefaultLvalueConversion(Res.get());
8228 if (!Res.isUsable())
8229 return ExprError();
8230 }
8231 return Res;
8232}
8233
Alexey Bataev60da77e2016-02-29 05:54:20 +00008234static std::pair<ValueDecl *, bool>
8235getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8236 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008237 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8238 RefExpr->containsUnexpandedParameterPack())
8239 return std::make_pair(nullptr, true);
8240
Alexey Bataevd985eda2016-02-10 11:29:16 +00008241 // OpenMP [3.1, C/C++]
8242 // A list item is a variable name.
8243 // OpenMP [2.9.3.3, Restrictions, p.1]
8244 // A variable that is part of another variable (as an array or
8245 // structure element) cannot appear in a private clause.
8246 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008247 enum {
8248 NoArrayExpr = -1,
8249 ArraySubscript = 0,
8250 OMPArraySection = 1
8251 } IsArrayExpr = NoArrayExpr;
8252 if (AllowArraySection) {
8253 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8254 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8255 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8256 Base = TempASE->getBase()->IgnoreParenImpCasts();
8257 RefExpr = Base;
8258 IsArrayExpr = ArraySubscript;
8259 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8260 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8261 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8262 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8263 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8264 Base = TempASE->getBase()->IgnoreParenImpCasts();
8265 RefExpr = Base;
8266 IsArrayExpr = OMPArraySection;
8267 }
8268 }
8269 ELoc = RefExpr->getExprLoc();
8270 ERange = RefExpr->getSourceRange();
8271 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008272 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8273 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8274 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8275 (S.getCurrentThisType().isNull() || !ME ||
8276 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8277 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008278 if (IsArrayExpr != NoArrayExpr)
8279 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8280 << ERange;
8281 else {
8282 S.Diag(ELoc,
8283 AllowArraySection
8284 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8285 : diag::err_omp_expected_var_name_member_expr)
8286 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8287 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008288 return std::make_pair(nullptr, false);
8289 }
8290 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8291}
8292
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008293OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8294 SourceLocation StartLoc,
8295 SourceLocation LParenLoc,
8296 SourceLocation EndLoc) {
8297 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008298 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008299 for (auto &RefExpr : VarList) {
8300 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008301 SourceLocation ELoc;
8302 SourceRange ERange;
8303 Expr *SimpleRefExpr = RefExpr;
8304 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008305 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008306 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008307 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008308 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008309 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008310 ValueDecl *D = Res.first;
8311 if (!D)
8312 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008313
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008314 QualType Type = D->getType();
8315 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008316
8317 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8318 // A variable that appears in a private clause must not have an incomplete
8319 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008320 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008321 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008322 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008323
Alexey Bataev758e55e2013-09-06 18:03:48 +00008324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8325 // in a Construct]
8326 // Variables with the predetermined data-sharing attributes may not be
8327 // listed in data-sharing attributes clauses, except for the cases
8328 // listed below. For these exceptions only, listing a predetermined
8329 // variable in a data-sharing attribute clause is allowed and overrides
8330 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008331 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008332 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008333 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8334 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008335 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008336 continue;
8337 }
8338
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008339 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008340 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008341 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008342 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8343 << getOpenMPClauseName(OMPC_private) << Type
8344 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8345 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008346 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008347 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008348 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008349 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008350 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008351 continue;
8352 }
8353
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008354 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8355 // A list item cannot appear in both a map clause and a data-sharing
8356 // attribute clause on the same construct
8357 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008358 if (DSAStack->checkMappableExprComponentListsForDecl(
8359 VD, /* CurrentRegionOnly = */ true,
8360 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8361 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008362 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8363 << getOpenMPClauseName(OMPC_private)
8364 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8365 ReportOriginalDSA(*this, DSAStack, D, DVar);
8366 continue;
8367 }
8368 }
8369
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008370 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8371 // A variable of class type (or array thereof) that appears in a private
8372 // clause requires an accessible, unambiguous default constructor for the
8373 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008374 // Generate helper private variable and initialize it with the default
8375 // value. The address of the original variable is replaced by the address of
8376 // the new private variable in CodeGen. This new variable is not added to
8377 // IdResolver, so the code in the OpenMP region uses original variable for
8378 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008379 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008380 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8381 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008382 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008383 if (VDPrivate->isInvalidDecl())
8384 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008385 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008386 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008387
Alexey Bataev90c228f2016-02-08 09:29:13 +00008388 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008389 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008390 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008391 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008392 Vars.push_back((VD || CurContext->isDependentContext())
8393 ? RefExpr->IgnoreParens()
8394 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008395 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008396 }
8397
Alexey Bataeved09d242014-05-28 05:53:51 +00008398 if (Vars.empty())
8399 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008400
Alexey Bataev03b340a2014-10-21 03:16:40 +00008401 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8402 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008403}
8404
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008405namespace {
8406class DiagsUninitializedSeveretyRAII {
8407private:
8408 DiagnosticsEngine &Diags;
8409 SourceLocation SavedLoc;
8410 bool IsIgnored;
8411
8412public:
8413 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8414 bool IsIgnored)
8415 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8416 if (!IsIgnored) {
8417 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8418 /*Map*/ diag::Severity::Ignored, Loc);
8419 }
8420 }
8421 ~DiagsUninitializedSeveretyRAII() {
8422 if (!IsIgnored)
8423 Diags.popMappings(SavedLoc);
8424 }
8425};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008426}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008427
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008428OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8429 SourceLocation StartLoc,
8430 SourceLocation LParenLoc,
8431 SourceLocation EndLoc) {
8432 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008433 SmallVector<Expr *, 8> PrivateCopies;
8434 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008435 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008436 bool IsImplicitClause =
8437 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8438 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8439
Alexey Bataeved09d242014-05-28 05:53:51 +00008440 for (auto &RefExpr : VarList) {
8441 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008442 SourceLocation ELoc;
8443 SourceRange ERange;
8444 Expr *SimpleRefExpr = RefExpr;
8445 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008446 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008447 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008448 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008449 PrivateCopies.push_back(nullptr);
8450 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008451 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008452 ValueDecl *D = Res.first;
8453 if (!D)
8454 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008455
Alexey Bataev60da77e2016-02-29 05:54:20 +00008456 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008457 QualType Type = D->getType();
8458 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008459
8460 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8461 // A variable that appears in a private clause must not have an incomplete
8462 // type or a reference type.
8463 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008464 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008465 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008466 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008467
8468 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8469 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008470 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008471 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008472 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008473
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008474 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008475 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008476 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008477 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008478 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008479 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008480 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8481 // A list item that specifies a given variable may not appear in more
8482 // than one clause on the same directive, except that a variable may be
8483 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008484 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008485 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008486 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008487 << getOpenMPClauseName(DVar.CKind)
8488 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008489 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008490 continue;
8491 }
8492
8493 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8494 // in a Construct]
8495 // Variables with the predetermined data-sharing attributes may not be
8496 // listed in data-sharing attributes clauses, except for the cases
8497 // listed below. For these exceptions only, listing a predetermined
8498 // variable in a data-sharing attribute clause is allowed and overrides
8499 // the variable's predetermined data-sharing attributes.
8500 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8501 // in a Construct, C/C++, p.2]
8502 // Variables with const-qualified type having no mutable member may be
8503 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008504 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008505 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8506 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008507 << getOpenMPClauseName(DVar.CKind)
8508 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008509 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008510 continue;
8511 }
8512
Alexey Bataevf29276e2014-06-18 04:14:57 +00008513 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008514 // OpenMP [2.9.3.4, Restrictions, p.2]
8515 // A list item that is private within a parallel region must not appear
8516 // in a firstprivate clause on a worksharing construct if any of the
8517 // worksharing regions arising from the worksharing construct ever bind
8518 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008519 if (isOpenMPWorksharingDirective(CurrDir) &&
8520 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008521 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008522 if (DVar.CKind != OMPC_shared &&
8523 (isOpenMPParallelDirective(DVar.DKind) ||
8524 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008525 Diag(ELoc, diag::err_omp_required_access)
8526 << getOpenMPClauseName(OMPC_firstprivate)
8527 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008528 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008529 continue;
8530 }
8531 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008532 // OpenMP [2.9.3.4, Restrictions, p.3]
8533 // A list item that appears in a reduction clause of a parallel construct
8534 // must not appear in a firstprivate clause on a worksharing or task
8535 // construct if any of the worksharing or task regions arising from the
8536 // worksharing or task construct ever bind to any of the parallel regions
8537 // arising from the parallel construct.
8538 // OpenMP [2.9.3.4, Restrictions, p.4]
8539 // A list item that appears in a reduction clause in worksharing
8540 // construct must not appear in a firstprivate clause in a task construct
8541 // encountered during execution of any of the worksharing regions arising
8542 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008543 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008544 DVar = DSAStack->hasInnermostDSA(
8545 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8546 [](OpenMPDirectiveKind K) -> bool {
8547 return isOpenMPParallelDirective(K) ||
8548 isOpenMPWorksharingDirective(K);
8549 },
8550 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008551 if (DVar.CKind == OMPC_reduction &&
8552 (isOpenMPParallelDirective(DVar.DKind) ||
8553 isOpenMPWorksharingDirective(DVar.DKind))) {
8554 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8555 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008556 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008557 continue;
8558 }
8559 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008560
8561 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8562 // A list item that is private within a teams region must not appear in a
8563 // firstprivate clause on a distribute construct if any of the distribute
8564 // regions arising from the distribute construct ever bind to any of the
8565 // teams regions arising from the teams construct.
8566 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8567 // A list item that appears in a reduction clause of a teams construct
8568 // must not appear in a firstprivate clause on a distribute construct if
8569 // any of the distribute regions arising from the distribute construct
8570 // ever bind to any of the teams regions arising from the teams construct.
8571 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8572 // A list item may appear in a firstprivate or lastprivate clause but not
8573 // both.
8574 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008575 DVar = DSAStack->hasInnermostDSA(
8576 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8577 [](OpenMPDirectiveKind K) -> bool {
8578 return isOpenMPTeamsDirective(K);
8579 },
8580 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008581 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8582 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008583 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008584 continue;
8585 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008586 DVar = DSAStack->hasInnermostDSA(
8587 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8588 [](OpenMPDirectiveKind K) -> bool {
8589 return isOpenMPTeamsDirective(K);
8590 },
8591 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008592 if (DVar.CKind == OMPC_reduction &&
8593 isOpenMPTeamsDirective(DVar.DKind)) {
8594 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008595 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008596 continue;
8597 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008598 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008599 if (DVar.CKind == OMPC_lastprivate) {
8600 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008601 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008602 continue;
8603 }
8604 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008605 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8606 // A list item cannot appear in both a map clause and a data-sharing
8607 // attribute clause on the same construct
8608 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008609 if (DSAStack->checkMappableExprComponentListsForDecl(
8610 VD, /* CurrentRegionOnly = */ true,
8611 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8612 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008613 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8614 << getOpenMPClauseName(OMPC_firstprivate)
8615 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8616 ReportOriginalDSA(*this, DSAStack, D, DVar);
8617 continue;
8618 }
8619 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008620 }
8621
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008622 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008623 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008624 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008625 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8626 << getOpenMPClauseName(OMPC_firstprivate) << Type
8627 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8628 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008629 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008630 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008631 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008632 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008633 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008634 continue;
8635 }
8636
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008637 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008638 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8639 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008640 // Generate helper private variable and initialize it with the value of the
8641 // original variable. The address of the original variable is replaced by
8642 // the address of the new private variable in the CodeGen. This new variable
8643 // is not added to IdResolver, so the code in the OpenMP region uses
8644 // original variable for proper diagnostics and variable capturing.
8645 Expr *VDInitRefExpr = nullptr;
8646 // For arrays generate initializer for single element and replace it by the
8647 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008648 if (Type->isArrayType()) {
8649 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008650 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008651 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008652 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008653 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008654 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008655 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008656 InitializedEntity Entity =
8657 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008658 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8659
8660 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8661 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8662 if (Result.isInvalid())
8663 VDPrivate->setInvalidDecl();
8664 else
8665 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008666 // Remove temp variable declaration.
8667 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008668 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008669 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8670 ".firstprivate.temp");
8671 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8672 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008673 AddInitializerToDecl(VDPrivate,
8674 DefaultLvalueConversion(VDInitRefExpr).get(),
8675 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008676 }
8677 if (VDPrivate->isInvalidDecl()) {
8678 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008679 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008680 diag::note_omp_task_predetermined_firstprivate_here);
8681 }
8682 continue;
8683 }
8684 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008685 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008686 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8687 RefExpr->getExprLoc());
8688 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008689 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008690 if (TopDVar.CKind == OMPC_lastprivate)
8691 Ref = TopDVar.PrivateCopy;
8692 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008693 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008694 if (!IsOpenMPCapturedDecl(D))
8695 ExprCaptures.push_back(Ref->getDecl());
8696 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008697 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008698 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008699 Vars.push_back((VD || CurContext->isDependentContext())
8700 ? RefExpr->IgnoreParens()
8701 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008702 PrivateCopies.push_back(VDPrivateRefExpr);
8703 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008704 }
8705
Alexey Bataeved09d242014-05-28 05:53:51 +00008706 if (Vars.empty())
8707 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008708
8709 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008710 Vars, PrivateCopies, Inits,
8711 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008712}
8713
Alexander Musman1bb328c2014-06-04 13:06:39 +00008714OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8715 SourceLocation StartLoc,
8716 SourceLocation LParenLoc,
8717 SourceLocation EndLoc) {
8718 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008719 SmallVector<Expr *, 8> SrcExprs;
8720 SmallVector<Expr *, 8> DstExprs;
8721 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008722 SmallVector<Decl *, 4> ExprCaptures;
8723 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008724 for (auto &RefExpr : VarList) {
8725 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008726 SourceLocation ELoc;
8727 SourceRange ERange;
8728 Expr *SimpleRefExpr = RefExpr;
8729 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008730 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008731 // It will be analyzed later.
8732 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008733 SrcExprs.push_back(nullptr);
8734 DstExprs.push_back(nullptr);
8735 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008736 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008737 ValueDecl *D = Res.first;
8738 if (!D)
8739 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008740
Alexey Bataev74caaf22016-02-20 04:09:36 +00008741 QualType Type = D->getType();
8742 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008743
8744 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8745 // A variable that appears in a lastprivate clause must not have an
8746 // incomplete type or a reference type.
8747 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008748 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008749 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008750 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008751
8752 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8753 // in a Construct]
8754 // Variables with the predetermined data-sharing attributes may not be
8755 // listed in data-sharing attributes clauses, except for the cases
8756 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008757 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008758 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8759 DVar.CKind != OMPC_firstprivate &&
8760 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8761 Diag(ELoc, diag::err_omp_wrong_dsa)
8762 << getOpenMPClauseName(DVar.CKind)
8763 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008764 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008765 continue;
8766 }
8767
Alexey Bataevf29276e2014-06-18 04:14:57 +00008768 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8769 // OpenMP [2.14.3.5, Restrictions, p.2]
8770 // A list item that is private within a parallel region, or that appears in
8771 // the reduction clause of a parallel construct, must not appear in a
8772 // lastprivate clause on a worksharing construct if any of the corresponding
8773 // worksharing regions ever binds to any of the corresponding parallel
8774 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008775 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008776 if (isOpenMPWorksharingDirective(CurrDir) &&
8777 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008778 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008779 if (DVar.CKind != OMPC_shared) {
8780 Diag(ELoc, diag::err_omp_required_access)
8781 << getOpenMPClauseName(OMPC_lastprivate)
8782 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008783 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008784 continue;
8785 }
8786 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008787
8788 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8789 // A list item may appear in a firstprivate or lastprivate clause but not
8790 // both.
8791 if (CurrDir == OMPD_distribute) {
8792 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8793 if (DVar.CKind == OMPC_firstprivate) {
8794 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8795 ReportOriginalDSA(*this, DSAStack, D, DVar);
8796 continue;
8797 }
8798 }
8799
Alexander Musman1bb328c2014-06-04 13:06:39 +00008800 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008801 // A variable of class type (or array thereof) that appears in a
8802 // lastprivate clause requires an accessible, unambiguous default
8803 // constructor for the class type, unless the list item is also specified
8804 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008805 // A variable of class type (or array thereof) that appears in a
8806 // lastprivate clause requires an accessible, unambiguous copy assignment
8807 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008808 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008809 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008810 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008811 D->hasAttrs() ? &D->getAttrs() : nullptr);
8812 auto *PseudoSrcExpr =
8813 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008814 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008815 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008816 D->hasAttrs() ? &D->getAttrs() : nullptr);
8817 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008818 // For arrays generate assignment operation for single element and replace
8819 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008820 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008821 PseudoDstExpr, PseudoSrcExpr);
8822 if (AssignmentOp.isInvalid())
8823 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008824 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008825 /*DiscardedValue=*/true);
8826 if (AssignmentOp.isInvalid())
8827 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008828
Alexey Bataev74caaf22016-02-20 04:09:36 +00008829 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008830 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008831 if (TopDVar.CKind == OMPC_firstprivate)
8832 Ref = TopDVar.PrivateCopy;
8833 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008834 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008835 if (!IsOpenMPCapturedDecl(D))
8836 ExprCaptures.push_back(Ref->getDecl());
8837 }
8838 if (TopDVar.CKind == OMPC_firstprivate ||
8839 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008840 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008841 ExprResult RefRes = DefaultLvalueConversion(Ref);
8842 if (!RefRes.isUsable())
8843 continue;
8844 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008845 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8846 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008847 if (!PostUpdateRes.isUsable())
8848 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008849 ExprPostUpdates.push_back(
8850 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008851 }
8852 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008853 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008854 Vars.push_back((VD || CurContext->isDependentContext())
8855 ? RefExpr->IgnoreParens()
8856 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008857 SrcExprs.push_back(PseudoSrcExpr);
8858 DstExprs.push_back(PseudoDstExpr);
8859 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008860 }
8861
8862 if (Vars.empty())
8863 return nullptr;
8864
8865 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008866 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008867 buildPreInits(Context, ExprCaptures),
8868 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008869}
8870
Alexey Bataev758e55e2013-09-06 18:03:48 +00008871OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8872 SourceLocation StartLoc,
8873 SourceLocation LParenLoc,
8874 SourceLocation EndLoc) {
8875 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008876 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008877 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008878 SourceLocation ELoc;
8879 SourceRange ERange;
8880 Expr *SimpleRefExpr = RefExpr;
8881 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008882 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008883 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008884 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008885 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008886 ValueDecl *D = Res.first;
8887 if (!D)
8888 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008889
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008890 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008891 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8892 // in a Construct]
8893 // Variables with the predetermined data-sharing attributes may not be
8894 // listed in data-sharing attributes clauses, except for the cases
8895 // listed below. For these exceptions only, listing a predetermined
8896 // variable in a data-sharing attribute clause is allowed and overrides
8897 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008898 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008899 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8900 DVar.RefExpr) {
8901 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8902 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008903 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008904 continue;
8905 }
8906
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008907 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008908 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008909 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008910 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008911 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8912 ? RefExpr->IgnoreParens()
8913 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008914 }
8915
Alexey Bataeved09d242014-05-28 05:53:51 +00008916 if (Vars.empty())
8917 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008918
8919 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8920}
8921
Alexey Bataevc5e02582014-06-16 07:08:35 +00008922namespace {
8923class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8924 DSAStackTy *Stack;
8925
8926public:
8927 bool VisitDeclRefExpr(DeclRefExpr *E) {
8928 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008929 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008930 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8931 return false;
8932 if (DVar.CKind != OMPC_unknown)
8933 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008934 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8935 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8936 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008937 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008938 return true;
8939 return false;
8940 }
8941 return false;
8942 }
8943 bool VisitStmt(Stmt *S) {
8944 for (auto Child : S->children()) {
8945 if (Child && Visit(Child))
8946 return true;
8947 }
8948 return false;
8949 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008950 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008951};
Alexey Bataev23b69422014-06-18 07:08:49 +00008952} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008953
Alexey Bataev60da77e2016-02-29 05:54:20 +00008954namespace {
8955// Transform MemberExpression for specified FieldDecl of current class to
8956// DeclRefExpr to specified OMPCapturedExprDecl.
8957class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8958 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8959 ValueDecl *Field;
8960 DeclRefExpr *CapturedExpr;
8961
8962public:
8963 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8964 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8965
8966 ExprResult TransformMemberExpr(MemberExpr *E) {
8967 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8968 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008969 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008970 return CapturedExpr;
8971 }
8972 return BaseTransform::TransformMemberExpr(E);
8973 }
8974 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8975};
8976} // namespace
8977
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008978template <typename T>
8979static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8980 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8981 for (auto &Set : Lookups) {
8982 for (auto *D : Set) {
8983 if (auto Res = Gen(cast<ValueDecl>(D)))
8984 return Res;
8985 }
8986 }
8987 return T();
8988}
8989
8990static ExprResult
8991buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8992 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8993 const DeclarationNameInfo &ReductionId, QualType Ty,
8994 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8995 if (ReductionIdScopeSpec.isInvalid())
8996 return ExprError();
8997 SmallVector<UnresolvedSet<8>, 4> Lookups;
8998 if (S) {
8999 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9000 Lookup.suppressDiagnostics();
9001 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9002 auto *D = Lookup.getRepresentativeDecl();
9003 do {
9004 S = S->getParent();
9005 } while (S && !S->isDeclScope(D));
9006 if (S)
9007 S = S->getParent();
9008 Lookups.push_back(UnresolvedSet<8>());
9009 Lookups.back().append(Lookup.begin(), Lookup.end());
9010 Lookup.clear();
9011 }
9012 } else if (auto *ULE =
9013 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9014 Lookups.push_back(UnresolvedSet<8>());
9015 Decl *PrevD = nullptr;
9016 for(auto *D : ULE->decls()) {
9017 if (D == PrevD)
9018 Lookups.push_back(UnresolvedSet<8>());
9019 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9020 Lookups.back().addDecl(DRD);
9021 PrevD = D;
9022 }
9023 }
9024 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
9025 Ty->containsUnexpandedParameterPack() ||
9026 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9027 return !D->isInvalidDecl() &&
9028 (D->getType()->isDependentType() ||
9029 D->getType()->isInstantiationDependentType() ||
9030 D->getType()->containsUnexpandedParameterPack());
9031 })) {
9032 UnresolvedSet<8> ResSet;
9033 for (auto &Set : Lookups) {
9034 ResSet.append(Set.begin(), Set.end());
9035 // The last item marks the end of all declarations at the specified scope.
9036 ResSet.addDecl(Set[Set.size() - 1]);
9037 }
9038 return UnresolvedLookupExpr::Create(
9039 SemaRef.Context, /*NamingClass=*/nullptr,
9040 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9041 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9042 }
9043 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9044 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9045 if (!D->isInvalidDecl() &&
9046 SemaRef.Context.hasSameType(D->getType(), Ty))
9047 return D;
9048 return nullptr;
9049 }))
9050 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9051 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9052 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
9053 if (!D->isInvalidDecl() &&
9054 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
9055 !Ty.isMoreQualifiedThan(D->getType()))
9056 return D;
9057 return nullptr;
9058 })) {
9059 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
9060 /*DetectVirtual=*/false);
9061 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
9062 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
9063 VD->getType().getUnqualifiedType()))) {
9064 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
9065 /*DiagID=*/0) !=
9066 Sema::AR_inaccessible) {
9067 SemaRef.BuildBasePathArray(Paths, BasePath);
9068 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9069 }
9070 }
9071 }
9072 }
9073 if (ReductionIdScopeSpec.isSet()) {
9074 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
9075 return ExprError();
9076 }
9077 return ExprEmpty();
9078}
9079
Alexey Bataevc5e02582014-06-16 07:08:35 +00009080OMPClause *Sema::ActOnOpenMPReductionClause(
9081 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9082 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009083 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
9084 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00009085 auto DN = ReductionId.getName();
9086 auto OOK = DN.getCXXOverloadedOperator();
9087 BinaryOperatorKind BOK = BO_Comma;
9088
9089 // OpenMP [2.14.3.6, reduction clause]
9090 // C
9091 // reduction-identifier is either an identifier or one of the following
9092 // operators: +, -, *, &, |, ^, && and ||
9093 // C++
9094 // reduction-identifier is either an id-expression or one of the following
9095 // operators: +, -, *, &, |, ^, && and ||
9096 // FIXME: Only 'min' and 'max' identifiers are supported for now.
9097 switch (OOK) {
9098 case OO_Plus:
9099 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009100 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009101 break;
9102 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009103 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009104 break;
9105 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009106 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009107 break;
9108 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009109 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009110 break;
9111 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009112 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009113 break;
9114 case OO_AmpAmp:
9115 BOK = BO_LAnd;
9116 break;
9117 case OO_PipePipe:
9118 BOK = BO_LOr;
9119 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009120 case OO_New:
9121 case OO_Delete:
9122 case OO_Array_New:
9123 case OO_Array_Delete:
9124 case OO_Slash:
9125 case OO_Percent:
9126 case OO_Tilde:
9127 case OO_Exclaim:
9128 case OO_Equal:
9129 case OO_Less:
9130 case OO_Greater:
9131 case OO_LessEqual:
9132 case OO_GreaterEqual:
9133 case OO_PlusEqual:
9134 case OO_MinusEqual:
9135 case OO_StarEqual:
9136 case OO_SlashEqual:
9137 case OO_PercentEqual:
9138 case OO_CaretEqual:
9139 case OO_AmpEqual:
9140 case OO_PipeEqual:
9141 case OO_LessLess:
9142 case OO_GreaterGreater:
9143 case OO_LessLessEqual:
9144 case OO_GreaterGreaterEqual:
9145 case OO_EqualEqual:
9146 case OO_ExclaimEqual:
9147 case OO_PlusPlus:
9148 case OO_MinusMinus:
9149 case OO_Comma:
9150 case OO_ArrowStar:
9151 case OO_Arrow:
9152 case OO_Call:
9153 case OO_Subscript:
9154 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009155 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009156 case NUM_OVERLOADED_OPERATORS:
9157 llvm_unreachable("Unexpected reduction identifier");
9158 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009159 if (auto II = DN.getAsIdentifierInfo()) {
9160 if (II->isStr("max"))
9161 BOK = BO_GT;
9162 else if (II->isStr("min"))
9163 BOK = BO_LT;
9164 }
9165 break;
9166 }
9167 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009168 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009169 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009170 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009171
9172 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009173 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009174 SmallVector<Expr *, 8> LHSs;
9175 SmallVector<Expr *, 8> RHSs;
9176 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009177 SmallVector<Decl *, 4> ExprCaptures;
9178 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009179 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9180 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009181 for (auto RefExpr : VarList) {
9182 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009183 // OpenMP [2.1, C/C++]
9184 // A list item is a variable or array section, subject to the restrictions
9185 // specified in Section 2.4 on page 42 and in each of the sections
9186 // describing clauses and directives for which a list appears.
9187 // OpenMP [2.14.3.3, Restrictions, p.1]
9188 // A variable that is part of another variable (as an array or
9189 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009190 if (!FirstIter && IR != ER)
9191 ++IR;
9192 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009193 SourceLocation ELoc;
9194 SourceRange ERange;
9195 Expr *SimpleRefExpr = RefExpr;
9196 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9197 /*AllowArraySection=*/true);
9198 if (Res.second) {
9199 // It will be analyzed later.
9200 Vars.push_back(RefExpr);
9201 Privates.push_back(nullptr);
9202 LHSs.push_back(nullptr);
9203 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009204 // Try to find 'declare reduction' corresponding construct before using
9205 // builtin/overloaded operators.
9206 QualType Type = Context.DependentTy;
9207 CXXCastPath BasePath;
9208 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9209 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9210 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9211 if (CurContext->isDependentContext() &&
9212 (DeclareReductionRef.isUnset() ||
9213 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9214 ReductionOps.push_back(DeclareReductionRef.get());
9215 else
9216 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009217 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009218 ValueDecl *D = Res.first;
9219 if (!D)
9220 continue;
9221
Alexey Bataeva1764212015-09-30 09:22:36 +00009222 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009223 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9224 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9225 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009226 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009227 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009228 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9229 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9230 Type = ATy->getElementType();
9231 else
9232 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009233 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009234 } else
9235 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9236 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009237
Alexey Bataevc5e02582014-06-16 07:08:35 +00009238 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9239 // A variable that appears in a private clause must not have an incomplete
9240 // type or a reference type.
9241 if (RequireCompleteType(ELoc, Type,
9242 diag::err_omp_reduction_incomplete_type))
9243 continue;
9244 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009245 // A list item that appears in a reduction clause must not be
9246 // const-qualified.
9247 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009248 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009249 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009250 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009251 bool IsDecl = !VD ||
9252 VD->isThisDeclarationADefinition(Context) ==
9253 VarDecl::DeclarationOnly;
9254 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009255 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009256 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009257 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009258 continue;
9259 }
9260 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9261 // If a list-item is a reference type then it must bind to the same object
9262 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009263 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009264 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009265 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009266 DSARefChecker Check(DSAStack);
9267 if (Check.Visit(VDDef->getInit())) {
9268 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9269 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9270 continue;
9271 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009272 }
9273 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009274
Alexey Bataevc5e02582014-06-16 07:08:35 +00009275 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9276 // in a Construct]
9277 // Variables with the predetermined data-sharing attributes may not be
9278 // listed in data-sharing attributes clauses, except for the cases
9279 // listed below. For these exceptions only, listing a predetermined
9280 // variable in a data-sharing attribute clause is allowed and overrides
9281 // the variable's predetermined data-sharing attributes.
9282 // OpenMP [2.14.3.6, Restrictions, p.3]
9283 // Any number of reduction clauses can be specified on the directive,
9284 // but a list item can appear only once in the reduction clauses for that
9285 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009286 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009287 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009288 if (DVar.CKind == OMPC_reduction) {
9289 Diag(ELoc, diag::err_omp_once_referenced)
9290 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009291 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009292 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009293 } else if (DVar.CKind != OMPC_unknown) {
9294 Diag(ELoc, diag::err_omp_wrong_dsa)
9295 << getOpenMPClauseName(DVar.CKind)
9296 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009297 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009298 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009299 }
9300
9301 // OpenMP [2.14.3.6, Restrictions, p.1]
9302 // A list item that appears in a reduction clause of a worksharing
9303 // construct must be shared in the parallel regions to which any of the
9304 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009305 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9306 if (isOpenMPWorksharingDirective(CurrDir) &&
9307 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009308 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009309 if (DVar.CKind != OMPC_shared) {
9310 Diag(ELoc, diag::err_omp_required_access)
9311 << getOpenMPClauseName(OMPC_reduction)
9312 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009313 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009314 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009315 }
9316 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009317
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009318 // Try to find 'declare reduction' corresponding construct before using
9319 // builtin/overloaded operators.
9320 CXXCastPath BasePath;
9321 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9322 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9323 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9324 if (DeclareReductionRef.isInvalid())
9325 continue;
9326 if (CurContext->isDependentContext() &&
9327 (DeclareReductionRef.isUnset() ||
9328 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9329 Vars.push_back(RefExpr);
9330 Privates.push_back(nullptr);
9331 LHSs.push_back(nullptr);
9332 RHSs.push_back(nullptr);
9333 ReductionOps.push_back(DeclareReductionRef.get());
9334 continue;
9335 }
9336 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9337 // Not allowed reduction identifier is found.
9338 Diag(ReductionId.getLocStart(),
9339 diag::err_omp_unknown_reduction_identifier)
9340 << Type << ReductionIdRange;
9341 continue;
9342 }
9343
9344 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9345 // The type of a list item that appears in a reduction clause must be valid
9346 // for the reduction-identifier. For a max or min reduction in C, the type
9347 // of the list item must be an allowed arithmetic data type: char, int,
9348 // float, double, or _Bool, possibly modified with long, short, signed, or
9349 // unsigned. For a max or min reduction in C++, the type of the list item
9350 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9351 // double, or bool, possibly modified with long, short, signed, or unsigned.
9352 if (DeclareReductionRef.isUnset()) {
9353 if ((BOK == BO_GT || BOK == BO_LT) &&
9354 !(Type->isScalarType() ||
9355 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9356 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9357 << getLangOpts().CPlusPlus;
9358 if (!ASE && !OASE) {
9359 bool IsDecl = !VD ||
9360 VD->isThisDeclarationADefinition(Context) ==
9361 VarDecl::DeclarationOnly;
9362 Diag(D->getLocation(),
9363 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9364 << D;
9365 }
9366 continue;
9367 }
9368 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9369 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9370 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9371 if (!ASE && !OASE) {
9372 bool IsDecl = !VD ||
9373 VD->isThisDeclarationADefinition(Context) ==
9374 VarDecl::DeclarationOnly;
9375 Diag(D->getLocation(),
9376 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9377 << D;
9378 }
9379 continue;
9380 }
9381 }
9382
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009383 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009384 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009385 D->hasAttrs() ? &D->getAttrs() : nullptr);
9386 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9387 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009388 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009389 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009390 (!ASE &&
9391 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009392 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009393 // Create pseudo array type for private copy. The size for this array will
9394 // be generated during codegen.
9395 // For array subscripts or single variables Private Ty is the same as Type
9396 // (type of the variable or single array element).
9397 PrivateTy = Context.getVariableArrayType(
9398 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9399 Context.getSizeType(), VK_RValue),
9400 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009401 } else if (!ASE && !OASE &&
9402 Context.getAsArrayType(D->getType().getNonReferenceType()))
9403 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009404 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009405 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9406 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009407 // Add initializer for private variable.
9408 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009409 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9410 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9411 if (DeclareReductionRef.isUsable()) {
9412 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9413 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9414 if (DRD->getInitializer()) {
9415 Init = DRDRef;
9416 RHSVD->setInit(DRDRef);
9417 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009418 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009419 } else {
9420 switch (BOK) {
9421 case BO_Add:
9422 case BO_Xor:
9423 case BO_Or:
9424 case BO_LOr:
9425 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9426 if (Type->isScalarType() || Type->isAnyComplexType())
9427 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9428 break;
9429 case BO_Mul:
9430 case BO_LAnd:
9431 if (Type->isScalarType() || Type->isAnyComplexType()) {
9432 // '*' and '&&' reduction ops - initializer is '1'.
9433 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009434 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009435 break;
9436 case BO_And: {
9437 // '&' reduction op - initializer is '~0'.
9438 QualType OrigType = Type;
9439 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9440 Type = ComplexTy->getElementType();
9441 if (Type->isRealFloatingType()) {
9442 llvm::APFloat InitValue =
9443 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9444 /*isIEEE=*/true);
9445 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9446 Type, ELoc);
9447 } else if (Type->isScalarType()) {
9448 auto Size = Context.getTypeSize(Type);
9449 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9450 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9451 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9452 }
9453 if (Init && OrigType->isAnyComplexType()) {
9454 // Init = 0xFFFF + 0xFFFFi;
9455 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9456 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9457 }
9458 Type = OrigType;
9459 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009460 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009461 case BO_LT:
9462 case BO_GT: {
9463 // 'min' reduction op - initializer is 'Largest representable number in
9464 // the reduction list item type'.
9465 // 'max' reduction op - initializer is 'Least representable number in
9466 // the reduction list item type'.
9467 if (Type->isIntegerType() || Type->isPointerType()) {
9468 bool IsSigned = Type->hasSignedIntegerRepresentation();
9469 auto Size = Context.getTypeSize(Type);
9470 QualType IntTy =
9471 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9472 llvm::APInt InitValue =
9473 (BOK != BO_LT)
9474 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9475 : llvm::APInt::getMinValue(Size)
9476 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9477 : llvm::APInt::getMaxValue(Size);
9478 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9479 if (Type->isPointerType()) {
9480 // Cast to pointer type.
9481 auto CastExpr = BuildCStyleCastExpr(
9482 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9483 SourceLocation(), Init);
9484 if (CastExpr.isInvalid())
9485 continue;
9486 Init = CastExpr.get();
9487 }
9488 } else if (Type->isRealFloatingType()) {
9489 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9490 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9491 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9492 Type, ELoc);
9493 }
9494 break;
9495 }
9496 case BO_PtrMemD:
9497 case BO_PtrMemI:
9498 case BO_MulAssign:
9499 case BO_Div:
9500 case BO_Rem:
9501 case BO_Sub:
9502 case BO_Shl:
9503 case BO_Shr:
9504 case BO_LE:
9505 case BO_GE:
9506 case BO_EQ:
9507 case BO_NE:
9508 case BO_AndAssign:
9509 case BO_XorAssign:
9510 case BO_OrAssign:
9511 case BO_Assign:
9512 case BO_AddAssign:
9513 case BO_SubAssign:
9514 case BO_DivAssign:
9515 case BO_RemAssign:
9516 case BO_ShlAssign:
9517 case BO_ShrAssign:
9518 case BO_Comma:
9519 llvm_unreachable("Unexpected reduction operation");
9520 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009521 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009522 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009523 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9524 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009525 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009526 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009527 if (RHSVD->isInvalidDecl())
9528 continue;
9529 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009530 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9531 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009532 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;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009538 continue;
9539 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009540 // Store initializer for single element in private copy. Will be used during
9541 // codegen.
9542 PrivateVD->setInit(RHSVD->getInit());
9543 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009544 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009545 ExprResult ReductionOp;
9546 if (DeclareReductionRef.isUsable()) {
9547 QualType RedTy = DeclareReductionRef.get()->getType();
9548 QualType PtrRedTy = Context.getPointerType(RedTy);
9549 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9550 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9551 if (!BasePath.empty()) {
9552 LHS = DefaultLvalueConversion(LHS.get());
9553 RHS = DefaultLvalueConversion(RHS.get());
9554 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9555 CK_UncheckedDerivedToBase, LHS.get(),
9556 &BasePath, LHS.get()->getValueKind());
9557 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9558 CK_UncheckedDerivedToBase, RHS.get(),
9559 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009560 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009561 FunctionProtoType::ExtProtoInfo EPI;
9562 QualType Params[] = {PtrRedTy, PtrRedTy};
9563 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9564 auto *OVE = new (Context) OpaqueValueExpr(
9565 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9566 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9567 Expr *Args[] = {LHS.get(), RHS.get()};
9568 ReductionOp = new (Context)
9569 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9570 } else {
9571 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9572 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9573 if (ReductionOp.isUsable()) {
9574 if (BOK != BO_LT && BOK != BO_GT) {
9575 ReductionOp =
9576 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9577 BO_Assign, LHSDRE, ReductionOp.get());
9578 } else {
9579 auto *ConditionalOp = new (Context) ConditionalOperator(
9580 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9581 RHSDRE, Type, VK_LValue, OK_Ordinary);
9582 ReductionOp =
9583 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9584 BO_Assign, LHSDRE, ConditionalOp);
9585 }
9586 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9587 }
9588 if (ReductionOp.isInvalid())
9589 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009590 }
9591
Alexey Bataev60da77e2016-02-29 05:54:20 +00009592 DeclRefExpr *Ref = nullptr;
9593 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009594 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009595 if (ASE || OASE) {
9596 TransformExprToCaptures RebuildToCapture(*this, D);
9597 VarsExpr =
9598 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9599 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009600 } else {
9601 VarsExpr = Ref =
9602 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009603 }
9604 if (!IsOpenMPCapturedDecl(D)) {
9605 ExprCaptures.push_back(Ref->getDecl());
9606 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9607 ExprResult RefRes = DefaultLvalueConversion(Ref);
9608 if (!RefRes.isUsable())
9609 continue;
9610 ExprResult PostUpdateRes =
9611 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9612 SimpleRefExpr, RefRes.get());
9613 if (!PostUpdateRes.isUsable())
9614 continue;
9615 ExprPostUpdates.push_back(
9616 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009617 }
9618 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009619 }
9620 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9621 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009622 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009623 LHSs.push_back(LHSDRE);
9624 RHSs.push_back(RHSDRE);
9625 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009626 }
9627
9628 if (Vars.empty())
9629 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009630
Alexey Bataevc5e02582014-06-16 07:08:35 +00009631 return OMPReductionClause::Create(
9632 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009633 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009634 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9635 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009636}
9637
Alexey Bataevecba70f2016-04-12 11:02:11 +00009638bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9639 SourceLocation LinLoc) {
9640 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9641 LinKind == OMPC_LINEAR_unknown) {
9642 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9643 return true;
9644 }
9645 return false;
9646}
9647
9648bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9649 OpenMPLinearClauseKind LinKind,
9650 QualType Type) {
9651 auto *VD = dyn_cast_or_null<VarDecl>(D);
9652 // A variable must not have an incomplete type or a reference type.
9653 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9654 return true;
9655 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9656 !Type->isReferenceType()) {
9657 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9658 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9659 return true;
9660 }
9661 Type = Type.getNonReferenceType();
9662
9663 // A list item must not be const-qualified.
9664 if (Type.isConstant(Context)) {
9665 Diag(ELoc, diag::err_omp_const_variable)
9666 << getOpenMPClauseName(OMPC_linear);
9667 if (D) {
9668 bool IsDecl =
9669 !VD ||
9670 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9671 Diag(D->getLocation(),
9672 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9673 << D;
9674 }
9675 return true;
9676 }
9677
9678 // A list item must be of integral or pointer type.
9679 Type = Type.getUnqualifiedType().getCanonicalType();
9680 const auto *Ty = Type.getTypePtrOrNull();
9681 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9682 !Ty->isPointerType())) {
9683 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9684 if (D) {
9685 bool IsDecl =
9686 !VD ||
9687 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9688 Diag(D->getLocation(),
9689 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9690 << D;
9691 }
9692 return true;
9693 }
9694 return false;
9695}
9696
Alexey Bataev182227b2015-08-20 10:54:39 +00009697OMPClause *Sema::ActOnOpenMPLinearClause(
9698 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9699 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9700 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009701 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009702 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009703 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009704 SmallVector<Decl *, 4> ExprCaptures;
9705 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009706 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009707 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009708 for (auto &RefExpr : VarList) {
9709 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009710 SourceLocation ELoc;
9711 SourceRange ERange;
9712 Expr *SimpleRefExpr = RefExpr;
9713 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9714 /*AllowArraySection=*/false);
9715 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009716 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009717 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009718 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009719 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009720 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009721 ValueDecl *D = Res.first;
9722 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009723 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009724
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009725 QualType Type = D->getType();
9726 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009727
9728 // OpenMP [2.14.3.7, linear clause]
9729 // A list-item cannot appear in more than one linear clause.
9730 // A list-item that appears in a linear clause cannot appear in any
9731 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009732 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009733 if (DVar.RefExpr) {
9734 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9735 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009736 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009737 continue;
9738 }
9739
Alexey Bataevecba70f2016-04-12 11:02:11 +00009740 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009741 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009742 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009743
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009744 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009745 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9746 D->hasAttrs() ? &D->getAttrs() : nullptr);
9747 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009748 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009749 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009750 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009751 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009752 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009753 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9754 if (!IsOpenMPCapturedDecl(D)) {
9755 ExprCaptures.push_back(Ref->getDecl());
9756 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9757 ExprResult RefRes = DefaultLvalueConversion(Ref);
9758 if (!RefRes.isUsable())
9759 continue;
9760 ExprResult PostUpdateRes =
9761 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9762 SimpleRefExpr, RefRes.get());
9763 if (!PostUpdateRes.isUsable())
9764 continue;
9765 ExprPostUpdates.push_back(
9766 IgnoredValueConversions(PostUpdateRes.get()).get());
9767 }
9768 }
9769 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009770 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009771 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009772 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009773 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009774 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009775 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9776 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9777
9778 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009779 Vars.push_back((VD || CurContext->isDependentContext())
9780 ? RefExpr->IgnoreParens()
9781 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009782 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009783 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009784 }
9785
9786 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009787 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009788
9789 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009790 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009791 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9792 !Step->isInstantiationDependent() &&
9793 !Step->containsUnexpandedParameterPack()) {
9794 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009795 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009796 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009797 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009798 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009799
Alexander Musman3276a272015-03-21 10:12:56 +00009800 // Build var to save the step value.
9801 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009802 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009803 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009804 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009805 ExprResult CalcStep =
9806 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009807 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009808
Alexander Musman8dba6642014-04-22 13:09:42 +00009809 // Warn about zero linear step (it would be probably better specified as
9810 // making corresponding variables 'const').
9811 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009812 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9813 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009814 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9815 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009816 if (!IsConstant && CalcStep.isUsable()) {
9817 // Calculate the step beforehand instead of doing this on each iteration.
9818 // (This is not used if the number of iterations may be kfold-ed).
9819 CalcStepExpr = CalcStep.get();
9820 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009821 }
9822
Alexey Bataev182227b2015-08-20 10:54:39 +00009823 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9824 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009825 StepExpr, CalcStepExpr,
9826 buildPreInits(Context, ExprCaptures),
9827 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009828}
9829
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009830static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9831 Expr *NumIterations, Sema &SemaRef,
9832 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009833 // Walk the vars and build update/final expressions for the CodeGen.
9834 SmallVector<Expr *, 8> Updates;
9835 SmallVector<Expr *, 8> Finals;
9836 Expr *Step = Clause.getStep();
9837 Expr *CalcStep = Clause.getCalcStep();
9838 // OpenMP [2.14.3.7, linear clause]
9839 // If linear-step is not specified it is assumed to be 1.
9840 if (Step == nullptr)
9841 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009842 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009843 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009844 }
Alexander Musman3276a272015-03-21 10:12:56 +00009845 bool HasErrors = false;
9846 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009847 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009848 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009849 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009850 SourceLocation ELoc;
9851 SourceRange ERange;
9852 Expr *SimpleRefExpr = RefExpr;
9853 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9854 /*AllowArraySection=*/false);
9855 ValueDecl *D = Res.first;
9856 if (Res.second || !D) {
9857 Updates.push_back(nullptr);
9858 Finals.push_back(nullptr);
9859 HasErrors = true;
9860 continue;
9861 }
9862 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9863 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9864 ->getMemberDecl();
9865 }
9866 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009867 Expr *InitExpr = *CurInit;
9868
9869 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009870 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009871 Expr *CapturedRef;
9872 if (LinKind == OMPC_LINEAR_uval)
9873 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9874 else
9875 CapturedRef =
9876 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9877 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9878 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009879
9880 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009881 ExprResult Update;
9882 if (!Info.first) {
9883 Update =
9884 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9885 InitExpr, IV, Step, /* Subtract */ false);
9886 } else
9887 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009888 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9889 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009890
9891 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009892 ExprResult Final;
9893 if (!Info.first) {
9894 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9895 InitExpr, NumIterations, Step,
9896 /* Subtract */ false);
9897 } else
9898 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009899 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9900 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009901
Alexander Musman3276a272015-03-21 10:12:56 +00009902 if (!Update.isUsable() || !Final.isUsable()) {
9903 Updates.push_back(nullptr);
9904 Finals.push_back(nullptr);
9905 HasErrors = true;
9906 } else {
9907 Updates.push_back(Update.get());
9908 Finals.push_back(Final.get());
9909 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009910 ++CurInit;
9911 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009912 }
9913 Clause.setUpdates(Updates);
9914 Clause.setFinals(Finals);
9915 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009916}
9917
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009918OMPClause *Sema::ActOnOpenMPAlignedClause(
9919 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9920 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9921
9922 SmallVector<Expr *, 8> Vars;
9923 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009924 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9925 SourceLocation ELoc;
9926 SourceRange ERange;
9927 Expr *SimpleRefExpr = RefExpr;
9928 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9929 /*AllowArraySection=*/false);
9930 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009931 // It will be analyzed later.
9932 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009933 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009934 ValueDecl *D = Res.first;
9935 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009936 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009937
Alexey Bataev1efd1662016-03-29 10:59:56 +00009938 QualType QType = D->getType();
9939 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009940
9941 // OpenMP [2.8.1, simd construct, Restrictions]
9942 // The type of list items appearing in the aligned clause must be
9943 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009944 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009945 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009946 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009947 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009948 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009949 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009950 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009951 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009952 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009953 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009954 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009955 continue;
9956 }
9957
9958 // OpenMP [2.8.1, simd construct, Restrictions]
9959 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009960 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009961 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009962 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9963 << getOpenMPClauseName(OMPC_aligned);
9964 continue;
9965 }
9966
Alexey Bataev1efd1662016-03-29 10:59:56 +00009967 DeclRefExpr *Ref = nullptr;
9968 if (!VD && IsOpenMPCapturedDecl(D))
9969 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9970 Vars.push_back(DefaultFunctionArrayConversion(
9971 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9972 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009973 }
9974
9975 // OpenMP [2.8.1, simd construct, Description]
9976 // The parameter of the aligned clause, alignment, must be a constant
9977 // positive integer expression.
9978 // If no optional parameter is specified, implementation-defined default
9979 // alignments for SIMD instructions on the target platforms are assumed.
9980 if (Alignment != nullptr) {
9981 ExprResult AlignResult =
9982 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9983 if (AlignResult.isInvalid())
9984 return nullptr;
9985 Alignment = AlignResult.get();
9986 }
9987 if (Vars.empty())
9988 return nullptr;
9989
9990 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9991 EndLoc, Vars, Alignment);
9992}
9993
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009994OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9995 SourceLocation StartLoc,
9996 SourceLocation LParenLoc,
9997 SourceLocation EndLoc) {
9998 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009999 SmallVector<Expr *, 8> SrcExprs;
10000 SmallVector<Expr *, 8> DstExprs;
10001 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +000010002 for (auto &RefExpr : VarList) {
10003 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
10004 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010005 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010006 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010007 SrcExprs.push_back(nullptr);
10008 DstExprs.push_back(nullptr);
10009 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010010 continue;
10011 }
10012
Alexey Bataeved09d242014-05-28 05:53:51 +000010013 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010014 // OpenMP [2.1, C/C++]
10015 // A list item is a variable name.
10016 // OpenMP [2.14.4.1, Restrictions, p.1]
10017 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +000010018 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010019 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010020 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
10021 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010022 continue;
10023 }
10024
10025 Decl *D = DE->getDecl();
10026 VarDecl *VD = cast<VarDecl>(D);
10027
10028 QualType Type = VD->getType();
10029 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
10030 // It will be analyzed later.
10031 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010032 SrcExprs.push_back(nullptr);
10033 DstExprs.push_back(nullptr);
10034 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010035 continue;
10036 }
10037
10038 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
10039 // A list item that appears in a copyin clause must be threadprivate.
10040 if (!DSAStack->isThreadPrivate(VD)) {
10041 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000010042 << getOpenMPClauseName(OMPC_copyin)
10043 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010044 continue;
10045 }
10046
10047 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10048 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000010049 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010050 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010051 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010052 auto *SrcVD =
10053 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
10054 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +000010055 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010056 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
10057 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010058 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
10059 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010060 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010061 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010062 // For arrays generate assignment operation for single element and replace
10063 // it by the original array element in CodeGen.
10064 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
10065 PseudoDstExpr, PseudoSrcExpr);
10066 if (AssignmentOp.isInvalid())
10067 continue;
10068 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
10069 /*DiscardedValue=*/true);
10070 if (AssignmentOp.isInvalid())
10071 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010072
10073 DSAStack->addDSA(VD, DE, OMPC_copyin);
10074 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010075 SrcExprs.push_back(PseudoSrcExpr);
10076 DstExprs.push_back(PseudoDstExpr);
10077 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010078 }
10079
Alexey Bataeved09d242014-05-28 05:53:51 +000010080 if (Vars.empty())
10081 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010082
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010083 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10084 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010085}
10086
Alexey Bataevbae9a792014-06-27 10:37:06 +000010087OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
10088 SourceLocation StartLoc,
10089 SourceLocation LParenLoc,
10090 SourceLocation EndLoc) {
10091 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000010092 SmallVector<Expr *, 8> SrcExprs;
10093 SmallVector<Expr *, 8> DstExprs;
10094 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010095 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010096 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10097 SourceLocation ELoc;
10098 SourceRange ERange;
10099 Expr *SimpleRefExpr = RefExpr;
10100 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10101 /*AllowArraySection=*/false);
10102 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010103 // It will be analyzed later.
10104 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010105 SrcExprs.push_back(nullptr);
10106 DstExprs.push_back(nullptr);
10107 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010108 }
Alexey Bataeve122da12016-03-17 10:50:17 +000010109 ValueDecl *D = Res.first;
10110 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000010111 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010112
Alexey Bataeve122da12016-03-17 10:50:17 +000010113 QualType Type = D->getType();
10114 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010115
10116 // OpenMP [2.14.4.2, Restrictions, p.2]
10117 // A list item that appears in a copyprivate clause may not appear in a
10118 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000010119 if (!VD || !DSAStack->isThreadPrivate(VD)) {
10120 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010121 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10122 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010123 Diag(ELoc, diag::err_omp_wrong_dsa)
10124 << getOpenMPClauseName(DVar.CKind)
10125 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +000010126 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010127 continue;
10128 }
10129
10130 // OpenMP [2.11.4.2, Restrictions, p.1]
10131 // All list items that appear in a copyprivate clause must be either
10132 // threadprivate or private in the enclosing context.
10133 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010134 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010135 if (DVar.CKind == OMPC_shared) {
10136 Diag(ELoc, diag::err_omp_required_access)
10137 << getOpenMPClauseName(OMPC_copyprivate)
10138 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000010139 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010140 continue;
10141 }
10142 }
10143 }
10144
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010145 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010146 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010147 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010148 << getOpenMPClauseName(OMPC_copyprivate) << Type
10149 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010150 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000010151 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010152 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010153 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010154 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010155 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010156 continue;
10157 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010158
Alexey Bataevbae9a792014-06-27 10:37:06 +000010159 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10160 // A variable of class type (or array thereof) that appears in a
10161 // copyin clause requires an accessible, unambiguous copy assignment
10162 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010163 Type = Context.getBaseElementType(Type.getNonReferenceType())
10164 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010165 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010166 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10167 D->hasAttrs() ? &D->getAttrs() : nullptr);
10168 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010169 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010170 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10171 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010172 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010173 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10174 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010175 PseudoDstExpr, PseudoSrcExpr);
10176 if (AssignmentOp.isInvalid())
10177 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010178 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010179 /*DiscardedValue=*/true);
10180 if (AssignmentOp.isInvalid())
10181 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010182
10183 // No need to mark vars as copyprivate, they are already threadprivate or
10184 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010185 assert(VD || IsOpenMPCapturedDecl(D));
10186 Vars.push_back(
10187 VD ? RefExpr->IgnoreParens()
10188 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010189 SrcExprs.push_back(PseudoSrcExpr);
10190 DstExprs.push_back(PseudoDstExpr);
10191 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010192 }
10193
10194 if (Vars.empty())
10195 return nullptr;
10196
Alexey Bataeva63048e2015-03-23 06:18:07 +000010197 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10198 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010199}
10200
Alexey Bataev6125da92014-07-21 11:26:11 +000010201OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10202 SourceLocation StartLoc,
10203 SourceLocation LParenLoc,
10204 SourceLocation EndLoc) {
10205 if (VarList.empty())
10206 return nullptr;
10207
10208 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10209}
Alexey Bataevdea47612014-07-23 07:46:59 +000010210
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010211OMPClause *
10212Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10213 SourceLocation DepLoc, SourceLocation ColonLoc,
10214 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10215 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010216 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010217 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010218 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010219 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010220 return nullptr;
10221 }
10222 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010223 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10224 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010225 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010226 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010227 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10228 /*Last=*/OMPC_DEPEND_unknown, Except)
10229 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010230 return nullptr;
10231 }
10232 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010233 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010234 llvm::APSInt DepCounter(/*BitWidth=*/32);
10235 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10236 if (DepKind == OMPC_DEPEND_sink) {
10237 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10238 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10239 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010240 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010241 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010242 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10243 DSAStack->getParentOrderedRegionParam()) {
10244 for (auto &RefExpr : VarList) {
10245 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010246 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010247 // It will be analyzed later.
10248 Vars.push_back(RefExpr);
10249 continue;
10250 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010251
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010252 SourceLocation ELoc = RefExpr->getExprLoc();
10253 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10254 if (DepKind == OMPC_DEPEND_sink) {
10255 if (DepCounter >= TotalDepCount) {
10256 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10257 continue;
10258 }
10259 ++DepCounter;
10260 // OpenMP [2.13.9, Summary]
10261 // depend(dependence-type : vec), where dependence-type is:
10262 // 'sink' and where vec is the iteration vector, which has the form:
10263 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10264 // where n is the value specified by the ordered clause in the loop
10265 // directive, xi denotes the loop iteration variable of the i-th nested
10266 // loop associated with the loop directive, and di is a constant
10267 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010268 if (CurContext->isDependentContext()) {
10269 // It will be analyzed later.
10270 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010271 continue;
10272 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010273 SimpleExpr = SimpleExpr->IgnoreImplicit();
10274 OverloadedOperatorKind OOK = OO_None;
10275 SourceLocation OOLoc;
10276 Expr *LHS = SimpleExpr;
10277 Expr *RHS = nullptr;
10278 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10279 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10280 OOLoc = BO->getOperatorLoc();
10281 LHS = BO->getLHS()->IgnoreParenImpCasts();
10282 RHS = BO->getRHS()->IgnoreParenImpCasts();
10283 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10284 OOK = OCE->getOperator();
10285 OOLoc = OCE->getOperatorLoc();
10286 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10287 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10288 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10289 OOK = MCE->getMethodDecl()
10290 ->getNameInfo()
10291 .getName()
10292 .getCXXOverloadedOperator();
10293 OOLoc = MCE->getCallee()->getExprLoc();
10294 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10295 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10296 }
10297 SourceLocation ELoc;
10298 SourceRange ERange;
10299 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10300 /*AllowArraySection=*/false);
10301 if (Res.second) {
10302 // It will be analyzed later.
10303 Vars.push_back(RefExpr);
10304 }
10305 ValueDecl *D = Res.first;
10306 if (!D)
10307 continue;
10308
10309 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10310 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10311 continue;
10312 }
10313 if (RHS) {
10314 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10315 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10316 if (RHSRes.isInvalid())
10317 continue;
10318 }
10319 if (!CurContext->isDependentContext() &&
10320 DSAStack->getParentOrderedRegionParam() &&
10321 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10322 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10323 << DSAStack->getParentLoopControlVariable(
10324 DepCounter.getZExtValue());
10325 continue;
10326 }
10327 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010328 } else {
10329 // OpenMP [2.11.1.1, Restrictions, p.3]
10330 // A variable that is part of another variable (such as a field of a
10331 // structure) but is not an array element or an array section cannot
10332 // appear in a depend clause.
10333 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10334 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10335 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10336 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10337 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010338 (ASE &&
10339 !ASE->getBase()
10340 ->getType()
10341 .getNonReferenceType()
10342 ->isPointerType() &&
10343 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010344 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10345 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010346 continue;
10347 }
10348 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010349 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10350 }
10351
10352 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10353 TotalDepCount > VarList.size() &&
10354 DSAStack->getParentOrderedRegionParam()) {
10355 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10356 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10357 }
10358 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10359 Vars.empty())
10360 return nullptr;
10361 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010362 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10363 DepKind, DepLoc, ColonLoc, Vars);
10364 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10365 DSAStack->addDoacrossDependClause(C, OpsOffs);
10366 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010367}
Michael Wonge710d542015-08-07 16:16:36 +000010368
10369OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10370 SourceLocation LParenLoc,
10371 SourceLocation EndLoc) {
10372 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010373
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010374 // OpenMP [2.9.1, Restrictions]
10375 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010376 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10377 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010378 return nullptr;
10379
Michael Wonge710d542015-08-07 16:16:36 +000010380 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10381}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010382
10383static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10384 DSAStackTy *Stack, CXXRecordDecl *RD) {
10385 if (!RD || RD->isInvalidDecl())
10386 return true;
10387
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010388 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10389 if (auto *CTD = CTSD->getSpecializedTemplate())
10390 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010391 auto QTy = SemaRef.Context.getRecordType(RD);
10392 if (RD->isDynamicClass()) {
10393 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10394 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10395 return false;
10396 }
10397 auto *DC = RD;
10398 bool IsCorrect = true;
10399 for (auto *I : DC->decls()) {
10400 if (I) {
10401 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10402 if (MD->isStatic()) {
10403 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10404 SemaRef.Diag(MD->getLocation(),
10405 diag::note_omp_static_member_in_target);
10406 IsCorrect = false;
10407 }
10408 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10409 if (VD->isStaticDataMember()) {
10410 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10411 SemaRef.Diag(VD->getLocation(),
10412 diag::note_omp_static_member_in_target);
10413 IsCorrect = false;
10414 }
10415 }
10416 }
10417 }
10418
10419 for (auto &I : RD->bases()) {
10420 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10421 I.getType()->getAsCXXRecordDecl()))
10422 IsCorrect = false;
10423 }
10424 return IsCorrect;
10425}
10426
10427static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10428 DSAStackTy *Stack, QualType QTy) {
10429 NamedDecl *ND;
10430 if (QTy->isIncompleteType(&ND)) {
10431 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10432 return false;
10433 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10434 if (!RD->isInvalidDecl() &&
10435 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10436 return false;
10437 }
10438 return true;
10439}
10440
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010441/// \brief Return true if it can be proven that the provided array expression
10442/// (array section or array subscript) does NOT specify the whole size of the
10443/// array whose base type is \a BaseQTy.
10444static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10445 const Expr *E,
10446 QualType BaseQTy) {
10447 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10448
10449 // If this is an array subscript, it refers to the whole size if the size of
10450 // the dimension is constant and equals 1. Also, an array section assumes the
10451 // format of an array subscript if no colon is used.
10452 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10453 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10454 return ATy->getSize().getSExtValue() != 1;
10455 // Size can't be evaluated statically.
10456 return false;
10457 }
10458
10459 assert(OASE && "Expecting array section if not an array subscript.");
10460 auto *LowerBound = OASE->getLowerBound();
10461 auto *Length = OASE->getLength();
10462
10463 // If there is a lower bound that does not evaluates to zero, we are not
10464 // convering the whole dimension.
10465 if (LowerBound) {
10466 llvm::APSInt ConstLowerBound;
10467 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10468 return false; // Can't get the integer value as a constant.
10469 if (ConstLowerBound.getSExtValue())
10470 return true;
10471 }
10472
10473 // If we don't have a length we covering the whole dimension.
10474 if (!Length)
10475 return false;
10476
10477 // If the base is a pointer, we don't have a way to get the size of the
10478 // pointee.
10479 if (BaseQTy->isPointerType())
10480 return false;
10481
10482 // We can only check if the length is the same as the size of the dimension
10483 // if we have a constant array.
10484 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10485 if (!CATy)
10486 return false;
10487
10488 llvm::APSInt ConstLength;
10489 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10490 return false; // Can't get the integer value as a constant.
10491
10492 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10493}
10494
10495// Return true if it can be proven that the provided array expression (array
10496// section or array subscript) does NOT specify a single element of the array
10497// whose base type is \a BaseQTy.
10498static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10499 const Expr *E,
10500 QualType BaseQTy) {
10501 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10502
10503 // An array subscript always refer to a single element. Also, an array section
10504 // assumes the format of an array subscript if no colon is used.
10505 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10506 return false;
10507
10508 assert(OASE && "Expecting array section if not an array subscript.");
10509 auto *Length = OASE->getLength();
10510
10511 // If we don't have a length we have to check if the array has unitary size
10512 // for this dimension. Also, we should always expect a length if the base type
10513 // is pointer.
10514 if (!Length) {
10515 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10516 return ATy->getSize().getSExtValue() != 1;
10517 // We cannot assume anything.
10518 return false;
10519 }
10520
10521 // Check if the length evaluates to 1.
10522 llvm::APSInt ConstLength;
10523 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10524 return false; // Can't get the integer value as a constant.
10525
10526 return ConstLength.getSExtValue() != 1;
10527}
10528
Samuel Antao661c0902016-05-26 17:39:58 +000010529// Return the expression of the base of the mappable expression or null if it
10530// cannot be determined and do all the necessary checks to see if the expression
10531// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010532// components of the expression.
10533static Expr *CheckMapClauseExpressionBase(
10534 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010535 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10536 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010537 SourceLocation ELoc = E->getExprLoc();
10538 SourceRange ERange = E->getSourceRange();
10539
10540 // The base of elements of list in a map clause have to be either:
10541 // - a reference to variable or field.
10542 // - a member expression.
10543 // - an array expression.
10544 //
10545 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10546 // reference to 'r'.
10547 //
10548 // If we have:
10549 //
10550 // struct SS {
10551 // Bla S;
10552 // foo() {
10553 // #pragma omp target map (S.Arr[:12]);
10554 // }
10555 // }
10556 //
10557 // We want to retrieve the member expression 'this->S';
10558
10559 Expr *RelevantExpr = nullptr;
10560
Samuel Antao5de996e2016-01-22 20:21:36 +000010561 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10562 // If a list item is an array section, it must specify contiguous storage.
10563 //
10564 // For this restriction it is sufficient that we make sure only references
10565 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010566 // exist except in the rightmost expression (unless they cover the whole
10567 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010568 //
10569 // r.ArrS[3:5].Arr[6:7]
10570 //
10571 // r.ArrS[3:5].x
10572 //
10573 // but these would be valid:
10574 // r.ArrS[3].Arr[6:7]
10575 //
10576 // r.ArrS[3].x
10577
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010578 bool AllowUnitySizeArraySection = true;
10579 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010580
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010581 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010582 E = E->IgnoreParenImpCasts();
10583
10584 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10585 if (!isa<VarDecl>(CurE->getDecl()))
10586 break;
10587
10588 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010589
10590 // If we got a reference to a declaration, we should not expect any array
10591 // section before that.
10592 AllowUnitySizeArraySection = false;
10593 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010594
10595 // Record the component.
10596 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10597 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010598 continue;
10599 }
10600
10601 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10602 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10603
10604 if (isa<CXXThisExpr>(BaseE))
10605 // We found a base expression: this->Val.
10606 RelevantExpr = CurE;
10607 else
10608 E = BaseE;
10609
10610 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10611 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10612 << CurE->getSourceRange();
10613 break;
10614 }
10615
10616 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10617
10618 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10619 // A bit-field cannot appear in a map clause.
10620 //
10621 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010622 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10623 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010624 break;
10625 }
10626
10627 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10628 // If the type of a list item is a reference to a type T then the type
10629 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010630 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010631
10632 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10633 // A list item cannot be a variable that is a member of a structure with
10634 // a union type.
10635 //
10636 if (auto *RT = CurType->getAs<RecordType>())
10637 if (RT->isUnionType()) {
10638 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10639 << CurE->getSourceRange();
10640 break;
10641 }
10642
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010643 // If we got a member expression, we should not expect any array section
10644 // before that:
10645 //
10646 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10647 // If a list item is an element of a structure, only the rightmost symbol
10648 // of the variable reference can be an array section.
10649 //
10650 AllowUnitySizeArraySection = false;
10651 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010652
10653 // Record the component.
10654 CurComponents.push_back(
10655 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010656 continue;
10657 }
10658
10659 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10660 E = CurE->getBase()->IgnoreParenImpCasts();
10661
10662 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10663 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10664 << 0 << CurE->getSourceRange();
10665 break;
10666 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010667
10668 // If we got an array subscript that express the whole dimension we
10669 // can have any array expressions before. If it only expressing part of
10670 // the dimension, we can only have unitary-size array expressions.
10671 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10672 E->getType()))
10673 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010674
10675 // Record the component - we don't have any declaration associated.
10676 CurComponents.push_back(
10677 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010678 continue;
10679 }
10680
10681 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010682 E = CurE->getBase()->IgnoreParenImpCasts();
10683
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010684 auto CurType =
10685 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10686
Samuel Antao5de996e2016-01-22 20:21:36 +000010687 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10688 // If the type of a list item is a reference to a type T then the type
10689 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010690 if (CurType->isReferenceType())
10691 CurType = CurType->getPointeeType();
10692
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010693 bool IsPointer = CurType->isAnyPointerType();
10694
10695 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010696 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10697 << 0 << CurE->getSourceRange();
10698 break;
10699 }
10700
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010701 bool NotWhole =
10702 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10703 bool NotUnity =
10704 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10705
Samuel Antaodab51bb2016-07-18 23:22:11 +000010706 if (AllowWholeSizeArraySection) {
10707 // Any array section is currently allowed. Allowing a whole size array
10708 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010709 //
10710 // If this array section refers to the whole dimension we can still
10711 // accept other array sections before this one, except if the base is a
10712 // pointer. Otherwise, only unitary sections are accepted.
10713 if (NotWhole || IsPointer)
10714 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000010715 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010716 // A unity or whole array section is not allowed and that is not
10717 // compatible with the properties of the current array section.
10718 SemaRef.Diag(
10719 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10720 << CurE->getSourceRange();
10721 break;
10722 }
Samuel Antao90927002016-04-26 14:54:23 +000010723
10724 // Record the component - we don't have any declaration associated.
10725 CurComponents.push_back(
10726 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010727 continue;
10728 }
10729
10730 // If nothing else worked, this is not a valid map clause expression.
10731 SemaRef.Diag(ELoc,
10732 diag::err_omp_expected_named_var_member_or_array_expression)
10733 << ERange;
10734 break;
10735 }
10736
10737 return RelevantExpr;
10738}
10739
10740// Return true if expression E associated with value VD has conflicts with other
10741// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010742static bool CheckMapConflicts(
10743 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10744 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010745 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10746 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010747 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010748 SourceLocation ELoc = E->getExprLoc();
10749 SourceRange ERange = E->getSourceRange();
10750
10751 // In order to easily check the conflicts we need to match each component of
10752 // the expression under test with the components of the expressions that are
10753 // already in the stack.
10754
Samuel Antao5de996e2016-01-22 20:21:36 +000010755 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010756 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010757 "Map clause expression with unexpected base!");
10758
10759 // Variables to help detecting enclosing problems in data environment nests.
10760 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010761 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010762
Samuel Antao90927002016-04-26 14:54:23 +000010763 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10764 VD, CurrentRegionOnly,
10765 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10766 StackComponents) -> bool {
10767
Samuel Antao5de996e2016-01-22 20:21:36 +000010768 assert(!StackComponents.empty() &&
10769 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010770 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010771 "Map clause expression with unexpected base!");
10772
Samuel Antao90927002016-04-26 14:54:23 +000010773 // The whole expression in the stack.
10774 auto *RE = StackComponents.front().getAssociatedExpression();
10775
Samuel Antao5de996e2016-01-22 20:21:36 +000010776 // Expressions must start from the same base. Here we detect at which
10777 // point both expressions diverge from each other and see if we can
10778 // detect if the memory referred to both expressions is contiguous and
10779 // do not overlap.
10780 auto CI = CurComponents.rbegin();
10781 auto CE = CurComponents.rend();
10782 auto SI = StackComponents.rbegin();
10783 auto SE = StackComponents.rend();
10784 for (; CI != CE && SI != SE; ++CI, ++SI) {
10785
10786 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10787 // At most one list item can be an array item derived from a given
10788 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010789 if (CurrentRegionOnly &&
10790 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10791 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10792 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10793 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10794 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010795 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010796 << CI->getAssociatedExpression()->getSourceRange();
10797 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10798 diag::note_used_here)
10799 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010800 return true;
10801 }
10802
10803 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010804 if (CI->getAssociatedExpression()->getStmtClass() !=
10805 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010806 break;
10807
10808 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010809 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010810 break;
10811 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000010812 // Check if the extra components of the expressions in the enclosing
10813 // data environment are redundant for the current base declaration.
10814 // If they are, the maps completely overlap, which is legal.
10815 for (; SI != SE; ++SI) {
10816 QualType Type;
10817 if (auto *ASE =
10818 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
10819 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
10820 } else if (auto *OASE =
10821 dyn_cast<OMPArraySectionExpr>(SI->getAssociatedExpression())) {
10822 auto *E = OASE->getBase()->IgnoreParenImpCasts();
10823 Type =
10824 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10825 }
10826 if (Type.isNull() || Type->isAnyPointerType() ||
10827 CheckArrayExpressionDoesNotReferToWholeSize(
10828 SemaRef, SI->getAssociatedExpression(), Type))
10829 break;
10830 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010831
10832 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10833 // List items of map clauses in the same construct must not share
10834 // original storage.
10835 //
10836 // If the expressions are exactly the same or one is a subset of the
10837 // other, it means they are sharing storage.
10838 if (CI == CE && SI == SE) {
10839 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010840 if (CKind == OMPC_map)
10841 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10842 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010843 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010844 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10845 << ERange;
10846 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010847 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10848 << RE->getSourceRange();
10849 return true;
10850 } else {
10851 // If we find the same expression in the enclosing data environment,
10852 // that is legal.
10853 IsEnclosedByDataEnvironmentExpr = true;
10854 return false;
10855 }
10856 }
10857
Samuel Antao90927002016-04-26 14:54:23 +000010858 QualType DerivedType =
10859 std::prev(CI)->getAssociatedDeclaration()->getType();
10860 SourceLocation DerivedLoc =
10861 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010862
10863 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10864 // If the type of a list item is a reference to a type T then the type
10865 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010866 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010867
10868 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10869 // A variable for which the type is pointer and an array section
10870 // derived from that variable must not appear as list items of map
10871 // clauses of the same construct.
10872 //
10873 // Also, cover one of the cases in:
10874 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10875 // If any part of the original storage of a list item has corresponding
10876 // storage in the device data environment, all of the original storage
10877 // must have corresponding storage in the device data environment.
10878 //
10879 if (DerivedType->isAnyPointerType()) {
10880 if (CI == CE || SI == SE) {
10881 SemaRef.Diag(
10882 DerivedLoc,
10883 diag::err_omp_pointer_mapped_along_with_derived_section)
10884 << DerivedLoc;
10885 } else {
10886 assert(CI != CE && SI != SE);
10887 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10888 << DerivedLoc;
10889 }
10890 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10891 << RE->getSourceRange();
10892 return true;
10893 }
10894
10895 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10896 // List items of map clauses in the same construct must not share
10897 // original storage.
10898 //
10899 // An expression is a subset of the other.
10900 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010901 if (CKind == OMPC_map)
10902 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10903 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010904 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010905 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10906 << ERange;
10907 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010908 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10909 << RE->getSourceRange();
10910 return true;
10911 }
10912
10913 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010914 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010915 if (!CurrentRegionOnly && SI != SE)
10916 EnclosingExpr = RE;
10917
10918 // The current expression is a subset of the expression in the data
10919 // environment.
10920 IsEnclosedByDataEnvironmentExpr |=
10921 (!CurrentRegionOnly && CI != CE && SI == SE);
10922
10923 return false;
10924 });
10925
10926 if (CurrentRegionOnly)
10927 return FoundError;
10928
10929 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10930 // If any part of the original storage of a list item has corresponding
10931 // storage in the device data environment, all of the original storage must
10932 // have corresponding storage in the device data environment.
10933 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10934 // If a list item is an element of a structure, and a different element of
10935 // the structure has a corresponding list item in the device data environment
10936 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010937 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010938 // data environment prior to the task encountering the construct.
10939 //
10940 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10941 SemaRef.Diag(ELoc,
10942 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10943 << ERange;
10944 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10945 << EnclosingExpr->getSourceRange();
10946 return true;
10947 }
10948
10949 return FoundError;
10950}
10951
Samuel Antao661c0902016-05-26 17:39:58 +000010952namespace {
10953// Utility struct that gathers all the related lists associated with a mappable
10954// expression.
10955struct MappableVarListInfo final {
10956 // The list of expressions.
10957 ArrayRef<Expr *> VarList;
10958 // The list of processed expressions.
10959 SmallVector<Expr *, 16> ProcessedVarList;
10960 // The mappble components for each expression.
10961 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10962 // The base declaration of the variable.
10963 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10964
10965 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10966 // We have a list of components and base declarations for each entry in the
10967 // variable list.
10968 VarComponents.reserve(VarList.size());
10969 VarBaseDeclarations.reserve(VarList.size());
10970 }
10971};
10972}
10973
10974// Check the validity of the provided variable list for the provided clause kind
10975// \a CKind. In the check process the valid expressions, and mappable expression
10976// components and variables are extracted and used to fill \a Vars,
10977// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10978// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10979static void
10980checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10981 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10982 SourceLocation StartLoc,
10983 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10984 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010985 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10986 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010987 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010988
Samuel Antao90927002016-04-26 14:54:23 +000010989 // Keep track of the mappable components and base declarations in this clause.
10990 // Each entry in the list is going to have a list of components associated. We
10991 // record each set of the components so that we can build the clause later on.
10992 // In the end we should have the same amount of declarations and component
10993 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010994
Samuel Antao661c0902016-05-26 17:39:58 +000010995 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010996 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010997 SourceLocation ELoc = RE->getExprLoc();
10998
Kelvin Li0bff7af2015-11-23 05:32:03 +000010999 auto *VE = RE->IgnoreParenLValueCasts();
11000
11001 if (VE->isValueDependent() || VE->isTypeDependent() ||
11002 VE->isInstantiationDependent() ||
11003 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011004 // We can only analyze this information once the missing information is
11005 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000011006 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011007 continue;
11008 }
11009
11010 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011011
Samuel Antao5de996e2016-01-22 20:21:36 +000011012 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000011013 SemaRef.Diag(ELoc,
11014 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000011015 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011016 continue;
11017 }
11018
Samuel Antao90927002016-04-26 14:54:23 +000011019 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
11020 ValueDecl *CurDeclaration = nullptr;
11021
11022 // Obtain the array or member expression bases if required. Also, fill the
11023 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000011024 auto *BE =
11025 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000011026 if (!BE)
11027 continue;
11028
Samuel Antao90927002016-04-26 14:54:23 +000011029 assert(!CurComponents.empty() &&
11030 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011031
Samuel Antao90927002016-04-26 14:54:23 +000011032 // For the following checks, we rely on the base declaration which is
11033 // expected to be associated with the last component. The declaration is
11034 // expected to be a variable or a field (if 'this' is being mapped).
11035 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
11036 assert(CurDeclaration && "Null decl on map clause.");
11037 assert(
11038 CurDeclaration->isCanonicalDecl() &&
11039 "Expecting components to have associated only canonical declarations.");
11040
11041 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
11042 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000011043
11044 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000011045 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000011046
11047 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000011048 // threadprivate variables cannot appear in a map clause.
11049 // OpenMP 4.5 [2.10.5, target update Construct]
11050 // threadprivate variables cannot appear in a from clause.
11051 if (VD && DSAS->isThreadPrivate(VD)) {
11052 auto DVar = DSAS->getTopDSA(VD, false);
11053 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
11054 << getOpenMPClauseName(CKind);
11055 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011056 continue;
11057 }
11058
Samuel Antao5de996e2016-01-22 20:21:36 +000011059 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
11060 // A list item cannot appear in both a map clause and a data-sharing
11061 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000011062
Samuel Antao5de996e2016-01-22 20:21:36 +000011063 // Check conflicts with other map clause expressions. We check the conflicts
11064 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000011065 // environment, because the restrictions are different. We only have to
11066 // check conflicts across regions for the map clauses.
11067 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11068 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011069 break;
Samuel Antao661c0902016-05-26 17:39:58 +000011070 if (CKind == OMPC_map &&
11071 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11072 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011073 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011074
Samuel Antao661c0902016-05-26 17:39:58 +000011075 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000011076 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11077 // If the type of a list item is a reference to a type T then the type will
11078 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011079 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011080
Samuel Antao661c0902016-05-26 17:39:58 +000011081 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
11082 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000011083 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000011084 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000011085 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
11086 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000011087 continue;
11088
Samuel Antao661c0902016-05-26 17:39:58 +000011089 if (CKind == OMPC_map) {
11090 // target enter data
11091 // OpenMP [2.10.2, Restrictions, p. 99]
11092 // A map-type must be specified in all map clauses and must be either
11093 // to or alloc.
11094 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
11095 if (DKind == OMPD_target_enter_data &&
11096 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
11097 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11098 << (IsMapTypeImplicit ? 1 : 0)
11099 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11100 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011101 continue;
11102 }
Samuel Antao661c0902016-05-26 17:39:58 +000011103
11104 // target exit_data
11105 // OpenMP [2.10.3, Restrictions, p. 102]
11106 // A map-type must be specified in all map clauses and must be either
11107 // from, release, or delete.
11108 if (DKind == OMPD_target_exit_data &&
11109 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
11110 MapType == OMPC_MAP_delete)) {
11111 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11112 << (IsMapTypeImplicit ? 1 : 0)
11113 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11114 << getOpenMPDirectiveName(DKind);
11115 continue;
11116 }
11117
11118 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11119 // A list item cannot appear in both a map clause and a data-sharing
11120 // attribute clause on the same construct
11121 if (DKind == OMPD_target && VD) {
11122 auto DVar = DSAS->getTopDSA(VD, false);
11123 if (isOpenMPPrivate(DVar.CKind)) {
11124 SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
11125 << getOpenMPClauseName(DVar.CKind)
11126 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11127 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11128 continue;
11129 }
11130 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011131 }
11132
Samuel Antao90927002016-04-26 14:54:23 +000011133 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000011134 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000011135
11136 // Store the components in the stack so that they can be used to check
11137 // against other clauses later on.
Samuel Antao661c0902016-05-26 17:39:58 +000011138 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
Samuel Antao90927002016-04-26 14:54:23 +000011139
11140 // Save the components and declaration to create the clause. For purposes of
11141 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000011142 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000011143 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11144 MVLI.VarComponents.back().append(CurComponents.begin(),
11145 CurComponents.end());
11146 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11147 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011148 }
Samuel Antao661c0902016-05-26 17:39:58 +000011149}
11150
11151OMPClause *
11152Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11153 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11154 SourceLocation MapLoc, SourceLocation ColonLoc,
11155 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11156 SourceLocation LParenLoc, SourceLocation EndLoc) {
11157 MappableVarListInfo MVLI(VarList);
11158 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11159 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011160
Samuel Antao5de996e2016-01-22 20:21:36 +000011161 // We need to produce a map clause even if we don't have variables so that
11162 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000011163 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11164 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11165 MVLI.VarComponents, MapTypeModifier, MapType,
11166 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011167}
Kelvin Li099bb8c2015-11-24 20:50:12 +000011168
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011169QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11170 TypeResult ParsedType) {
11171 assert(ParsedType.isUsable());
11172
11173 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11174 if (ReductionType.isNull())
11175 return QualType();
11176
11177 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11178 // A type name in a declare reduction directive cannot be a function type, an
11179 // array type, a reference type, or a type qualified with const, volatile or
11180 // restrict.
11181 if (ReductionType.hasQualifiers()) {
11182 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11183 return QualType();
11184 }
11185
11186 if (ReductionType->isFunctionType()) {
11187 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11188 return QualType();
11189 }
11190 if (ReductionType->isReferenceType()) {
11191 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11192 return QualType();
11193 }
11194 if (ReductionType->isArrayType()) {
11195 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11196 return QualType();
11197 }
11198 return ReductionType;
11199}
11200
11201Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11202 Scope *S, DeclContext *DC, DeclarationName Name,
11203 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11204 AccessSpecifier AS, Decl *PrevDeclInScope) {
11205 SmallVector<Decl *, 8> Decls;
11206 Decls.reserve(ReductionTypes.size());
11207
11208 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11209 ForRedeclaration);
11210 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11211 // A reduction-identifier may not be re-declared in the current scope for the
11212 // same type or for a type that is compatible according to the base language
11213 // rules.
11214 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11215 OMPDeclareReductionDecl *PrevDRD = nullptr;
11216 bool InCompoundScope = true;
11217 if (S != nullptr) {
11218 // Find previous declaration with the same name not referenced in other
11219 // declarations.
11220 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11221 InCompoundScope =
11222 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11223 LookupName(Lookup, S);
11224 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11225 /*AllowInlineNamespace=*/false);
11226 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11227 auto Filter = Lookup.makeFilter();
11228 while (Filter.hasNext()) {
11229 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11230 if (InCompoundScope) {
11231 auto I = UsedAsPrevious.find(PrevDecl);
11232 if (I == UsedAsPrevious.end())
11233 UsedAsPrevious[PrevDecl] = false;
11234 if (auto *D = PrevDecl->getPrevDeclInScope())
11235 UsedAsPrevious[D] = true;
11236 }
11237 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11238 PrevDecl->getLocation();
11239 }
11240 Filter.done();
11241 if (InCompoundScope) {
11242 for (auto &PrevData : UsedAsPrevious) {
11243 if (!PrevData.second) {
11244 PrevDRD = PrevData.first;
11245 break;
11246 }
11247 }
11248 }
11249 } else if (PrevDeclInScope != nullptr) {
11250 auto *PrevDRDInScope = PrevDRD =
11251 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11252 do {
11253 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11254 PrevDRDInScope->getLocation();
11255 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11256 } while (PrevDRDInScope != nullptr);
11257 }
11258 for (auto &TyData : ReductionTypes) {
11259 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11260 bool Invalid = false;
11261 if (I != PreviousRedeclTypes.end()) {
11262 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11263 << TyData.first;
11264 Diag(I->second, diag::note_previous_definition);
11265 Invalid = true;
11266 }
11267 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11268 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11269 Name, TyData.first, PrevDRD);
11270 DC->addDecl(DRD);
11271 DRD->setAccess(AS);
11272 Decls.push_back(DRD);
11273 if (Invalid)
11274 DRD->setInvalidDecl();
11275 else
11276 PrevDRD = DRD;
11277 }
11278
11279 return DeclGroupPtrTy::make(
11280 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11281}
11282
11283void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11284 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11285
11286 // Enter new function scope.
11287 PushFunctionScope();
11288 getCurFunction()->setHasBranchProtectedScope();
11289 getCurFunction()->setHasOMPDeclareReductionCombiner();
11290
11291 if (S != nullptr)
11292 PushDeclContext(S, DRD);
11293 else
11294 CurContext = DRD;
11295
11296 PushExpressionEvaluationContext(PotentiallyEvaluated);
11297
11298 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011299 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11300 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11301 // uses semantics of argument handles by value, but it should be passed by
11302 // reference. C lang does not support references, so pass all parameters as
11303 // pointers.
11304 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011305 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011306 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011307 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11308 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11309 // uses semantics of argument handles by value, but it should be passed by
11310 // reference. C lang does not support references, so pass all parameters as
11311 // pointers.
11312 // Create 'T omp_out;' variable.
11313 auto *OmpOutParm =
11314 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11315 if (S != nullptr) {
11316 PushOnScopeChains(OmpInParm, S);
11317 PushOnScopeChains(OmpOutParm, S);
11318 } else {
11319 DRD->addDecl(OmpInParm);
11320 DRD->addDecl(OmpOutParm);
11321 }
11322}
11323
11324void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11325 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11326 DiscardCleanupsInEvaluationContext();
11327 PopExpressionEvaluationContext();
11328
11329 PopDeclContext();
11330 PopFunctionScopeInfo();
11331
11332 if (Combiner != nullptr)
11333 DRD->setCombiner(Combiner);
11334 else
11335 DRD->setInvalidDecl();
11336}
11337
11338void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11339 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11340
11341 // Enter new function scope.
11342 PushFunctionScope();
11343 getCurFunction()->setHasBranchProtectedScope();
11344
11345 if (S != nullptr)
11346 PushDeclContext(S, DRD);
11347 else
11348 CurContext = DRD;
11349
11350 PushExpressionEvaluationContext(PotentiallyEvaluated);
11351
11352 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011353 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11354 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11355 // uses semantics of argument handles by value, but it should be passed by
11356 // reference. C lang does not support references, so pass all parameters as
11357 // pointers.
11358 // Create 'T omp_priv;' variable.
11359 auto *OmpPrivParm =
11360 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011361 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11362 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11363 // uses semantics of argument handles by value, but it should be passed by
11364 // reference. C lang does not support references, so pass all parameters as
11365 // pointers.
11366 // Create 'T omp_orig;' variable.
11367 auto *OmpOrigParm =
11368 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011369 if (S != nullptr) {
11370 PushOnScopeChains(OmpPrivParm, S);
11371 PushOnScopeChains(OmpOrigParm, S);
11372 } else {
11373 DRD->addDecl(OmpPrivParm);
11374 DRD->addDecl(OmpOrigParm);
11375 }
11376}
11377
11378void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11379 Expr *Initializer) {
11380 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11381 DiscardCleanupsInEvaluationContext();
11382 PopExpressionEvaluationContext();
11383
11384 PopDeclContext();
11385 PopFunctionScopeInfo();
11386
11387 if (Initializer != nullptr)
11388 DRD->setInitializer(Initializer);
11389 else
11390 DRD->setInvalidDecl();
11391}
11392
11393Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11394 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11395 for (auto *D : DeclReductions.get()) {
11396 if (IsValid) {
11397 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11398 if (S != nullptr)
11399 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11400 } else
11401 D->setInvalidDecl();
11402 }
11403 return DeclReductions;
11404}
11405
Kelvin Li099bb8c2015-11-24 20:50:12 +000011406OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11407 SourceLocation StartLoc,
11408 SourceLocation LParenLoc,
11409 SourceLocation EndLoc) {
11410 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011411
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011412 // OpenMP [teams Constrcut, Restrictions]
11413 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011414 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11415 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011416 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011417
11418 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11419}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011420
11421OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11422 SourceLocation StartLoc,
11423 SourceLocation LParenLoc,
11424 SourceLocation EndLoc) {
11425 Expr *ValExpr = ThreadLimit;
11426
11427 // OpenMP [teams Constrcut, Restrictions]
11428 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011429 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11430 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011431 return nullptr;
11432
11433 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11434 EndLoc);
11435}
Alexey Bataeva0569352015-12-01 10:17:31 +000011436
11437OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11438 SourceLocation StartLoc,
11439 SourceLocation LParenLoc,
11440 SourceLocation EndLoc) {
11441 Expr *ValExpr = Priority;
11442
11443 // OpenMP [2.9.1, task Constrcut]
11444 // The priority-value is a non-negative numerical scalar expression.
11445 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11446 /*StrictlyPositive=*/false))
11447 return nullptr;
11448
11449 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11450}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011451
11452OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11453 SourceLocation StartLoc,
11454 SourceLocation LParenLoc,
11455 SourceLocation EndLoc) {
11456 Expr *ValExpr = Grainsize;
11457
11458 // OpenMP [2.9.2, taskloop Constrcut]
11459 // The parameter of the grainsize clause must be a positive integer
11460 // expression.
11461 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11462 /*StrictlyPositive=*/true))
11463 return nullptr;
11464
11465 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11466}
Alexey Bataev382967a2015-12-08 12:06:20 +000011467
11468OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11469 SourceLocation StartLoc,
11470 SourceLocation LParenLoc,
11471 SourceLocation EndLoc) {
11472 Expr *ValExpr = NumTasks;
11473
11474 // OpenMP [2.9.2, taskloop Constrcut]
11475 // The parameter of the num_tasks clause must be a positive integer
11476 // expression.
11477 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11478 /*StrictlyPositive=*/true))
11479 return nullptr;
11480
11481 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11482}
11483
Alexey Bataev28c75412015-12-15 08:19:24 +000011484OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11485 SourceLocation LParenLoc,
11486 SourceLocation EndLoc) {
11487 // OpenMP [2.13.2, critical construct, Description]
11488 // ... where hint-expression is an integer constant expression that evaluates
11489 // to a valid lock hint.
11490 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11491 if (HintExpr.isInvalid())
11492 return nullptr;
11493 return new (Context)
11494 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11495}
11496
Carlo Bertollib4adf552016-01-15 18:50:31 +000011497OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11498 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11499 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11500 SourceLocation EndLoc) {
11501 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11502 std::string Values;
11503 Values += "'";
11504 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11505 Values += "'";
11506 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11507 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11508 return nullptr;
11509 }
11510 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011511 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011512 if (ChunkSize) {
11513 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11514 !ChunkSize->isInstantiationDependent() &&
11515 !ChunkSize->containsUnexpandedParameterPack()) {
11516 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11517 ExprResult Val =
11518 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11519 if (Val.isInvalid())
11520 return nullptr;
11521
11522 ValExpr = Val.get();
11523
11524 // OpenMP [2.7.1, Restrictions]
11525 // chunk_size must be a loop invariant integer expression with a positive
11526 // value.
11527 llvm::APSInt Result;
11528 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11529 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11530 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11531 << "dist_schedule" << ChunkSize->getSourceRange();
11532 return nullptr;
11533 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011534 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11535 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011536 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11537 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11538 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011539 }
11540 }
11541 }
11542
11543 return new (Context)
11544 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011545 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011546}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011547
11548OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11549 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11550 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11551 SourceLocation KindLoc, SourceLocation EndLoc) {
11552 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11553 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11554 Kind != OMPC_DEFAULTMAP_scalar) {
11555 std::string Value;
11556 SourceLocation Loc;
11557 Value += "'";
11558 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11559 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11560 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11561 Loc = MLoc;
11562 } else {
11563 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11564 OMPC_DEFAULTMAP_scalar);
11565 Loc = KindLoc;
11566 }
11567 Value += "'";
11568 Diag(Loc, diag::err_omp_unexpected_clause_value)
11569 << Value << getOpenMPClauseName(OMPC_defaultmap);
11570 return nullptr;
11571 }
11572
11573 return new (Context)
11574 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11575}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011576
11577bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11578 DeclContext *CurLexicalContext = getCurLexicalContext();
11579 if (!CurLexicalContext->isFileContext() &&
11580 !CurLexicalContext->isExternCContext() &&
11581 !CurLexicalContext->isExternCXXContext()) {
11582 Diag(Loc, diag::err_omp_region_not_file_context);
11583 return false;
11584 }
11585 if (IsInOpenMPDeclareTargetContext) {
11586 Diag(Loc, diag::err_omp_enclosed_declare_target);
11587 return false;
11588 }
11589
11590 IsInOpenMPDeclareTargetContext = true;
11591 return true;
11592}
11593
11594void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11595 assert(IsInOpenMPDeclareTargetContext &&
11596 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11597
11598 IsInOpenMPDeclareTargetContext = false;
11599}
11600
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011601void
11602Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11603 const DeclarationNameInfo &Id,
11604 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11605 NamedDeclSetType &SameDirectiveDecls) {
11606 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11607 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11608
11609 if (Lookup.isAmbiguous())
11610 return;
11611 Lookup.suppressDiagnostics();
11612
11613 if (!Lookup.isSingleResult()) {
11614 if (TypoCorrection Corrected =
11615 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11616 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11617 CTK_ErrorRecovery)) {
11618 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11619 << Id.getName());
11620 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11621 return;
11622 }
11623
11624 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11625 return;
11626 }
11627
11628 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11629 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11630 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11631 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11632
11633 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11634 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11635 ND->addAttr(A);
11636 if (ASTMutationListener *ML = Context.getASTMutationListener())
11637 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11638 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11639 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11640 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11641 << Id.getName();
11642 }
11643 } else
11644 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11645}
11646
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011647static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11648 Sema &SemaRef, Decl *D) {
11649 if (!D)
11650 return;
11651 Decl *LD = nullptr;
11652 if (isa<TagDecl>(D)) {
11653 LD = cast<TagDecl>(D)->getDefinition();
11654 } else if (isa<VarDecl>(D)) {
11655 LD = cast<VarDecl>(D)->getDefinition();
11656
11657 // If this is an implicit variable that is legal and we do not need to do
11658 // anything.
11659 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011660 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11661 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11662 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011663 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011664 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011665 return;
11666 }
11667
11668 } else if (isa<FunctionDecl>(D)) {
11669 const FunctionDecl *FD = nullptr;
11670 if (cast<FunctionDecl>(D)->hasBody(FD))
11671 LD = const_cast<FunctionDecl *>(FD);
11672
11673 // If the definition is associated with the current declaration in the
11674 // target region (it can be e.g. a lambda) that is legal and we do not need
11675 // to do anything else.
11676 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011677 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11678 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11679 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011680 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011681 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011682 return;
11683 }
11684 }
11685 if (!LD)
11686 LD = D;
11687 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11688 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11689 // Outlined declaration is not declared target.
11690 if (LD->isOutOfLine()) {
11691 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11692 SemaRef.Diag(SL, diag::note_used_here) << SR;
11693 } else {
11694 DeclContext *DC = LD->getDeclContext();
11695 while (DC) {
11696 if (isa<FunctionDecl>(DC) &&
11697 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11698 break;
11699 DC = DC->getParent();
11700 }
11701 if (DC)
11702 return;
11703
11704 // Is not declared in target context.
11705 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11706 SemaRef.Diag(SL, diag::note_used_here) << SR;
11707 }
11708 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011709 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11710 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11711 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011712 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011713 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011714 }
11715}
11716
11717static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11718 Sema &SemaRef, DSAStackTy *Stack,
11719 ValueDecl *VD) {
11720 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11721 return true;
11722 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11723 return false;
11724 return true;
11725}
11726
11727void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11728 if (!D || D->isInvalidDecl())
11729 return;
11730 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11731 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11732 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11733 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11734 if (DSAStack->isThreadPrivate(VD)) {
11735 Diag(SL, diag::err_omp_threadprivate_in_target);
11736 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11737 return;
11738 }
11739 }
11740 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11741 // Problem if any with var declared with incomplete type will be reported
11742 // as normal, so no need to check it here.
11743 if ((E || !VD->getType()->isIncompleteType()) &&
11744 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11745 // Mark decl as declared target to prevent further diagnostic.
11746 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011747 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11748 Context, OMPDeclareTargetDeclAttr::MT_To);
11749 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011750 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011751 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011752 }
11753 return;
11754 }
11755 }
11756 if (!E) {
11757 // Checking declaration inside declare target region.
11758 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11759 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011760 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11761 Context, OMPDeclareTargetDeclAttr::MT_To);
11762 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011763 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011764 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011765 }
11766 return;
11767 }
11768 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11769}
Samuel Antao661c0902016-05-26 17:39:58 +000011770
11771OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11772 SourceLocation StartLoc,
11773 SourceLocation LParenLoc,
11774 SourceLocation EndLoc) {
11775 MappableVarListInfo MVLI(VarList);
11776 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11777 if (MVLI.ProcessedVarList.empty())
11778 return nullptr;
11779
11780 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11781 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11782 MVLI.VarComponents);
11783}
Samuel Antaoec172c62016-05-26 17:49:04 +000011784
11785OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11786 SourceLocation StartLoc,
11787 SourceLocation LParenLoc,
11788 SourceLocation EndLoc) {
11789 MappableVarListInfo MVLI(VarList);
11790 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11791 if (MVLI.ProcessedVarList.empty())
11792 return nullptr;
11793
11794 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11795 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11796 MVLI.VarComponents);
11797}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011798
11799OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11800 SourceLocation StartLoc,
11801 SourceLocation LParenLoc,
11802 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000011803 MappableVarListInfo MVLI(VarList);
11804 SmallVector<Expr *, 8> PrivateCopies;
11805 SmallVector<Expr *, 8> Inits;
11806
Carlo Bertolli2404b172016-07-13 15:37:16 +000011807 for (auto &RefExpr : VarList) {
11808 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11809 SourceLocation ELoc;
11810 SourceRange ERange;
11811 Expr *SimpleRefExpr = RefExpr;
11812 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11813 if (Res.second) {
11814 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000011815 MVLI.ProcessedVarList.push_back(RefExpr);
11816 PrivateCopies.push_back(nullptr);
11817 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000011818 }
11819 ValueDecl *D = Res.first;
11820 if (!D)
11821 continue;
11822
11823 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000011824 Type = Type.getNonReferenceType().getUnqualifiedType();
11825
11826 auto *VD = dyn_cast<VarDecl>(D);
11827
11828 // Item should be a pointer or reference to pointer.
11829 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000011830 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11831 << 0 << RefExpr->getSourceRange();
11832 continue;
11833 }
Samuel Antaocc10b852016-07-28 14:23:26 +000011834
11835 // Build the private variable and the expression that refers to it.
11836 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
11837 D->hasAttrs() ? &D->getAttrs() : nullptr);
11838 if (VDPrivate->isInvalidDecl())
11839 continue;
11840
11841 CurContext->addDecl(VDPrivate);
11842 auto VDPrivateRefExpr = buildDeclRefExpr(
11843 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
11844
11845 // Add temporary variable to initialize the private copy of the pointer.
11846 auto *VDInit =
11847 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
11848 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
11849 RefExpr->getExprLoc());
11850 AddInitializerToDecl(VDPrivate,
11851 DefaultLvalueConversion(VDInitRefExpr).get(),
11852 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
11853
11854 // If required, build a capture to implement the privatization initialized
11855 // with the current list item value.
11856 DeclRefExpr *Ref = nullptr;
11857 if (!VD)
11858 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11859 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
11860 PrivateCopies.push_back(VDPrivateRefExpr);
11861 Inits.push_back(VDInitRefExpr);
11862
11863 // We need to add a data sharing attribute for this variable to make sure it
11864 // is correctly captured. A variable that shows up in a use_device_ptr has
11865 // similar properties of a first private variable.
11866 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
11867
11868 // Create a mappable component for the list item. List items in this clause
11869 // only need a component.
11870 MVLI.VarBaseDeclarations.push_back(D);
11871 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11872 MVLI.VarComponents.back().push_back(
11873 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000011874 }
11875
Samuel Antaocc10b852016-07-28 14:23:26 +000011876 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000011877 return nullptr;
11878
Samuel Antaocc10b852016-07-28 14:23:26 +000011879 return OMPUseDevicePtrClause::Create(
11880 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
11881 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000011882}
Carlo Bertolli70594e92016-07-13 17:16:49 +000011883
11884OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11885 SourceLocation StartLoc,
11886 SourceLocation LParenLoc,
11887 SourceLocation EndLoc) {
11888 SmallVector<Expr *, 8> Vars;
11889 for (auto &RefExpr : VarList) {
11890 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11891 SourceLocation ELoc;
11892 SourceRange ERange;
11893 Expr *SimpleRefExpr = RefExpr;
11894 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11895 if (Res.second) {
11896 // It will be analyzed later.
11897 Vars.push_back(RefExpr);
11898 }
11899 ValueDecl *D = Res.first;
11900 if (!D)
11901 continue;
11902
11903 QualType Type = D->getType();
11904 // item should be a pointer or array or reference to pointer or array
11905 if (!Type.getNonReferenceType()->isPointerType() &&
11906 !Type.getNonReferenceType()->isArrayType()) {
11907 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11908 << 0 << RefExpr->getSourceRange();
11909 continue;
11910 }
11911 Vars.push_back(RefExpr->IgnoreParens());
11912 }
11913
11914 if (Vars.empty())
11915 return nullptr;
11916
11917 return OMPIsDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11918 Vars);
11919}